Drawing shapes in Java

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
I am trying to draw any number of shapes on a JPanel in java.

Right now I have my onMouseClick event logging the starting point for the shape. I have my on mouse drag logging points as it drags and repainting as that changes.

The thing is, in Java, the first point is the top left point of the shape. I can't figure out how to draw the shape starting at any other corner.

For instance, if I draw my fillRect at starting point (40, 40), and then drag the mouse to (20, 20) how do I get the rectangle to show up in that space and NOT from 40, 40 to 60, 60?

I am working on this in a college project, so do not write the code. If you could point me in the right direction; however, that would be much appreciated. I've been stuck on this part for a good 2 hours :(

Thanks,
-Kevin
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
If the x drag direction takes you to a point where x_new < x_old (i.e., they've dragged left beyond the initial click), then set start_point = (x_new, y_old) and adjust the width accordingly (same with the y direction). That should take care of it.
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
I actually figured it out for the rectangle. Its working very well; however, implementing it for Square is requiring me to modify it a little more than I thought ;)

-Kevin