O'Reilly logo

Java 2D Graphics by Jonathan Knudsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.6. Constructive Area Geometry

Constructive area geometry is a fancy name for combining shapes. Two or more shapes can be combined using different rules or operations, much the way numbers can be combined in an equation. The 2D API supports four different operations for combining the areas of two shapes:

addition (union)

The addition of two shapes is the area covered by one or both of the shapes.

intersection

The intersection of two shapes is the area that is covered by both shapes simultaneously.

subtraction

The result of subtracting one shape from another is the area covered by one that is not covered by the other.

exclusive or

The exclusive or operator is the inverse of the intersection operator. In other words, the exclusive or of two shapes is the area that is covered by one or the other of the shapes. But it does not include the area covered by both.

It's hard to visualize these operators just by reading about them. Figure 3.21 shows the result of applying these operators to two overlapping shapes.

In Java 2D, the java.awt.geom.Area class supports constructive area geometry. This class offers two constructors:

public Area()

This constructor creates an empty Area. You can accumulate area using the add() method, described below.

public Area(Shape g)

This constructor creates an Area using the interior of the supplied Shape.

Figure 3.21. Different ways of combining two shapes

The four area operators are implemented as Area's methods:

public void add(Area rhs)

Use this ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required