// Drawing.java // Class that stores a drawing as an ordered list of Shape objects. // Written by THC for CS 5 Lab Assignment 3. import java.util.ArrayList; import java.awt.*; public class Drawing { private ArrayList shapes; // the ordered list of Shape objects private Color currentColor; // current default color // Constructor creates an empty list of Shape objects and saves the // default color. public Drawing(Color initialColor) { shapes = new ArrayList(); currentColor = initialColor; } // Set the default color. public void setColor(Color c) { currentColor = c; } // Return the default color. public Color getColor() { return currentColor; } // Add a Shape to the front of the list. public void add(Shape s) { shapes.add(0, s); } // Have each Shape in the list draw itself. // Draws from back to front, so that Shapes in the front overlay Shapes // in the back. public void draw(Graphics page) { for (int i = shapes.size() - 1; i >= 0; i--) shapes.get(i).draw(page); } // Return a reference to the first Shape in the drawing (from front to back) // that contains Point p. If no Shape contains p, return null. public Shape getFrontmostContainer(Point p) { for (int i = 0; i < shapes.size(); i++) if (shapes.get(i).containsPoint(p)) return shapes.get(i); return null; } // Given a reference to a Shape, remove it from the drawing if it's there. public void remove(Shape s) { int index = shapes.indexOf(s); // where in the ArrayList it is if (index >= 0) // is it in the ArrayList? shapes.remove(index); // yes, remove it } // Given a reference to a Shape, move it to the front of the drawing // if it's actually in the drawing. public void moveToFront(Shape s) { int index = shapes.indexOf(s); // where in the ArrayList it is if (index >= 0) { // is it in the ArrayList? shapes.remove(index); // yes, remove it from where it is... shapes.add(0, s); // ...and add it to the front } } // Given a reference to a Shape, move it to the back of the drawing // if it's actually in the drawing. public void moveToBack(Shape s) { int index = shapes.indexOf(s); // where in the ArrayList it is if (index >= 0) { // is it in the ArrayList? shapes.remove(index); // yes, remove it from where it is... shapes.add(s); // ...and add it to the back } } // Make a Shape replace the Shape currently at the front of the drawing. public void replaceFront(Shape s) { if (shapes.size() > 0) { // is the drawing nonempty? shapes.remove(0); // yes, remove the front Shape... shapes.add(0, s); // ...and put in the replacement } } }