Previous | Next | Trail Map | 2D Graphics | Overview of the Java 2D API

Shapes

The classes in the java.awt.geom package define common graphics primitives, such as points, lines, curves, arcs, rectangles, and ellipses:

Arc2D

Area

CubicCurve2D

Dimension2D

Ellipse2D

GeneralPath

Line2D

Point2D

QuadCurve2D

Rectangle2D

RectangularShape

RoundRectangle2D

Except for Point2D and Dimension2D , each of the geometry classes ( geometries ) implements the Shape interface, which provides a common set of methods for describing and inspecting two-dimensional geometric objects.

With these classes, you can create virtually any geometric shape and render it through Graphics2D by calling the draw or fill methods. For example, the geometric shapes in the following ShapesDemo2D applet are defined using basic Java 2D geometries:

If you're curious, the code for this program is in ShapesDemo2D.java. How to draw and fill shapes is described in detail in Displaying Graphics with Graphics2D.

Rectangular Shapes

The Rectangle2D , RoundRectangle2D , Arc2D , and Ellipse2D primitives are all derived from RectangularShape , which defines methods for Shape objects that can be described by a rectangular bounding box--the geometry of a RectangularShape can be extrapolated from a rectangle that completely encloses the outline of the Shape :

GeneralPath

The GeneralPath class enables you to construct an arbitrary shape by specifying a series of positions along the shape's boundary. These positions can be connected by line segments, quadratic curves, or cubic (Bézier) curves.

A quadratic curve can be defined by two endpoints and a single control point:

A cubic curve is defined by four points, two endpoints and two control points:

Areas

With the Area class, you can perform boolean operations such as union, intersection, and subtraction on any two Shape objects. This technique, often referred to as constructive area geometry, enables you to quickly create complex Shape objects without having to describe each line segment or curve.


Previous | Next | Trail Map | 2D Graphics | Overview of the Java 2D API