<< Chapter < Page | Chapter >> Page > |
Behavior of the rotatePicture method
The rotatePicture method performs the following operations:
Prepare the rotation transform
Listing 4 begins by instantiating a new object of the AffineTransform class and saving the object's reference in the local reference variable named rotateTransform .
Call an overloaded rotate method
Then Listing 4 calls one of four overloaded rotate methods on the rotation transform object.
Three parameters are required
This version of the rotate method requires three parameters:
To make a long story short...
The rotate method prepares the transform object to rotate an image around the pointspecified by the last two parameters.
The angle of rotation must be specified in radians.
Convert from degrees to radians
Listing 4 calls the static toRadians method of the Math class to convert the rotation angle from degrees to radians. The angle in radians is passed as the first parameter (theta) to the rotate method.
Compute the anchor point
Then the code in Listing 4 computes the coordinates of the center of the image and passes those coordinates to the rotate method as anchorx and anchory .
The dimensions of the new Picture object
How would you compute the dimensions of the new Picture object required to barely contain the rotated image shown in Figure 3 ?
The computation of those dimensions is not rocket science, but would certainly require you to know quite a lot about dealing with angles andtriangles.
Fortunately, we don't have to perform that computation
Ericson provides a method named getTransformEnclosingRect that will perform that computation for us, returning the required dimensions inthe form of a reference to a standard Java Rectangle2D object.
Compute the dimensions of the new Picture object
The code in Listing 5 calls the getTransformEnclosingRect method on the previously scaled Picture object passing a reference to the rotation transform object to get the required dimensions for a Picture object that will contain the rotated image.
Listing 5 - Compute the dimensions of the new Picture object. |
---|
Rectangle2D rectangle2D =
pic.getTransformEnclosingRect(rotateTransform);int resultWidth = (int)(rectangle2D.getWidth());
int resultHeight = (int)(rectangle2D.getHeight()); |
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?