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.

«interface»<br />

java.awt.Shape<br />

java.awt.geom.Path2D<br />

+lineTo(x: double, y: double): void<br />

+moveTo(x: double, y: double): void<br />

+quadTo(x1: double, y1: double, x2:<br />

double, y2: double): void<br />

+curveTo(x1: double, y1: double, x2:<br />

double, y2: double): void<br />

+closePath(): void<br />

+setWindingRule(rule: int): void<br />

+getWindingRu le(): int<br />

Figure 49.21<br />

Draws a line from the current position <strong>to</strong> the specified p oint (x, y).<br />

Moves <strong>to</strong> the specified point (x, y).<br />

Draws a quadratic curve from the current point <strong>to</strong> (x2, y2) using (x1, y1)<br />

as a quadratic parametric control point.<br />

Draws a cub ic curve from the current point <strong>to</strong> (x3, y3) using (x1, y1) and<br />

(x2, y2) as control points.<br />

Draws a line <strong>to</strong> connect the current point with the point in the last<br />

moveTo method.<br />

Sets a new winding rule.<br />

Gets the winding rule.<br />

The Path2D class contains the methods for constructing path segments.<br />

You may create a Path2D using a construc<strong>to</strong>r from Path2D.Double and<br />

Path2D.Float. The process of the path construction can be viewed as<br />

drawing with a pen. At any moment, the pen has a current position. You<br />

can use the moveTo(x, y) method <strong>to</strong> move the pen <strong>to</strong> the new position at<br />

point (x, y), use the lineTo(x, y) <strong>to</strong> add a point (x, y) <strong>to</strong> the path by<br />

drawing a straight line from the current point <strong>to</strong> this new point, use<br />

the quadTo(ctrlx, ctrly, x, y) method <strong>to</strong> draw a quadratic curve from<br />

the current location <strong>to</strong> (x, y) using (ctrlx, ctrly) as the control<br />

point, use the curveTo(ctrlx1, ctrly1, ctrlx2, ctrly2, x, y) method <strong>to</strong><br />

draw a cubic curve from the current location <strong>to</strong> (x, y) using (ctrlx1,<br />

ctrly1) and (ctrlx2, ctrly2) as the control points, and use the<br />

closePath() method <strong>to</strong> connect the current point with the point in the<br />

last moveTo method.<br />

Listing 49.8 gives a program that demonstrates how <strong>to</strong> draw a shape<br />

using Path2D. Figure 49.22 shows a sample run of the program.<br />

Listing 49.8 Path2DDemo.java<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

import java.awt.*;<br />

import java.awt.geom.*;<br />

import javax.swing.*;<br />

public class Path2DDemo extends JApplet {<br />

public Path2DDemo() {<br />

add(new ShapePanel());<br />

}<br />

22

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

Saved successfully!

Ooh no, something went wrong!