import objectdraw.*; /** * Created nested squares iteratively (non-recursively). */ public class NestedSquaresIter { // spacing between layers of nested squares private static final double SQUARE_SPACING = 20; // side size of the smallest (innermost) square private static final double SMALLEST_SIDE_SIZE = 50; // the outermost square private FramedRect outerSquare; // create the nested squares at (upperLeftX, upperLeftY) with spacing // squareSpacing and an outermost square that has side of length sideSize. public NestedSquaresIter(double upperLeftX, double upperLeftY, double sideSize, DrawingCanvas canvas) { // draw the outer square no matter what outerSquare = new FramedRect(upperLeftX, upperLeftY, sideSize, sideSize, canvas); // if the sideSize is still greater than the smallest side size, // draw the rest iteratively while (sideSize > SMALLEST_SIDE_SIZE) { sideSize = sideSize - SQUARE_SPACING * 2; upperLeftX = upperLeftX + SQUARE_SPACING; upperLeftY = upperLeftY + SQUARE_SPACING; new FramedRect(upperLeftX, upperLeftY, sideSize, sideSize, canvas); } } // centers the outer square on the given point public void centerOnPoint(Location point) { outerSquare.moveTo(point.getX() - outerSquare.getWidth()/2, point.getY() - outerSquare.getHeight()/2); } // move outer square by indicated offsets public void move(double xOffset, double yOffset) { outerSquare.move(xOffset, yOffset); } // checks if the Location is in the NestedSquares object public boolean contains(Location point) { return outerSquare.contains(point); } }