Java Reference
In-Depth Information
import java.awt.*;
import java.io.Serializable;
import static Constants.SketcherConstants.*;
import java.awt.geom.*;
public abstract class Element implements Serializable{
// Code defining the base class...
// Nested class defining a line...
// Nested class defining a rectangle...
// Nested class defining a circle
public static class Circle extends Element {
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, 2.*radius,
2.*radius);
bounds = new java.awt.Rectangle(position.x, position.y,
1 + (int)circle.width,
1+(int)circle.height);
}
// Display the circle
public void draw(Graphics2D g2D) {
g2D.setPaint(color);
// Set the circle
color
g2D.translate(position.x, position.y);
// Move context
origin
g2D.draw(circle);
// Draw the circle
g2D.translate(-position.x, -position.y);
// Move context
origin back
}
// Recreate this circle
public void modify(Point center, Point circum) {
double radius = center.distance(circum);
circle.width = circle.height = 2*radius;
position.x = center.x - (int)radius;
position.y = center.y - (int)radius;
bounds = new java.awt.Rectangle(position.x, position.y,
1 + (int)circle.width,
1+(int)circle.height);
}
private Ellipse2D.Double circle;
Search WWH ::




Custom Search