// SetOfRects.java // Solution to Short Assignment #10 by THC. // Represents a set of Rect objects. When we want to add a Rect object to the // set but the set is full, we replace the least recently added Rect by the // new Rect. import java.awt.*; public class SetOfRects { private Rect[] rects; // reference to array of rectangles private int rectCount; // Only entries 0 to rectCount-1 have real rectangles private int leastRecentlyAdded; // index of least recently added rectangle // Constructor for a setOfRects. Makes an empty set of a given max size. public SetOfRects(int size) { rectCount = 0; rects = new Rect[size]; leastRecentlyAdded = 0; } // Add a rectangle to a setOfRects. If there's no room, punt the least // recently added rectangle. public void add(int left, int top, int width, int height, Color clr) { add(new Rect(left, top, width, height, clr)); } // Add a rectangle to a setOfRects. If there's no room, punt the least // recently added rectangle. // Alternate version. public void add(Rect rectangle) { if (rectCount < rects.length) { // There's room. rects[rectCount] = rectangle; rectCount++; } else { // No room for the new rectangle. rects[leastRecentlyAdded] = rectangle; leastRecentlyAdded = (leastRecentlyAdded + 1) % rects.length; } } // Draw all rectangles in a setOfRects. public void fill(Graphics page) { for (int i = 0; i < rectCount; i++) rects[i].fill(page); } }