Java Reference
In-Depth Information
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.
Forexample, 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 create-
TransformedShape() 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 com-
bined 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.
6.9 Images
We introduce the basics of image handling here so you can begin to use them
in your programs. We return to images again in Chapter 11 with a much more
detailed discussion.
The base class for images is java.awt.Image .With Java 1.2 came the
more capable subclass java.awt.image.BufferedImage .Itworks with the
image processing tools of the Java 2D API and so we wait to discuss Buffered-
Image in Chapter 11.
As of Java 1.4 you can load and draw image files encoded as JPEG, GIF,
and PNG. With Java 5.0 you can also load bitmap formats BMP and WBMP. To
load an image into an applet, you can use one of the overloaded getImage()
methods in the Applet class for locating the file with a URL, as in
Image img = getImage ( " http://www.someschool.edu/anImage.gif " );
Search WWH ::




Custom Search