Okay I’m trying to rotate a Java Polygon based on it’s original position of angle 0. x, and y end up being converted to an int at the end of me using them, so I could understand not seeing some change, but when the difference in angles is big like 0 to 180 I think I should see something.
I’ve been at this for a little while and can’t think of what it is. Here’s the method. (Sorry if it messes up in the code tags, my firefox messes them up.)
public void rotate(double x, double y, obj o1) { double dist = Math.sqrt(Math.pow(x - (o1.x + (o1.w/2)), 2) + Math.pow(y - (o1.y + (o1.h/2)),2)); x += (Math.sin(Math.toRadians(o1.a)) * dist); y -= (Math.cos(Math.toRadians(o1.a)) * dist); }
The values of
xandythat are being manipulated in therotatemethod will not be seen in the method that is calling it because Java passes method arguments by value.Therefore, the
xandyvalues that are being changed in therotatemethod is a local copy, so once it goes out of scope (i.e. returning from therotatemethod to its calling method), the values ofxandywill disappear.So currently, what is happening is:
The only way to get multiple values back from a method in Java is to pass an object, and manipulate the object that is passed in. (Actually, a copy of the reference to the object is passed in when an method call is made.)
For example, redefining
rotateto return aPoint:That said, as Pierre suggests, using the AffineTransform would be much easier in my opinion.
For example, creating a
Rectangleobject and rotating it usingAffineTransformcan be performed by the following:AffineTransformcan be applied to classes which implement theShapeinterface. A list of classes implementingShapecan be found in the linked Java API specifications for theShapeinterface.For more information on how to use
AffineTransformand Java 2D: