import objectdraw.*; import java.awt.*; import java.util.Random; /** * Class representing a quilt of colored squares. */ public class Quilt { // quilt square size private static final int SQUARE_SIZE = 50; // quilt upper-left corner location private static final int QUILT_UPPER_LEFT_X = 50; private static final int QUILT_UPPER_LEFT_Y = 100; // for random colors of quilt squares private static final int NUM_COLOR_LEVELS = 256; private Random rand = new Random(); // the quilt is a 2-dimensional array of solid squares and outlines private FilledRect[][] quiltSquares; public Quilt(int numRows, int numCols, DrawingCanvas canvas) { // create the quilt array quiltSquares = new FilledRect[numRows][numCols]; // fill the array of quilt squares; create the quilt // outlines, but don't put them in an array since // we won't actually be doing anything with/to them // the y coordinate changes for each row int yCoord = QUILT_UPPER_LEFT_Y; for (int r = 0; r < quiltSquares.length; r++) { // the x coordinate changes for each column int xCoord = QUILT_UPPER_LEFT_X; for (int c = 0; c < quiltSquares[r].length; c++) { quiltSquares[r][c] = new FilledRect(xCoord, yCoord, SQUARE_SIZE, SQUARE_SIZE, canvas); quiltSquares[r][c].setColor(new Color(rand.nextInt(NUM_COLOR_LEVELS), rand.nextInt(NUM_COLOR_LEVELS), rand.nextInt(NUM_COLOR_LEVELS))); new FramedRect(xCoord, yCoord, SQUARE_SIZE, SQUARE_SIZE, canvas); // change x coordinate for the next column xCoord = xCoord + SQUARE_SIZE; } // change y coordinate for the next row yCoord = yCoord + SQUARE_SIZE; } } // if point is inside one of the quilt squares, return // a refererence to that square; otherwise return null public FilledRect squareSelected(Location point) { for (int r = 0; r < quiltSquares.length; r++) { for (int c = 0; c < quiltSquares[r].length; c++) { if (quiltSquares[r][c].contains(point)) { return quiltSquares[r][c]; } } } return null; } }