<< Chapter < Page | Chapter >> Page > |
The dimensions of the rectangle
After getting a reference to the rectangle, Listing 5 gets and saves the width and height of the rectangle. These values will be used later to instantiate a new Picture object of the same size as the rectangle.
Prepare the translation transform
Listing 6 prepares a translation transform that can be used to translate the rotated image to the center of the new Picture object.
Listing 6 - Prepare the translation transform. |
---|
AffineTransform translateTransform =
new AffineTransform();translateTransform.translate(
(resultWidth - pic.getWidth())/2,(resultHeight - pic.getHeight())/2); |
A new AffineTransform object
Listing 6 begins by instantiating a new object of the AffineTransform class and saving the object's reference in the local reference variable named translateTransform .
Call the translate method on the transform object
Then Listing 6 calls the translate method on the AffineTransform object.
According to the documentation , the required parameters of the translate method are:
Compute the translation distance components
Listing 6 computes the distance from the center of the image to the center of the new Picture object and passes the X and Y components of this distance to the translate method.
Two AffineTransform objects
At this point, we have two different AffineTransform objects. One is capable of rotating the image by a specified angle. The other iscapable of translating the image by a specified amount.
We could apply the two transforms sequentially to the image being careful to rotate before we translate. (The order of rotation and translation makes a huge difference.)
A more computationally economical approach
The preferred approach is to concatenate the two transform objects and apply only the concatenated transform object to the image. This is particularlyimportant if the transforms are going to be applied to a large number of images such as in a game program for example.
Concatenate the transforms
Listing 7 calls the concatenate method on the translation transform passing a reference to the rotation transform as a parameter. Thismodifies the translation transform in such a way that it can be used to rotate the image around its center point and then translate it to the center of the new Picture object.
Listing 7 - Concatenate the transforms. |
---|
translateTransform.concatenate(rotateTransform); |
Instantiate the new Picture object
Listing 8 instantiates a new Picture object with the dimensions computed in Listing 5 . This Picture object will be used to contain and return the rotated image.
Listing 8 - Instantiate the new Picture object . |
---|
Picture result = new Picture(
resultWidth,resultHeight); |
Perform the concatenated transform
Listing 9 performs the rotation and translation, draws the modified image in the new Picture object, and returns a reference to the new Picture object.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?