swing - Trouble drawing with MouseDragged in Java -
there's problem drawing or setting size of jpanel when drag mouse, location setted click , size depending of drag position(x , y) drawing resizable rectangle(jpanel).
private void panelmousedragged(java.awt.event.mouseevent evt) { rsx = (int)mouseinfo.getpointerinfo().getlocation().getx(); rsy = (int)mouseinfo.getpointerinfo().getlocation().gety(); rectangulodefault.setbounds(rx,ry,rsx-rx,rsy-ry); } private void panelmousepressed(java.awt.event.mouseevent evt) { rx = (int)mouseinfo.getpointerinfo().getlocation().getx(); ry = (int)mouseinfo.getpointerinfo().getlocation().gety(); rectangulodefault.setlocation(rx,ry); }
but when drag mouse in negative coordinate of click(start of drawing) disappear.
here better explain http://i.picasion.com/resize80/49c88c55d4c11c53c020acfcc4fc2f45.png
but when drag mouse in negative coordinate
rectangulodefault.setbounds(rx,ry,rsx-rx,rsy-ry);
your width/height calculation assumes drag mouse in positive direction.
you need use absolute value of 2 points:
int width = math.abs(rsx - rx); int height = math.abs(rsy - ry); rectangulodefault.setbounds(rx, ry, width, height);
your x/y values need minimum of (rx , rsx) , (ry , rsy). can use math.min(...) method this.
Comments
Post a Comment