my dog learned polymorphism
The moose likes Beginning Java and the fly likes How to change the angle of the polygon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to change the angle of the polygon" Watch "How to change the angle of the polygon" New topic

How to change the angle of the polygon

vv ss

Joined: Apr 24, 2001
Posts: 1
I want to change the angle of the polygon when the user drags it?
I don't understand the relationship between angles and shapes. can anyone explain this to me?
vv ss.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, vv ss.
Well, presumably within your paint() method, you'll have something like

where polygonXs is an integer array of the polygon's x-coordinates, and polygonYs is an integer array of the polygon's y-coordinates.
So if your class is called "Polygon", I would define instance variables polygonXs and polygonYs as arrays of ints, giving them some default starting values.
This class will also have to implement MouseListener and MouseMotionListener.
When the mouse is clicked, you will need to determine whether the click is in the proximity of one of the polygon's corners: if it is, then "remember" which corner the user has, probably in a third instance variable.
When the mouse is dragged, then, you will have to update the single, changing corner's x- and y-coordinates in the polygonXs and polygonYs arrays, and do a repaint().
When the mouse is released, you'll need to once again "forget" which corner the user had grabbed; I recommend just setting that instance variable that contained the index of the grabbed corner back to -1. In your mouseDragged() method, then, you'll need to be sure that you update your polygonXs and polygonYs arrays only if that instance variable is greater than -1.
If your polygon has more than three sides, you'll need to ensure that the user can't drag one of the corners to make something that isn't a polygon. (For example, if you start out with a square, don't allow the user to pull the upper-right-hand corner directly left beyond the square's upper-left-hand corner, or you'll be drawing two triangles. Maybe that's OK for your app, I don't know.)
Sounds like a good project, vv ss. Good luck.
jQuery in Action, 3rd edition
subject: How to change the angle of the polygon
jQuery in Action, 3rd edition