Java Reference
In-Depth Information
Initially, you define the five classes shown Figure 19-22 that represent shapes, with the Element class as
a base. They provide objects that represent straight lines, rectangles, circles, freehand curves, and blocks of
text. These classes inherit the fields that you define for the Element class and they are serializable.
As you can see from the names of the Sketcher shape classes in Figure 19-22 , they are all nested classes
to the Element class. The Element class serves as the base class, as well as housing the shape classes. This
helps to avoid any possible confusion with other classes in the Java libraries that might have the same names.
Because there are no Element objects around, you declare the inner shape classes as static members of the
Element class.
You can now define the base class, Element . This won't be the final version because you will add more
functionality in later chapters. Here's the code for Element.java :
import java.awt.*;
import java.io.Serializable;
public abstract class Element implements Serializable {
protected Element(Point position, Color color) {
this.position = position;
this.color = color;
}
public Color getColor() {
return color;
}
public Point getPosition() {
return position;
}
public java.awt.Rectangle getBounds() {
return bounds;
}
public abstract void draw(Graphics2D g2D);
public abstract void modify(Point start, Point last);
protected Point position;
// Position of a shape
protected Color color;
// Color of a shape
protected java.awt.Rectangle bounds;
// Bounding rectangle
private final static long serialVersionUID = 1001L;
}
Directory "Sketcher 4 drawing sketch line and rectangle elements"
Put this file in the same directory as Sketcher.java . You have defined a constructor to initialize the
color and position data member and the get methods to provide access to these. The bounds member is
created by the subclasses and it also has a get method. The member and return type must be qualified by the
Search WWH ::




Custom Search