<< Chapter < Page Chapter >> Page >

Behavior of the rotatePicture method

The rotatePicture method performs the following operations:

  • Prepare an AffineTransform object that can be used to rotate the incoming image around its center by the specified angle.
  • Get the dimensions of a rectangle of sufficient size to contain the rotated image.
  • Prepare an AffineTransform object that will translate the rotated image to the center of a new, larger Picture object having the dimensions computed above.
  • Concatenate the rotation transform object with the translation transform object.
  • Create a new Picture object with the dimensions computed above.
  • Apply the concatenated transform to the incoming image and draw the transformed image in the new Picture object.
  • Return a reference to the new Picture object containing the rotated and translated image.

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:

  • theta - the angle of rotation measured in radians
  • anchorx - the X coordinate of the rotation anchor point
  • anchory - the Y coordinate of the rotation anchor point

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());

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