AffineTransform : Java Glossary

AffineTransform
java.awt.geom.AffineTransform is a class that represents a PostScript-like coordinate 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 coordinate to a new x',y' coordinate.
[ 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

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 coordinates and the drawing will appear in transformed coordinates. Note that with Graphics2D, your coordinates are doubles not ints.

Correcting Mouse Co-ordinates

The catch is, the mouse knows nothing about your transforms. You need to convert mouse screen coordinates to your user-coordinates. This is the inverse of what you normally do, namely converting convenient user coordinates to pixel display coordinates. 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

Oracle’s Technote Guide on 2D : available:
Oracle’s Javadoc on AffineTransform class : available:
Oracle’s Javadoc on Graphics2D class : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/affinetransform.html J:\mindprod\jgloss\affinetransform.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.213]
You are visitor number 39,250.