19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The program translates the coordinate’s origin <strong>to</strong> (160, 0) in line 19,<br />

creates a new path (line 20), sets the path winding rule <strong>to</strong><br />

WIND_EVEN_ODD (line 21), and displays it in line 22.<br />

The program translates the coordinate’s origin <strong>to</strong> (160, 0) in line 24,<br />

creates a new path (line 25), sets the path winding rule <strong>to</strong><br />

WIND_NON_ZERO (line 26), and displays it in line 27.<br />

Note that if a path is unclosed, the fill method implicitly closes it<br />

and draws a filled path.<br />

49.10 Constructive Area Geometry<br />

Shapes can be <strong>com</strong>bined <strong>to</strong> create new shapes. This is known as<br />

constructive area geometry. <strong>Java</strong> 2D provides class Area <strong>to</strong><br />

perform constructive area geometry, as shown in Figure 49.25.<br />

«interface»<br />

java.awt.Shape<br />

java.awt.geom.Area<br />

+Area()<br />

+Area(s: Shape)<br />

+add(area: Area): void<br />

+subtract(area: Area): void<br />

+intersect(area: Area): void<br />

+exclusiveOr(area: Area): void<br />

Figure 49.25<br />

C reat es an em pty area.<br />

Creates an Area for the specified shape.<br />

Adds the shape in the specified area with the shape in this area.<br />

Subtracts the shap e in the specified area from the shape in this area.<br />

Sets the sh ape of this area <strong>to</strong> the intersection of its current shape with the<br />

shape in the specified area.<br />

Same as invoking this.add(area) and then invoking this.subtract(area).<br />

The Area class contains the methods for constructing new areas.<br />

Area implements Shape and provides the methods add, subtract,<br />

intersect, and exclusiveOr <strong>to</strong> perform set-theoretic operations<br />

union, difference, intersection, and symmetric difference. These<br />

operations perform on the shapes s<strong>to</strong>red in the areas. The union<br />

of two areas consists of all points that are in either area. The<br />

difference of two areas consists of the points that are in the<br />

first area, but not in the second area. The intersection of two<br />

areas consists of all points that are in both areas. The<br />

symmetric difference consists of the points that are in exactly<br />

one of the two areas.<br />

Listing 49.10 gives a program that demonstrates constructive geometry<br />

using the Area class. Figure 49.26 shows a sample run of the program.<br />

Listing 49.10 AreaDemo.java<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

26

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!