Java Glossary : AffineTransform

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 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

You are here : home : Java Glossary : A words : AffineTransform.

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         ]
    

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.

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:

...

// transform that shifts points 10 left and 20 "down"
AffineTransform transformer = AffineTransform.getTranslateInstance ( 10.0d ,- 20.0d );

// create point
Point2D before = new Point2D.Double( 3.0d , 6.0d );

// create point to hold result
Point2D after = new Point2D.Double();

// transform the point
after = transformer.transform ( before, after );

// prints 13.0,-14.0
System.out.println( after.getX() + "," + after.getY());

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.

public void paintComponent ( Graphics g )
   {
   super.paintComponent( g );

   // access extended 2D graphics methods.
   Graphics2D g2 = ( Graphics2D ) g;

   // save original transform
   AffineTransform origTransform = g.getTransform();

   // transform that shifts points 10 left and 20 "down"
   AffineTransform transformer = AffineTransform.getTranslateInstance ( 10.0d , -20.0d);

   // also apply a rotation.
   transformer.rotate ( Math.toRadians( 30 ) );

   // hook up our new transform
   g.setTransform ( transformer );

   // choose font
   g.setFont(new Font( "Sans" , Font.BOLD, 24 ));

   // draw specifying untransformed co-ordinates.
   g.drawString( "Hello World", 0.0d, 5.0d);

   // restore original transform
   g.setTransform( origTransform );

   }

Correcting Mouse Co-ordinates

The catch is, the mouse knows nothing about your transforms. You need to convert mouse co-ordinates back to the original. For this you need the inverse of your transform.

...

// transform that shifts points 10 left and 20 "down"
AffineTransform transformer = AffineTransform.getTranslateInstance ( 10.0d ,- 20.0d );

// create transform to undo the translation.
AffineTransform reverse = transformer.createInverse();

// manually untransform points as above with reverse.transform( before, after )

You can learn more with a Google search.

co-ordinates ¤ Java 2D ¤ point ¤ sample code to see how it is used


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 1442.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/affinetransform.html J:\mindprod\jgloss\affinetransform.html