Java Reference
In-Depth Information
import java.awt.*;
import java.io.Serializable;
import static Constants.SketcherConstants.*;
import java.awt.geom.*;
class Element implements Serializable {
// Code for the base class definition...
// Nested class defining a line...
// Nested class defining a rectangle
public static class Rectangle extends Element {
public Rectangle(Point start, Point end, Color color) {
super(new Point(
Math.min(start.x, end.x), Math.min(start.y,
end.y)), color);
rectangle = new Rectangle2D.Double(
origin.x, origin.y,
//
Top-left corner
Math.abs(start.x - end.x), Math.abs(start.y - end.y));// Width
& height
bounds = new java.awt.Rectangle(
Math.min(start.x ,end.x),
Math.min(start.y,
end.y),
Math.abs(start.x - end.x)+1, Math.abs(start.y -
end.y)+1);
}
// Display the rectangle
public void draw(Graphics2D g2D) {
g2D.setPaint(color);
// Set the
rectangle color
g2D.translate(position.x, position.y);
// Move context
origin
g2D.draw(rectangle);
// Draw the
rectangle
g2D.translate(-position.x, -position.y);
// Move context
origin back
}
// Method to redefine the rectangle
public void modify(Point start, Point last) {
bounds.x = position.x = Math.min(start.x, last.x);
bounds.y = position.y = Math.min(start.y, last.y);
rectangle.width = Math.abs(start.x - last.x);
rectangle.height = Math.abs(start.y - last.y);
bounds.width = (int)rectangle.width +1;
bounds.height = (int)rectangle.height + 1;
Search WWH ::




Custom Search