Java Reference
In-Depth Information
Transform Default
Description
setToRotation
(double angle)
You call this method for a transform object to make it a rotation of
angle radians about the origin. This replaces the previous
transform. To rotate the axes 30 degrees clockwise, you could write:
g2D.getTransform().setToRotation(30*Math.PI/180);
This statement gets the current transform object for g2D and sets it
to be the rotation specified by the expression 30*Math.PI/180 .
Since
radians is 180 degrees, this expression produces the
equivalent to 30 degrees in radians.
π
setToRotation
(double angle,
double deltaX,
double deltaY)
This method defines a rotation of angle radians about the point
deltaX,deltaY . It is equivalent to three successive transform
operations - a translation by deltaX, deltaY , then a rotation
through angle radians about the new position of the origin and then
a translation back by -deltaX,-deltaY to restore the previous
origin point.
You could use this to draw a shape rotated about the shape's
reference point. For example, if the reference point for a shape was
at shapeX,shapeY , you could draw the shape rotated through
π
/3
radians with the following:
g2D.getTransform().setToRotation(Math.PI/3,
shapeX, shapeY);
// Draw the shape...
The coordinate system has been rotated about the point
shapeX,shapeY , and will remain so until you change the
transformation in effect. You would probably want to restore the
original transform after drawing the shape rotated.
setToScale
(double scaleX,
double scaleY)
This method sets the transform object to scale the x coordinates by
scaleX , and the y coordinates by scaleY . To draw everything half
scale you could set the transformation with the statement:
g2D.getTransform().setToScale(0.5, 0.5);
setToShear
(double shearX,
double shearY)
The x coordinates are converted to x+shearX*y , and the y
coordinates are converted to y+shearY*x .
All of these methods that we have discussed here replace the transform in an AffineTransform
object. We can modify the existing transform object in a graphics context, too.
Search WWH ::




Custom Search