CONTENTS | PREV | NEXT Java 2D API


4.5 Transforming Text

You can transform text the same way that you transform any other graphic object, by applying a transform to the Graphics2D before rendering the text. Transforms can also be applied to a Font, for more information see "Creating Font Derivations" on page 62.

In the following code excerpt, a text string is rotated several times around a center point using an AffineTransform.

// Define the rendering transform
AffineTransform at = new AffineTransform();
// Apply a translation transform to make room for the
// rotated text.
at.setToTranslation(400.0, 400.0);
g2.transform(at);
// Create a rotation transform to rotate the text
at.setToRotation(Math.PI / 2.0);
// Create a StyledString object, specifying the text and
// font.
StyledString ss = new StyledString("Java", Helvetica);
// Render four copies of the string at 90 degree angles
for (int i = 0; i < 4; i++) {
g2.drawString(ss, 0.0f, 0.0f);
g2.transform(at);
}


CONTENTS | PREV | NEXT
Copyright © 1997-1998 Sun Microsystems, Inc. All Rights Reserved.