Java Reference
In-Depth Information
You should be able to drag the control points around with the mouse and see the curves change shape.
If you find it's 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
The
mousePressed()
method in the
MouseHandler
class is called when you press a mouse button. In
this method you check whether the current cursor position is within any of the markers enclosing the
control points. You 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 sup-
ply the coordinates of the current cursor position. If one of the markers does enclose the cursor, you
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, you 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 is redrawn, taking account of the modi-
fied control point position.
Releasing the mouse button causes the
mouseReleased()
method to be called. In here you 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 geometric 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.
In general, closed shapes such as rectangles and ellipses can be filled with a color or pattern quite easily
because they consist of a closed path enclosing a region. In this case, whether a given point is inside or
outside a shape can be determined quite simply. With more complex shapes such as those defined by a
Gen-
eralPath
object, it can be more difficult. Such paths may be defined by an exterior bounding path that en-