The values of x
and y
that are being manipulated in the rotate
method will not be seen in the method that is calling it because Java passes method arguments by value.
Therefore, the x
and y
values that are being changed in the rotate
method is a local copy, so once it goes out of scope (i.e. returning from the rotate
method to its calling method), the values of x
and y
will disappear.
So currently, what is happening is:
x = 10;
y = 10;
o1 = new obj();
o1.a = 100;
rotate(x, y, obj);
System.out.println(x); // Still prints 10
System.out.println(y); // Still prints 10
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 rotate
to return a Point
:
public Point rotate(int x, int y, double angle)
{
// Do rotation.
return new Point(newX, newY);
}
public void callingMethod()
{
int x = 10;
int y = 10;
p = rotate(x, y, 45);
System.out.println(x); // Should print something other than 10.
System.out.println(y); // Should print something other than 10.
}
That said, as Pierre suggests, using the AffineTransform would be much easier in my opinion.
For example, creating a Rectangle
object and rotating it using AffineTransform
can be performed by the following:
Rectangle rect = new Rectangle(0, 0, 10, 10);
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(45));
Shape rotatedRect = at.createTransformedShape(rect);
AffineTransform
can be applied to classes which implement the Shape
interface. A list of classes implementing Shape
can be found in the linked Java API specifications for the Shape
interface.
For more information on how to use AffineTransform
and Java 2D: