CONTENTS | PREV | NEXT Java 2D API


3.4 Transforming Shapes

To transform a Shape, text string, or Image you add a new AffineTransform to the transformation pipeline in the Graphics2D context before rendering. The transformation is applied when the graphic object is rendered.

For example, to draw a rectangle that is rotated 45 degrees:

  1. Get a rotation transform by calling AffineTransform. getRotateInstance.
  2. Call Graphics2D.setTransform to add the new transform to the transformation pipeline.
  3. Create a Rectangle2D.Float object.
  4. Call Graphics2D.draw to render the rectangle.
Rectangle2D rect = new Rectangle2D.Float(1.0,1.0,2.0,3.0);
AffineTransform rotate45 =
AffineTransform.getRotateInstance(Math.PI/4.0,0.0,0.0)
g2.setTransform(rotate45);
g2.draw(rect);


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