Java Reference
In-Depth Information
public abstract java.awt.Rectangle getBounds();
protected Color color; // Color of a shape
}
We have defined a constructor to initialize the data member, and the getColor() method. The other
methods are abstract , so they must be implemented by the subclasses.
Note that the return type for the abstract getBounds() method is fully qualified using the package name.
This is to prevent confusion with our own Rectangle class that we will add later on in this chapter.
Storing Shapes in the Document
Even though we haven't defined the classes for the shapes that Sketcher will create, we can implement
the mechanism for storing them in the SketchModel class. We'll be storing all of them as objects of
type Element . We can use a LinkedList collection class object to hold an arbitrary number of
Element objects, since a LinkedList can store any kind of object. It also has the advantage that
deleting a shape from the list is fast.
We can add a member to the SketchModel class that we added earlier to the Sketcher program to
store elements:
import java.util.Observable;
import java.util.LinkedList;
class SketchModel extends Observable {
protected LinkedList elementList = new LinkedList();
}
We will want methods to add and delete Element objects from the linked list, and a method to return
an iterator for the list, so we should add those to the class too:
import java.util.Observable;
import java.util.LinkedList;
import java.util.Iterator;
class SketchModel extends Observable {
public boolean remove(Element element) {
boolean removed = elementList.remove(element);
if(removed) {
setChanged();
notifyObservers(element.getBounds());
}
return removed;
}
public void add(Element element) {
elementList.add(element);
setChanged();
Search WWH ::




Custom Search