import java.util.*; import java.awt.*; /** * Box is a color box from a palette of colors. * * @author Chris Thiel * @version 20 Nov 2018 */ public class Box { private int x, y; // location of top left corner private int size; // the size of the box private Color color; public static final Color[] palette= {Color.RED, Color.GREEN, Color.BLUE, Color.MAGENTA}; public static final String[] names = {"red", "green", "blue", "magenta"}; /** * Constructor for objects of class Box */ public Box(int size) { x = 0; y = 0; this.size = size; color = palette[(int)( palette.length*Math.random() )]; } public Color getColor() { return color; } public static String colorName(Color c) { String result = c.toString(); for(int i=0; i< palette.length ; i++) { if (c.equals(palette[i])) result = names[i]; } return result; } public void setLocation(int x, int y) { this.x = x; this.y = y; } /** * The contains method returns true if the x,y point is inside the box */ public boolean contains(int x, int y) { return ( x >= this.x ) && ( x <= this.x + size) && (y >= this.y) && (y <= this.y + size); } public void draw(Graphics g) { g.setColor(color); g.fillRect( x, y, size, size); g.setColor(Color.BLACK); g.drawRect( x, y, size, size); } }