|Contents | Previous | Next|
The Java 2D™ API provides several classes
that define common geometric objects, such as points, lines,
curves, and rectangles. These new geometry classes are part of the
java.awt.geom package. For backward
compatibility, the geometry classes that existed in previous
versions of the JDK software, such as
Polygon, remain in the
The Java 2D API geometries such as
Rectangle2D implement the
Shape interface defined in
a common protocol for describing and inspecting geometric path
objects. A new interface,
defines methods for retrieving elements from a geometry.
Using the geometry classes, you can easily define and manipulate virtually any two-dimensional object.
The following tables list the key geometry
interfaces and classes. Most of these interfaces and classes are
part of the
java.awt.geom package. Some,
Shape, are part of the
java.awt package, primarily to maintain backward
compatibility with earlier versions of the JDK software.
Defines methods for retrieving elements from a path.
Provides a common set of methods for describing and inspecting geometric path objects. Implemented by
Represents an arc defined by a bounding rectangle, start angle, angular extent, and a closure type. Implemented to specify arcs in float and double precision:
Represents an area geometry that supports boolean operations.
Represents a cubic parametric curve segment in (w) coordinate space. Implemented to specify cubic curves in float and double precision:
Encapsulates a width and height dimension. Abstract superclass for all objects that store a 2D dimension.
Represents an ellipse defined by a bounding rectangle. Implemented to specify ellipses in float and double precision:
Returns a flattened view of a
Can be used to provide flattening behavior for
Represents a geometric path constructed from lines and quadratic and cubic curves.
Represents a line segment in (x, y) coordinate space. Implemented to specify lines in float and double precision:
A point representing a location in (x,y) coordinate space. Implemented to specify points in float and double precision:
Represents a quadratic parametric curve segment in (x, y) coordinate space. Implemented to specify quadratic curves in float and double precision:
Represents a rectangle defined by a location (x, y) and dimension (w x h). Implemented to specify rectangles in float and double precision:
Provides common manipulation routines for operating on shapes that have rectangular bounds.
Represents a rectangle with rounded corners defined by a location (x, y), a dimension (w x h), and the width and height of the corner arc. Implemented to specify round rectangles in float and double precision:
Shape is an instance of any class that implements
Shape interface, such as
Shape’s contour (outline) is referred to as
Shape is drawn,
the pen style defined by the
object in the
Graphics2D context is
applied to the
Shape’s path. When
Shape is filled, the
Paint in the
context is applied to the area within its path. For more
information, see “Rendering
with Graphics2D” on page 15.
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 32.
GeneralPath is a
shape that can be used to represent any two-dimensional object that
can be constructed from lines and quadratic or cubic curves. For
additional implementations of the
interface that represent common geometric objects such as
rectangles, ellipses, arcs, and curves. The Java2D™ API also
provides a special type of shape that supports constructive area
Constructive Area Geometry (CAG) is the process of
creating new geometric objects by performing boolean operations on
existing objects. In the Java 2D API, a special type of
Shape called an
Area supports boolean operations. You can construct
Area from any
Areas support the
following Boolean operations:
These operations are illustrated in Figure 3-1.
A bounding box is a rectangle that fully encloses a shape’s geometry. Bounding boxes are used to determine whether or not an object has been selected or “hit” by the user.
defines two methods for retrieving a shape’s bounding box,
getBounds2D method returns a
Rectangle2D instead of a
Rectangle, providing a higher-precision description
of the shape’s bounding box.
Shape also provides
methods for determining whether or not:
Areas can be used to
quickly construct complex
simple shapes such as circles and squares. To create a new complex
Shape by combining
Shapes, construct the
Areasto be combined.
For example, CAG could be used to create a pear
like that shown in
The body of the pear is constructed by performing
a union operation on two overlapping
Areas: a circle and an oval. The leaves are each
created by performing an intersection on two overlapping circles
and then joined into a single
through a union operation. Overlapping circles are also used to
construct the stem through two subtraction operations.
You can implement the
Shape interface to create a class that defines a new
type of shape. It doesn’t matter how you represent the shape
internally, as long as you can implement the
Shape interface methods. The
Shape must be able to generate a path that specifies
For example, you could create a simple
Shape that represents
polygons as arrays of points. Once the polygon is built, it could
be passed to
setClip, or any other method that expects a
Shape object as an argument.
must implement the
|Contents | Previous | Next|