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.

where (x1, y1) and (x2, y2) specify two endpoints and (ctrlx, ctrly) is<br />

a control point. The control point is usually not on the curve instead<br />

of defining the trend of the curve, as shown in Figure 49.19a.<br />

(ctrlx, ctrly)<br />

(ctrlx1, ctrly1)<br />

(x1, y1)<br />

(x2, y2)<br />

(x1, y1) (x2, y2)<br />

(ctrlx2, ctrly2)<br />

(a)<br />

(b)<br />

Figure 49.19<br />

(a) A quadratic curve is specified using three points. (b) A cubic<br />

curve is specified using four points.<br />

A cubic curve is mathematically defined as a cubic polynomial. To<br />

create a CubicCurve2D.Double, use the following construc<strong>to</strong>r:<br />

CubicCurve2D.Double(double x1, double y1, double ctrlx1,<br />

double ctrly1, double ctrlx2, double ctrly2, double x2, double y2)<br />

where (x1, y1) and (x2, y2) specify two endpoints and (ctrlx1, ctrly1)<br />

and (ctrlx2, ctrly2) are two control points. The control points are<br />

usually not on the curve instead of defining the trend of the curve, as<br />

shown in Figure 49.19(b).<br />

Listing 49.7 gives a program that demonstrates how <strong>to</strong> draw quadratic<br />

curves and cubic curves. Figure 49.20 shows a sample run of the<br />

program.<br />

Listing 49.7 CurveDemo.java<br />

<br />

<br />

<br />

<br />

<br />

<br />

import java.awt.*;<br />

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

import javax.swing.*;<br />

public class CurveDemo extends JApplet {<br />

public CurveDemo() {<br />

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

}<br />

static class CurvePanel extends JPanel {<br />

protected void paintComponent(Graphics g) {<br />

super.paintComponent(g);<br />

Graphics2D g2d = (Graphics2D)g;<br />

20

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

Saved successfully!

Ooh no, something went wrong!