FileDocCategorySizeDatePackage
Transforms.javaAPI DocExample3547Sat Jan 24 10:44:36 GMT 2004je3.graphics

# Transforms

public class Transforms extends Object implements GraphicsExample
 A demonstration of Java2D transformations

Fields Summary
Shape
shape
AffineTransform[]
transforms
String[]
transformLabels
Constructors Summary
public Transforms()
This constructor sets up the Shape and AffineTransform objects we need

``````	GeneralPath path = new GeneralPath();  	// Create a shape to draw
path.append(new Line2D.Float(0.0f, 0.0f, 0.0f, 100.0f), false);
path.append(new Line2D.Float(-10.0f, 50.0f, 10.0f, 50.0f), false);
path.append(new Polygon(new int[] { -5, 0, 5 },
new int[] { 5, 0, 5 }, 3), false);
this.shape = path;  // Remember this shape

// Set up some transforms to alter the shape
this.transforms = new AffineTransform[6];
// 1) the identity transform
transforms[0] = new AffineTransform();
// 2) A scale tranform: 3/4 size
transforms[1] = AffineTransform.getScaleInstance(0.75, 0.75);
// 3) A shearing transform
transforms[2] = AffineTransform.getShearInstance(-0.4, 0.0);
// 4) A 30 degree clockwise rotation about the origin of the shape
transforms[3] = AffineTransform.getRotateInstance(Math.PI*2/12);
// 5) A 180 degree rotation about the midpoint of the shape
transforms[4] = AffineTransform.getRotateInstance(Math.PI, 0.0, 50.0);
// 6) A combination transform
transforms[5] = AffineTransform.getScaleInstance(0.5, 1.5);
transforms[5].shear(0.0, 0.4);
transforms[5].rotate(Math.PI/2, 0.0, 50.0);  // 90 degrees

// Define names for the transforms
transformLabels = new String[] {
"identity", "scale", "shear", "rotate", "rotate", "combo"
};
``````
Methods Summary
public voiddraw(java.awt.Graphics2D g, java.awt.Component c)
Draw the defined shape and label, using each transform

``````	// Define basic drawing attributes
g.setColor(Color.black);                                   // black
g.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_SQUARE,  // 2-pixel
BasicStroke.JOIN_BEVEL));
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,        // antialias
RenderingHints.VALUE_ANTIALIAS_ON);

// Now draw the shape once using each of the transforms we've defined
for(int i = 0; i < transforms.length; i++) {
AffineTransform save = g.getTransform();    // save current state
g.translate(i*125 + 50, 50);                // move origin
g.transform(transforms[i]);                 // apply transform
g.draw(shape);                              // draw shape
g.drawString(transformLabels[i], -25, 125); // draw label
g.drawRect(-40, -10, 80, 150);              // draw box
g.setTransform(save);                       // restore transform
}
``````
public intgetHeight()

`` return 250; ``
public java.lang.StringgetName()

`` return "Transforms"; ``
public intgetWidth()

`` return 750; ``