Java Reference
In-Depth Information
form objects is to call a
static
member of the
AffineTransform
class. There are nine static methods, each
of which returns an
AffineTransform
object containing the transform that has the effect described:
•
getTranslateInstance(double deltaX, double deltaY)
: Translates the origin to the point
(
deltaX
,
deltaY
).
•
getRotateInstance(double angle)
: Rotates the coordinate system about the origin through
angle
radians.
•
getRotateInstance(double angle, double pX, double pY)
: Rotates the coordinate system
about the point (
pX
,
pY
) through
angle
radians.
•
getRotateInstance(double vectorX, double vectorY)
: Rotates the coordinate system about
the origin so that the point (
vectorX
,
vectorY
) lies on the x-axis.
•
getRotateInstance(double vectorX, double vectorY, pX, pY)
: Rotates the coordinate
system about the point (
pX
,
pY
) so that the x-axis is parallel to a line from the origin to the point
(
vectorX
,
vectorY
) lies on the x-axis.
•
getQuadrantRotateInstance(int nQuadrants)
: Rotates the coordinate system about the ori-
gin through
nQuadrants
quadrants, where a quadrant is π/2 radians, which is 90 degrees. A pos-
itive argument specifies a clockwise rotation and a negative argument specifies an anticlockwise
rotation.
•
getQuadrantRotateInstance(int nQuadrants, double pX, double pY)
: This is similar to
the previous transform except that the rotation is about the point (
pX
,
pY
).
•
getScaleInstance(double scaleX, double scaleY)
: Scales the coordinate system by
scaleX
for x-coordinates and
scaleY
for y-coordinates.
•
getShearInstance(double shearX, double shearY)
: Shears the coordinate system so that
each
x
coordinate becomes
(x + shearX*y)
and each
y
coordinate becomes
(y + shearY*x).
For example, to create a transform to rotate the user space clockwise by 90 degrees, you could write the
following:
AffineTransform at = AffineTransform.getRotateInstance(Math.PI/2);
Alternatively you could write:
AffineTransform at = AffineTransform.getQuadrantRotateInstance(1);
An argument of −1 would rotate the coordinate system counterclockwise by 90 degrees.
After you have created an
AffineTransform
object, you can apply it to a graphics context by passing it
as an argument to the
setTransform()
method.
Transforming Shapes
An
AffineTransform
object has another use, too: You can use it to transform a
Shape
object. The
cre-
ateTransformedShape()
method for the
AffineTransform
object does this. Suppose you define a
Rect-
angle
object with the following statement:
Rectangle rect = new Rectangle(10, 10, 100, 50);
You now have a rectangle that is 100 wide by 50 high, at position (10,10). You can create a transform
object with the statement: