Site hosted by Angelfire.com: Build your free website today!
 

Example

In this example, we are going to re-implement the Drawing class in the previous section.

package drawing;

import java.util.*;

import graphicalobject.GraphicElement;

public class Drawing {
//	GraphicElement[] element = new GraphicElement[10];
	Collection elements = new Vector(); //or ArrayList or HashMap
//	private int count = 0;
	public void addElement(GraphicElement elem) {
//		element[count++] = elem;
		elements.add(elem);
	}
	public int getElementCount() {
//		return count;
		return elements.size();
	}
	public void display() {
//		for (int i = 0; i < count; i++) {
//			element[i].display();
//		}
		for (Iterator it = elements.iterator(); it.hasNext(); ) {
			((GraphicElement)it.next()).display();
		}
	}
}