Java Reference
In-Depth Information
public Shape getShape() {
return circle;
}
public java.awt.Rectangle getBounds() {
return circle.getBounds();
}
public void modify(Point center, Point circum) {
double radius = center.distance(circum);
circle.x = center.x - (int)radius;
circle.y = center.y - (int)radius;
circle.width = circle.height = 2*radius;
}
private Ellipse2D.Double circle;
}
}
If we amend the createElement()
method in the MouseHandler class by
uncommenting the line that creates
Element.Circle objects, we will be ready
to draw circles. You are now equipped to
produce artwork such as that shown here.
How It Works
The circle is generated with the button down point as the center and the cursor position while dragging is on
the circumference. The distance() method defined in the Point2D class is used to calculate the radius,
and then this value is used to calculate the coordinates of the top-left corner of the enclosing rectangle. The
circle is stored as an Ellipse2D.Double object with a width and height as twice the radius.
Drawing Curves
Curves are a bit trickier to deal with than the other shapes. We want to be able to create a freehand
curve by dragging the mouse, so that as the cursor moves the curve extends. This will need to be
reflected in how we define the Element.Curve class. Let's first consider how the process of drawing a
curve is going to work, and define the Element.Curve class based on that.
Search WWH ::




Custom Search