Java Reference
In-Depth Information
// Draw tangents from the curve end points to the control marker centers
Line2D.Double tangent = new Line2D.Double(startQ, ctrlQuad.getCenter());
g2D.draw(tangent);
tangent = new Line2D.Double(endQ, ctrlQuad.getCenter());
g2D.draw(tangent);
tangent = new Line2D.Double(startC, ctrlCubic1.getCenter());
g2D.draw(tangent);
tangent = new Line2D.Double(endC, ctrlCubic2.getCenter());
g2D.draw(tangent);
}
If you recompile the applet with these changes,
when you execute it again you should see the
window shown here.
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. We 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 we call the
draw()
method for each of the
Marker
objects to draw a red circle
around each control point. The tangents are just lines from the endpoints 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 we 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
We will 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 will stop the process for that control point,
so you will then be free to manipulate another one. To do this we will add another inner class to
CurveApplet
that will handle mouse events: