import objectdraw.*;
import java.awt.*;

// Class to represent a graphical structure composed of nested rectangles.
public class NestedRects {
    
    // size difference of successive rectangles
    private static final int SIZE_DIFF = 8;
    // difference in x and y coordinates of upper left of successive rectangles
    private static final int INSET = 4;
    
    // the set of rectangles
    private FramedRect[ ] theRectangles;
    // the number of rectangles
    private int nestingDepth;
    
    // construct a graphical object of nested rectangles
    // double x, y - x and y coordinates of upper left
    // double width, height - width and height of the outermost rectangle
    // DrawingCanvas canvas - canvas on which nested rects should be drawn
    public NestedRects(double x, double y, double width, 
                       double height, DrawingCanvas canvas) {
        
        // determine the number of rectangles in the nested structure
        nestingDepth =  (int) Math.min(width, height)/SIZE_DIFF + 1;
        theRectangles = new FramedRect[nestingDepth];
        
        // construct the rectangular components
        for (int i = 0; i < nestingDepth; i++) {
            theRectangles[i] = new FramedRect(x, y, width, height, canvas);
            x = x + INSET;
            y = y + INSET;
            width = width - SIZE_DIFF;
            height = height - SIZE_DIFF;
        }
    }
    
    // move nested rects to specified location
    // double x, double y - new x and y coordinates of upper left
    public void moveTo(double x, double y) {
        for (int i = 0; i < nestingDepth; i++) {
            theRectangles[i].moveTo(x + 4*i, y + 4*i);
        }
    }
    
    // remove nested rects from the canvas
    public void removeFromCanvas() {
        for (int i = 0; i < nestingDepth; i++) {
            theRectangles[i].removeFromCanvas();
        }
    }
}
