<< Chapter < Page Chapter >> Page >

Beginning of the method named rotatePicture

The method named rotatePicture begins in Listing 4 .

Listing 4 - Beginning of the method named rotatePicture.
private Picture rotatePicture(Picture pic,double angle){ //Prepare the rotation transformAffineTransform rotateTransform = new AffineTransform();rotateTransform.rotate(Math.toRadians(angle), pic.getWidth()/2,pic.getHeight()/2);

Rotate and translate

The rotatePicture method accepts a reference to a Picture object along with a rotation angle in degrees.

It creates and returns a new Picture object that is of the correct size, containing the rotated version of the image as shown in Figure 3 .

The incoming image is rotated around its center by the specified rotation angle. Then it is translated to and drawn in the center of the new Picture object.

Affine transforms

The rotatePicture method uses affine transforms to rotate and translate the image. Affine transforms can also be used to scaleimages, but it is easier to scale images using Ericson's scale method.

However, the lack of complexity of the scale method is easily made up for by the complexity of affine transforms.

Google me

I have published several tutorials discussing and explaining the use of the AffineTransform class in Java. You can locate those modules by going to Google and searching for the following keywords:

richard baldwin affine transform

The AffineTransform class

The AffineTransform class is part of the standard Java library. Here is part of what the documentation has to say about the class:

"The AffineTransform class represents a 2D affine transform that performs a linear mapping from 2D coordinates to other 2Dcoordinates that preserves the "straightness" and "parallelness" of lines. Affine transformations can be constructed using sequences of translations,scales, flips, rotations, and shears."

The ideas behind affine transforms

One of the ideas behind affine transforms is that you can create an affine transform object and apply it to an unlimited number of other objects. Thismight be useful in a game program, for example, where a large number of enemy ships need to be rotated, translated, and scaled in unison.

Concatenated affine transform objects

Another idea is that you can create two or more affine transform objects, concatenate them, and apply the concatenated transform object to an unlimitednumber of other objects.

Application of concatenated transform objects

Applying a concatenated transform to an object is equivalent to applying one of the transform objects to the original object and then applying the othertransform objects to the transformed objects in sequential fashion. Concatenation of transform objects can result in considerable computationalsavings in certain situations.

A larger Picture object is required

Looking back at Figure 2 and Figure 3 , you can see that the Picture object required to contain the rotated image must be larger than the Picture object required to contain the original image. You will learn how to compute the dimensions of the larger Picture object later in this module.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask