Java Reference
In-Depth Information
The expressions for the coordinates for the point, position , ensure that we do set it as the location of
the top-left corner. The rectangle object is defined with its top-left corner at the origin, and its width and
height as before. We have to adjust the modify() method to adjust the location stored in position, and
leave the rectangle defined at the origin:
public void modify(Point start, Point last) {
position.x = Math.min(start.x, last.x);
position.y = Math.min(start.y, last.y);
rectangle.width = Math.abs(start.x - last.x);
rectangle.height = Math.abs(start.y - last.y);
}
You should already have added the revised version of the draw() and getBounds() methods for an
Element.Rectangle object essentially the same as that for lines.
Translating Circles
The Element.Circle class constructor is also very easy:
public Circle(Point center, Point circum, Color color) {
super(color);
// Radius is distance from center to circumference
double radius = center.distance(circum);
position = new Point(center.x - (int)radius,
center.y - (int)radius);
circle = new Ellipse2D.Double(origin.x, origin.y, // Position - top-left
2.*radius, 2.*radius ); // Width & height
}
The radius is calculated as before, and we make the top-left corner of the Ellipse2D.Double object
the origin point. Thus position is calculated as for the top-left corner in the previous version of the
constructor. We can adjust the modify() method to record the new coordinates of position :
public void modify(Point center, Point circum) {
double radius = center.distance(circum);
position.x = center.x - (int)radius;
position.y = center.y - (int)radius;
circle.width = circle.height = 2*radius;
}
The draw() and getBounds() methods are already done, so it's curves next.
Translating Curves
The Element.Curve class is just as simple:
public Curve(Point start, Point next, Color color) {
super(color);
Search WWH ::




Custom Search