Java Reference
In-Depth Information
An object of each curve type defines a curve segment between two points. The control points - one for
a QuadCurve2D curve and two for a CubicCurve2D curve - control the direction and magnitude of
the tangents at the end points. A QuadCurve2D curve constructor has six parameters corresponding to
the coordinates of the starting point for the segment, the coordinates of the control point and the
coordinates of the end point. We can define a QuadCurve2D curve from a point start to a point end ,
plus a control point, control , with the statements:
Point2D.Double startQ = new Point2D.Double(50, 150);
Point2D.Double endQ = new Point2D.Double(150, 150);
Point2D.Double control = new Point2D.Double(80,100);
QuadCurve2D.Double quadCurve
= new QuadCurve2D.Double(startQ.x, startQ.y, // Segment start point
control.x, control.y, // Control point
endQ.x, endQ.y); // Segment end point
The QuadCurve2D subclasses have public members storing the end points and the control point so you
can access them directly. The coordinates of the start and end points are stored in the fields, x1 , y1 , x2 ,
and y2 . The coordinates of the control point are stored in ctrlx and ctrly .
Defining a cubic curve segment is very similar - you just have two control points, one for each end of
the segment. The arguments are the ( x , y ) coordinates of the start point, the control point for the start of
the segment, the control point for the end of the segment and finally the end point. We could define a
cubic curve with the statements:
Point2D.Double startC = new Point2D.Double(50, 300);
Point2D.Double endC = new Point2D.Double(150, 300);
Point2D.Double controlStart = new Point2D.Double(80, 250);
Point2D.Double controlEnd = new Point2D.Double(160, 250);
CubicCurve2D.Double cubicCurve = new CubicCurve2D.Double(
startC.x, startC.y, // Segment start point
controlStart.x, controlStart.y, // Control point for start
controlEnd.x, controlEnd.y, // Control point for end
endC.x, endC.y); // Segment end point
The cubic curve classes also have public members for all the points: x1 , y1 , x2 and y2 for the end
points, and ctrlx1 , ctrly1 , ctrlx2 and ctrly2 for the corresponding control points.
We can understand these better if we try them out. This time let's do it with an applet.
Try It Out - Drawing Curves
We can define an applet to display the curves we used as examples above:
import javax.swing.JApplet;
import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Graphics2D;
Search WWH ::




Custom Search