Repository navigation
Expand file tree
/
Copy pathSquare.java
More file actions
134 lines (114 loc) · 3.06 KB
/
Copy pathSquare.java
File metadata and controls
134 lines (114 loc) · 3.06 KB
1
import java.awt.Color;import java.awt.Graphics;/** * A single square in a window. * * <p> * Adapterion of Nifty Assignment (http://nifty.stanford.edu/) by Mike Clancy in * 2001. Original code by Mike Clancy. Updated Fall 2022 by K. Raven * Russell.</p> */public class Square { /** * The next id when a new square is created. */ private static int nextID = 0; /** * The X position of the upper left corner of this square. */ private int upperLeftX; /** * The Y position of the upper left corner of this square. */ private int upperLeftY; /** * The size of this square. */ private int size; /** * The color this square. */ private Color color; /** * The id of this square. */ private int id; /** * Initialize a square with the given position (specified relative to the * WindowManager in which all this is being run), dimensions, and color. * * @param centerX the x position of the center of this square * @param centerY the y position of the center of this square * @param size the size of this square * @param c the color of this square */ public Square(int centerX, int centerY, int size, Color c) { this.upperLeftX = centerX - size / 2; this.upperLeftY = centerY - size / 2; this.size = size; this.color = c; this.id = nextID; nextID++; } /** * method that checks if a given (x,y) position is within a square. * * @param x position on grid * @param y position on grid * @return true if the (x,y) is in square, else false */ public boolean contains(int x, int y) { if(x >= this.upperLeftX && x <= (this.upperLeftX+size) && y >= this.upperLeftY && x <= (this.upperLeftY+size)){ return true; } return false; } /** * Fetches the id of this square. * * @return the id of this square */ public int id() { return id; } /** * Gets the upper left x position of the square. * * @return the upper left x position */ public int getUpperLeftX() { return upperLeftX; } /** * Gets the upper left y position of the square. * * @return the upper left y position */ public int getUpperLeftY() { return upperLeftY; } /** * {@inheritDoc} */ @Override public boolean equals(Object o) { if (o instanceof Square) { Square sq = (Square) o; return upperLeftX == sq.upperLeftX && upperLeftY == sq.upperLeftY && size == sq.size; } return false; } /** * Draw the square, surrounded by a border. * * @param g the graphics to paint on, assumed not null */ public void paint(Graphics g) { g.setColor(color); g.drawRect(this.upperLeftX, this.upperLeftY, size, size); g.fillRect(this.upperLeftX, this.upperLeftY, size, size); }}