Affine Transforms in Java2D

title page &
other versions

.

 

6. AffineTransform

Instances of the class java.awt.geom.AffineTransform represent the transformation from one coordinate system to another.  AffineTransform was used to save and restore the transform state of a Graphics2D in a previous example.  AffineTransform can also be useful when you need to compute and store transformed coordinates before you draw.

There are several ways to create and build up AffineTransform instances.  The simplest is to build them up the same way one can in Graphics2D:

Building by Transformation

AffineTransform a = new AffineTransform();
     -- the no argument constructor creates an identity transform

a.translate(x, y);
a.rotate(theta);
a.rotate(theta, x, y);
a.scale(x, y);
a.shear(x, y);

Each of these methods operates just like the equivalent version in Graphics2D.   Each interprets its arguments in the current transformed coordinates, then transforms the system appropriately.  Using these methods a single AffineTransform instance can be built up to represent a whole sequence of transformations.

If all you need is an AffineTransform to represent one of these transformations, it can be created in one step:

Getting an Instance

AffineTransform a;

a = AffineTransform.getTranslateInstance(x, y);
a = AffineTransform.getRotate(theta);
a = AffineTransform.getRotate(theta, x, y);
a = AffineTransform.getScale(x, y);
a = AffineTransform.getShear(x, y);

Each of these creates an AffineTransform representing the appropriate transformation.

Two instances of AffineTransform can be combined with the concatenate method. For example, the following two code sequences produce the same transform:

Via concatenate

AffineTransform t,r;

t = AffineTransform.getTranslateInstance(
         10.0, 0.0);
r = AffineTransform.getRotateInstance(
         45.0 * Math.PI / 180.0);

t.concatenate(r);
   
-- this modifies t

return t;

Via Transformation Methods

AffineTransform a;

a = new AfineTransform();

a.translate(10.0, 0.0);
a.rotate(45.0 * Math.PI / 180.0);

return a;

The concatenate method combines two AffineTransform instances into one.  The resulting transform is the coordinate system transform of the first transform, followed by the transform of the second.  The first transform instance is modified to be this resulting transform (much the way the transformation methods modify the instance).   Which of these two methods you use is largely a matter of preference and situation.   If you are being passed AffineTransform objects then you need to manipulate them with concatenate.

There is also a method preConcatenate.  This method is the same as concatenate only the order in which the transforms is combined is reversed:  The resulting transform is the coordinate system transform of the second followed by the transform of the first.  The first transform instance is still the one modified to be the resulting transform.  This method is much less useful.

Once you have an instance of AffineTransform, you can use it several ways.   AffineTransform has methods for transforming distances, points, and entire shapes.   It can also compute inverse transforms is needed (useful for mapping mouse events).   Graphics2D, as we saw earlier, has methods for getting and setting the transform.   It also has a method for transforming by an instance of AffineTransform.  In the next sections, we'll see examples of these operations.

Affine Transform Lesson
1. Introduction
2. Ordering
3. Ordering, Part II
4. Try It
5. Graphics2D
6. AffineTransform
7. Wiggly Text
8. Code Examples
9. Links & Credits
Back ] Next ]

 

Back ] Next ]
 
pixel-008000.gif (807 bytes)
Copyright 1999
Glyphic Technology