CONTENTS | PREV | NEXT | Java 2D API |
Using the classes in the java.awt.geom package, you can describe virtually any two-dimensional object. Geometric objects are represented by an instance of a class that implements the Shape interface.
The GeneralPath class implements the Shape interface and can be used to represent any geometric object that can be constructed from lines and quadratic or cubic curves. For convenience, java.awt.geom also provides implementations of the Shape interface that represent common geometric objects such as rectangles, ellipses, arcs, and curves.A Shape's contour (outline) is referred to as its path. When a shape is drawn, it's path is rendered using the pen style defined by the Stroke object in the Graphics2D context. (For more information, see "Stroking Shapes" on page 19.) When a shape is filled, the area within its path is rendered using the paint in the Graphics2D context
A shape's path can be also used to define a clipping path. A clipping path determines what pixels are rendered--only those pixels that lie within the area defined by the clipping path are rendered. The clipping path is part of the Graphics2D context. For more information, see "Setting the Clipping Path" on page 31.
A bounding box is a rectangle that fully encloses the geometry of a Shape. You often use the bounds of a Shape during hit testing.The Shape interface defines two methods for retrieving the bounds, getBounds and getBounds2D.
In addition, Shape provides methods for determining whether or not:
Constructive Area Geometry (CAG) is the process of creating new geometric objects by performing boolean operations on existing objects. In the Java2D API, a special type of Shape called an Area supports boolean operations. You can construct an Area from any Shape.Areas support the following Boolean operations:
The Java 2D API provides one transform class, AffineTransform. AffineTransform supports operations such as translation, scaling, rotation, and shearing.An affine transformation performs a linear transformation on a set of graphics primitives. It always transforms straight lines into straight lines and parallel lines into parallel lines, but it might alter the distance between points and the angles between non-parallel lines. An affine transformation is based on a two-dimensional matrix of the following form:
To use the AffineTransform class, however, you do not need to interact directly with transformation matrices. AffineTransform provides a set of convenience methods for constructing AffineTransform objects that perform standard transformations:
You can modify the transform in the graphics context by calling the Graphics2D transformation methods: rotate, scale, shear, translate. The transform in the Graphics2D context transforms all of the shapes, text, and images you draw from User Space to Device Space.Graphics2D also implements a version of drawImage that takes an instance of AffineTransform as a parameter. When you use this version of drawImage, the image is drawn as if you had concatenated the transform to the Graphics2D object. This enables you to apply any transformation, such as a rotation, to an image object when it is drawn.
To concatenate additional transform objects to the current Graphics2D transform, call Graphics2D.transform. When you concatenate a transform, it becomes part of the pipeline of transformations applied during rendering. The last transform specified is the first to be applied.
You can also apply transforms to Font objects to create new font derivations, as discussed in "Creating Font Derivations" on page 62.