Java Reference
In-Depth Information
g2D.translate(position.x, position.y);
// Move context
origin
g2D.draw(curve);
// Draw the curve
g2D.translate(-position.x, -position.y);
// Move context
origin back
}
private GeneralPath curve;
private final static long serialVersionUID = 1001L;
}
// Abstract methods & fields in Element class...
}
Directory "Sketcher 6 drawing sketch curve elements"
The
Curve
class constructor creates a
GeneralPath
object and adds a single line segment to it by moving
the current point for the path to
start
by calling
moveTo()
and then calling the
lineTo()
method for
the
GeneralPath
object with
next
as the argument. You compute an initial bounding rectangle here that
encloses the first two points. As always, the height and width of the rectangle are increased by one to
ensure all points are enclosed.
Additional curve segments are added by the
modify()
method. This calls
lineTo()
for the
GeneralPath
member of the class with the new point,
next
, as the argument. This adds a line from the end of the last
segment that was added to the new point. The bounding rectangle is extended, if necessary, by adding a
1× 1 rectangle at
next
to bounds. Adding a 1× 1 rectangle, rather than just the
next
point, is necessary
to ensure that the new point lies within bounds and not on its boundary.
Of course, you need to uncomment the line creating an
Element.Curve
object in the
createElement()
method in the
MouseHandler
inner class to
SketcherFrame
. Then you're ready to roll again. If you re-
compile Sketcher you are able to give freehand curves a whirl and produce elegant sketches such as that
in
Figure 19-29
.