Java Reference
In-Depth Information
If you recompile the applet with these changes and run
it again you should get something like the window
here.
You should be able to drag the control points around with the mouse. If it is a bit difficult to select the
control points, just make the value of
radius
a bit larger. Note how the angle of the tangent as well as
its length affects the shape of the curve.
How It Works
In the
MouseHandler
class, the
mousePressed()
method will be called when you press a mouse
button. In this method we check whether the current cursor position is within any of the markers
enclosing the control points. We do this by calling the
contains()
method for each marker object and
passing the coordinates of the cursor position to it. The
getX()
and
getY()
methods for the
MouseEvent
object supply the coordinates of the current cursor position. If one of the markers does
enclose the cursor, we store a reference to the
Marker
object in the
selected
member of the
MouseHandler
class for use by the
mouseDragged()
method.
In the
mouseDragged()
method, we set the location for the
Marker
object referenced by
selected
to the current cursor position and call
repaint()
for the
pane
object. The
repaint()
method causes
the
paint()
method to be called for the component, so everything will be redrawn, taking account of
the modified control point position.
Releasing the mouse button will cause the
mouseReleased()
method to be called. In here we just set
the
selected
field back to
null
so no
Marker
object is selected. Remarkably easy, wasn't it?
Complex Paths
You can define a more complex shape as an object of type
GeneralPath
. A
GeneralPath
object can
be a composite of lines,
Quad2D
curves, and
Cubic2D
curves, or even other
GeneralPath
objects.
The process for determining whether a point is inside or outside a
GeneralPath
object is specified by
the
winding rule
for the object. There are two winding rules that you can specify by constants defined
in the class: