AffineTransform : Java Glossary
home A words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
AffineTransform
java.awt.geom.AffineTransform is a class that represents a PostScript-like co-ordinate transformation i.e. a 3x3 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 Rotating Images
Manual Use Learning More
Drawing Links
Mouse Co-ordinates

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: 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 Guide : available:
Sun’s Javadoc on the AffineTransform class : available:
Sun’s Javadoc on the Graphics2D class : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.18] Spread the Net
You are visitor number 15,838.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/affinetransform.html J:\mindprod\jgloss\affinetransform.html