| The example here demonstrates the use of AffineTransform, 
              which transforms a shape in a way that maintains the parallelism 
              of straight lines. The transformations include rotatation, scaling, 
              and shearing.  In the previous applet in Shapes & 
              Areas, we used a transform to translate the drawing context. 
              
              Note: transforms can be done either 
                directly on a given shape or on the graphics context itself. In 
                the latter case, all further drawings will be rendered according 
                to the new transformed coordinate system. The translation transform is done on the graphics context. The 
                other transforms are done on the shape objects to create new shapes 
                that are transformed versions of the originals. The demo applet below illustrates transforms for translation, rotation, 
              scaling, shearing and combinations of transforms. The applet loads 
              a TransformPanel 
              object, which carries out the transformations and drawings.  
               
                
                   
                    | TransformApplet.java
 |   
                    | import 
                      javax.swing.*; import java.awt.*;
 import java.awt.geom.*;
 
 /**  Demonstration of AffineTransform in Java2D.**/
 public class TransformApplet extends JApplet
 {
 public void init () {
 Container content_pane = getContentPane 
                      ();
 
 // Create an instance of DrawingPanel
 TransformPanel transform_panel =
 new TransformPanel 
                      ();
 
 // Add the DrawingPanel to the contentPane.
 content_pane.add (transform_panel);
 
 } //init
 
 } // class TransformApplet
 
 |   
                    | import 
                      javax.swing.*; import java.awt.*;
 import java.awt.geom.*;
 
 /**  Demonstrate different AffineTransforms of 
                      a rectangle.**/
 class TransformPanel extends JPanel {
 
 public void paintComponent (Graphics g) {
 // First paint background
 super.paintComponent (g);
 Graphics2D g2 =  (Graphics2D)g;
 
 // Turn on anti-aliasing.
 g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,
 RenderingHints.VALUE_ANTIALIAS_ON);
 
 // Create a rectangle.
 Shape shape1 =  new Rectangle2D.Double 
                      (20.0, 20.0, 30.0, 50.0);
 // Now draw it.
 g2.draw (shape1);
 
 // Shift the drawing origin 72 units  (1 
                      inch) to the right
 // to obtain room for the next drawing
 AffineTransform at = AffineTransform.getTranslateInstance 
                      (72,0);
 g2.transform (at);
 
 // Create a rotation transform of 
                      30 degrees CCW around the
 // the top left corner of the rectangle.
 AffineTransform  atx =
 AffineTransform.getRotateInstance 
                      (-Math.PI/6,30,20);
 // Take the shape object and create 
                      a rotated version
 Shape atShape = atx.createTransformedShape 
                      (shape1);
 g2.draw (atShape);
 
 // Another 72 unit shift.
 g2.transform (at);
 
 // Create a scaling transform
 atx = AffineTransform.getScaleInstance 
                      (1.5,1.5);
 // Take the shape object and create 
                      a scaled version
 atShape = atx.createTransformedShape 
                      (shape1);
 g2.draw (atShape);
 
 // Another 72 unit shift.
 g2.transform (at);
 
 // Create a shear transform
 atx = AffineTransform.getShearInstance 
                      (0.0,0.5);
 // Take the shape object and create 
                      a sheared version
 atShape = atx.createTransformedShape 
                      (shape1);
 g2.draw (atShape);
 
 // Another 72 unit shift.
 g2.transform (at);
 
 // Illustrate compound transforms
 // First get a transform object
 atx = new AffineTransform ();
 // Then set to a shear transform
 atx.setToShear (0.0,0.5);
 // and then rotate  about 
                      the current origin
 atx.rotate (-Math.PI/5,40,50);
 // Now apply to the rectangle
 atShape = atx.createTransformedShape 
                      (shape1);
 g2.draw (atShape);
 
 } // paintComponent
 } //class TransformPanel
 
 |  In the first four transforms, we used static methods in the 
                   AffineTransform 
                  class to obtain instances of the class for the particular type 
                  of transform desired. For example, a translation transform is 
                  obtained with
    AffineTransform 
                  at = AffineTransform.getTranslateInstance (72, 0);  Applying this to the graphics context moves its 
                  origin horizontally by 72 units:    g2.transform 
                  (at);  All subsequent coordinate values are relative 
                  to that new position on the drawing surface. Here the translation 
                  lets us draw the rectangle at a different position without changing 
                  its internal coordinate values. Then the rectangle is operated 
                  on with a rotation transform obtained with the getRotateInstance() 
                  static method:    AffineTransform 
                  atx =AffineTransform.getRotateInstance (-Math.PI/6, 
                  40, 50);
 
 Then a new rectangle in the rotated orientation is obtained 
                  with the createTransformedShape() 
                  method as follows:
    Shape 
                  atShape = atx.createTransformedShape (shape1);
 Finally, the new rotated rectangle is drawn with:
 g2.draw 
                  (atShape);
 
 This same process is repeated for scaling and shear transforms. 
                  Finally, a combined transform is created by creating an instance 
                  of AffineTransform 
                  and then its setToShear() 
                  and rotate() 
                  methods are applied to the original rectangle to obtain a new 
                  transformed one.
 
   Latest update: Oct. 27, 2004 |