This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 18: Class 18: Lab Lab Transformations in the 2D API Transformations in the 2D API 2 Affine Transformations • The 2D API provides strong support for affine transformations . – Affine means linear • An affine transformation maps 2D coordinates so that the straightness and parallelism of lines are preserved. • All affine transformations can be represented by a 3x3 floating point matrix. • There are a number of “primitive” affine transformations that can be combined: scaling, rotation, and translation. 1 3 Transformations in the 2D API • package. • its no argument constructor. • • – – – – • complex transformations. Transformations are represented by instances of the AffineTransform class in the java.awt.geom You can create a new AffineTransform object with AffineTransform at = new AffineTransform(); You can invoke any of the following methods on an AffineTransform object: at.scale(double sx, double sy) at.translate(double tx, double ty) at.rotate(double theta) at.rotate(double theta, double x, double y) These methods can be combined to build 4 Translation t y t x 1 1 1 x x y y + ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ = + ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ 1 1 t x x t t y y t 2 5 Translation Example, 1 } } } 6 Applying Transforms • Shapes baseXf.translate( 50, 50 ); Shape transformed = g2.fill(transformed); • g2.transform(baseXf); g2.fill(rect); import javax.swing.*; import java.awt.*; import java.awt.geom.*; public class TransformPanel extends JPanel { Rectangle2D.Double rect; public TransformPanel() { rect = new Rectangle2D.Double(0, 0, 50, 100); public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setPaint(Color.BLUE); AffineTransform baseXf = new AffineTransform(); // Shift to the right 50 pixels, down 50 pixels baseXf.translate(50,50); Shape s = baseXf.createTransformedShape( rect ); g2.fill(rect); You can apply a transformation to one or more and draw the result: AffineTransform baseXf = new AffineTransform(); rect = new Rectangle2D.Double(0, 0, 50, 100); baseXf.createTransformedShape( rect ); Or you can transform the coordinate space and everything in it: 3 7...
View
Full
Document
This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.
 Spring '05
 GeorgeKocur

Click to edit the document details