Java Reference
In-Depth Information
Figure 6.9
The
TransformsPanel
demonstrates the effects of the affine transforms
by performing them on a rectangle shape like that shown on the far left. A
translation operation shifts the drawing coordinates further to the right for each
example. The shape second from the left is created by a
rotation
transform on the
rectangle, the third a
scale
transform, the fourth a
shear
, and the shape on the far
right is created with a compound transform of a shear and then a rotation.
is turned on (for good practice, but not crucial for the straight edged shapes
displayed here). A
Rectangle2D.Double
object is created and drawn. Then a
set of transforms are carried out, each time creating a new
Shape
object (therefore
the original rectangle is not altered). Figure 6.9 shows the display of this panel
when it is added to a
JApplet
.
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
Search WWH ::
Custom Search