Academic Java    Java Tutorial  >  Graphics  >  AffineTransform

AffineTransform Example

Java AffineTransform EXAMPLE output AffineTransform is a Graphics2D class to construct transformations for translating (moving), scaling, flipping, rotating, and shearing. It is typically used in a paintComponent() or paint() method.

Here we first render an ellipse.

Then we create an AffineTransform that is a simple translation. This is then used to change the 'shape' of the same ellipse i.e. its position, prior to rendering it.
// AffineTransform translate Example
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class AffineTransformExample extends JApplet {

   @Override public void paint(Graphics g) {
      super.paint(g);
      Graphics2D g2 = (Graphics2D)g;

      g2.setStroke(new BasicStroke(10));

      Ellipse2D e = new Ellipse2D.Float(100,50,80,200);
      g2.draw(e);

      AffineTransform at = new AffineTransform();
      at.translate(50,20);
      g2.setColor(Color.cyan);
      g2.draw(at.createTransformedShape(e));

   }
}


* AffineTransform Examples