Transforms

The AffineTransform class can transform a shape (or coordinate system) by translating, scaling, rotating, or shearing it, using any combination of these individual transformation types. Figure 12-5 illustrates the results of various types of coordinate-system transformations on the appearance of one shape, drawn multiple times. Example 12-7 shows the Java code that generates the figure.

Transformed shapes

Figure 12-5. Transformed shapes

An affine transformation is a linear transformation that has two important properties: all straight lines remain straight, and all parallel lines remain parallel. The AffineTransform class can perform a great variety of transformations, but it can’t produce nonlinear effects, such as distorting a figure as through a fisheye lens. That kind of effect can be achieved only with image-processing techniques. For a further discussion of transformations and the linear algebra behind the AffineTransform class, see Chapter 4 of Java Foundation Classes in a Nutshell.

Example 12-7. Transforms.java

package je3.graphics; import java.awt.*; import java.awt.geom.*; /** A demonstration of Java2D transformations */ public class Transforms implements GraphicsExample { public String getName( ) { return "Transforms"; } // From GraphicsExample public int getWidth( ) { return 750; } // From GraphicsExample public int getHeight( ) { return 250; } // From GraphicsExample Shape shape; ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.