Java Reference
In-Depth Information
Directory "CurveApplet 2 displaying control points"
If you recompile the applet with these changes, when you execute it again you should see the window
shown in
Figure 19-14
.
How It Works
In the
Marker
class constructor, the top-left corner of the rectangle enclosing the circle for a control point
is obtained by subtracting the radius from the
x
and
y
coordinates of the control point. You then create
an
Ellipse2D.Double
object with the width and height as twice the value of
radius
— which is the
diameter of the circle.
In the
paint()
method, you call the
draw()
method for each of the
Marker
objects to draw a red circle
around each control point. The tangents to the curves are just lines from the end points of each curve
segment to the centers of the corresponding
Marker
objects.
It would be good to see what happens to a curve segment when you move the control points around.
Then you could really see how the control points affect the shape of the curve. That's not as difficult to
implement as it might sound, so let's give it a try.
TRY IT OUT: Moving the Control Points
You arrange to allow a control point to be moved by positioning the cursor on it, pressing a mouse button,
and dragging it around. Releasing the mouse button stops the process for that control point, so the user
then is free to manipulate another control point. To implement this functionality in the applet you add
another inner class to
CurveApplet
that handles mouse events: