java.awt.geom.AffineTransform is a class that represents a PostScript-like co-ordinate
transformation i.e. a 3 × 3 matrix multiplication. It lets you rotate around
an anchor point, translate the axes and shear (change the x and y scaling). Using negative scale factors, you can
flip (reflect around the x or y axis). The transform converts an x,y co-ordinate to a new x',y'
co-ordinate.
[ x'] [ m00 m01 m02 ] [ x ] [ m00 * x + m01 * y + m02 ]
[ y'] = [ m10 m11 m12 ] [ y ] = [ m10 * x + m11 * y + m12 ]
[ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]
Though AffineTransform is often associated with drawing, it is not tied in any way to
the Graphics class. It is purely mathematical.
Tips
Watch out: rotate is cumulative but setToRotation is not.
Don’t forget to call Graphics2.setTransform after every change to your
AffineTransform. It pays to learn how to use Graphics2D and
AffineTransform. It is much simpler to draw something simple on a simple grid and
transform it than it is to compute all the transformed points on the natural grid yourself. Angles are in radians
are points are in the current transformed user co-ordinate system. Best to just experiment drawing lines on a
Canvas till you get the hang of it before tackling your real project.
Graphics.drawImage
will do a simple scale for you, often inadvertently. You don’t need an AffineTransform for that.
Manual Use of AffineTransform
The basic idea is this. A transform maps every point x,y onto a new point, by a combination of rotation,
scaling, translation and mirroring. You can manually transform a point like this:
Drawing with AffineTransform
You can also simply plug an AffineTransform into your Graphics2D object. Then you can specify simple co-ordinates and the drawing will appear in
transformed co-ordinates. Note that with Graphics2D, your co-ordinates are
doubles not ints.
Correcting Mouse Co-ordinates
The catch is, the mouse knows nothing about your transforms. You need to convert mouse screen co-ordinates to
your user-co-ordinates. This is the inverse of what you normally do, namely converting convenient user
co-ordinates to pixel display co-ordinates. For this, you need the inverse of your transform.
You can learn more with a Google
search.
Rotating Images
Rotating Images is a tad trickier than you might expect. The code snippet below will show you how it is done.
The tricks:
- AffineTransform has no flip method to turn an
Image upside down. Use scale( 1.0, -1.0 ) instead.
- AffineTransform has no mirror method to reverse an
image left to right. Use scale( -1.0,
1.0 ) instead.
- If you are rotating by multiples of 90 degrees, you will get faster and more accurate results if you use
quadrantRotate().
- If you scale or rotate, the Image will spin around the upper left corner of the
Image and disappear off the screen. You must first move the origin to the center of
the image, do your rotation, then move it back.
- Try writing yourself a training program that just draws coloured rectangles. Experiment with various
combinations of transforms until you see the effects, stage by stage.
Rotating is trickier than you might suppose. Run this program and follow through the steps to make sure you can
follow what happens at each stage.
Learning More
Sun’s JDK Technote Guide on
2D : available:
Sun’s Javadoc on
AffineTransform class : available:
Sun’s Javadoc on
Graphics2D class : available: