import java.awt.*; public class Square { public static boolean DEBUGGING = false; /** * EFFECT: Initialize a square with the given position (specified * relative to the CS61BWindow in which all this is being run), * dimensions, and color. */ public Square (int centerX, int centerY, int size, Color c) { myUpperLeftX = centerX - size/2; myUpperLeftY = centerY - size/2; mySize = size; myColor = c; myId = ourNextId; ourNextId++; if (DEBUGGING) { System.out.println ("Square " + myId + " with size " + size + " and upper left corner (" + myUpperLeftX + "," + myUpperLeftY + ") is created."); } } /** * EFFECT: Return true if the square is covering coordinate (x,y); * return false otherwise. */ public boolean contains (int x, int y) { boolean result = x >= myUpperLeftX && x <= myUpperLeftX+mySize && y >= myUpperLeftY && y <= myUpperLeftY+mySize; if (result && DEBUGGING) { System.out.println ("Square " + myId + " is clicked on."); } return result; } /** * EFFECT: Return the identification # of this square. */ public int id ( ) { return myId; } /** * REQUIRES: o != null; o's type is a subclass of Square. * EFFECT: Returns true when this square has the same position * and dimensions as o. */ public boolean equals (Object o) { Square sq = (Square) o; return myUpperLeftX == sq.myUpperLeftX && myUpperLeftY == sq.myUpperLeftY && mySize == sq.mySize; } /** * REQUIRES: g != null. * MODIFIES: the displayed window. * EFFECT: Draw the square, surrounded by a border. */ public void paint (Graphics g) { g.setColor (myColor); g.fillRect (myUpperLeftX, myUpperLeftY, mySize, mySize); g.setColor (Color.black); g.drawRect (myUpperLeftX, myUpperLeftY, mySize, mySize); } private int myUpperLeftX; // this square's upper left corner private int myUpperLeftY; private int mySize; // this square's size private Color myColor; // this square's color private int myId; // helps to tell squares apart when debugging static int ourNextId = 0; }