import objectdraw.*; /** * Created nested squares iteratively. */ public class NestedSquaresV1 { private static final double SQUARE_SPACING = 20; private static final double SMALLEST_SIDE_SIZE = 50; private FramedRect outerSquare; // the outermost square // create the nested squares at (upperLeftX, upperLeftY) with spacing // squareSpacing and an outermost square that has side of length sideSize. public NestedSquaresV1(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 -= SQUARE_SPACING * 2; upperLeftX += SQUARE_SPACING; upperLeftY += SQUARE_SPACING; new FramedRect(upperLeftX, upperLeftY, sideSize, sideSize, canvas); } } // centers the outer square on the given point public void centerOnPoint(Location point) { // only centers the outer square, not the rest of the picture! 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) { // only moves the outer square, not the rest of the picture! outerSquare.move(xOffset, yOffset); } // checks if the Location is in the NestedSquares object public boolean contains(Location point) { return outerSquare.contains(point); } }