Java Reference
In-Depth Information
The AffineTransform Class
In Java, the AffineTransform class in the java.awt.geom package represents an affine
transformation. Every Graphics2D graphics context has one. The default AffineTransform object
in a graphics context is the identity transform , which leaves user coordinates unchanged. It is applied to
the user coordinate system anyway for everything you draw, but all the coordinates are unaltered by
default. You can retrieve a copy of the current transform for a graphics context object by calling its
getTransform() method. For example:
AffineTransform at = g2D.getTransform(); // Get current transform
While this retrieves a copy of the current transform for a graphics context, you can also set it with
another transform object:
g2D.setTransform(at);
You can retrieve the transform currently in effect with getTransform() , set it to some other
operation before you draw some shapes, and then restore the original transform later with
setTransform() when you're finished. The fact that getTransform() returns a reference to a copy
rather than a reference to the original transform object is important. It means you can alter the existing
transform and then restore the copy later.
Although the default transform object for a graphics context leaves everything unchanged, you could set
it to do something by calling one of its member functions. All of these have a return type of void so
none of them return anything:
Transform Default
Description
setToTranslation
(double deltaX,
double deltaY)
This method makes the transform a translation of deltaX in x and
deltaY in y . This replaces whatever the previous transform was for
the graphics context. You could apply this to the transform for a
graphics context with the statements:
// Save current transform and set a new one
AffineTransform at = g2D.getTransform();
at.setToTranslation(5.0, 10.0);
The effect of the new transform will be to shift everything that is
drawn in the graphics context, g2D , 5.0 to the right, and down by
10.0. This will apply to everything that is drawn in g2D subsequent
to the statement that sets the new transform.
Search WWH ::




Custom Search