Java Reference
In-Depth Information
The QuadCurve2D and CubicCurve2D classes don't really suit our purpose here. A curve is going to
be represented by a series of connected line segments, but we don't know ahead of time how many
there are going to be - as long as the mouse is being dragged we'll collect more points. This gives us a
hint as to the approach we could adopt for creating a curve.
This looks like a job for a GeneralPath object. It can handle any number of segments and we can add
to it. If we construct an initial curve as soon as we have two points - which is when we receive the first
MOUSE _ DRAGGED event - we can extend the curve by calling the modify() method to add another
segment for each of the subsequent MOUSE _ DRAGGED events.
Try It Out - The Element.Curve Class
This means that the outline of the Curve class is going to be:
class Element {
// Code defining the base class...
// Nested class defining a line...
// Nested class defining a rectangle...
// Nested class defining a circle...
// Nested class defining a curve
public static class Curve extends Element {
public Curve(Point start, Point next, Color color) {
super(color);
curve = new GeneralPath();
curve.moveTo(start.x, start.y);
curve.lineTo(next.x, next.y);
}
// Add another segment
public void modify(Point start, Point next) {
curve.lineTo(next.x,
next.y);
Search WWH ::




Custom Search