import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; /** * Cell is a color box. It can be hilited with an outline, * set to be on or off, draw itself with the correct * color (based on whether it is highlighted, and whether it is * on or off). Provided with an (x,y) coordinate, it can return * whether or not that location is contained within the cell. * * @author Chris Thiel * @version 1 May 2021 */ public class Cell { private int x, y, size; private boolean hilighted, on; private Rectangle bounds; private Color hiliteColor, onColor, offColor; public Cell(int x, int y, int size, Color hiliteColor, Color onColor, Color offColor){ this.x=x; this.y=y; this.setSize(size); bounds=new Rectangle(x,y,size, size); hilighted=false; on=false; this.hiliteColor=hiliteColor; this.onColor = onColor; this.offColor = offColor; } public Cell(int x, int y, int size) { this(x, y, size, Color.YELLOW, Color.CYAN, Color.BLUE); } public Cell(int x, int y, int size, Color onColor) { this(x, y, size, Color.YELLOW, onColor, onColor.darker() ); } public void setLocation(int x, int y) { this.x=x; this.y=y; bounds=new Rectangle(x,y,size, size); } public int getX() {return x;} public int getY() {return y;} public Color getHiliteColor() {return hiliteColor;} public Color getOnColor() {return onColor;} public Color getOffColor() {return offColor;} public Color getColor() { if (on) return onColor; return offColor; } public void setColor(Color c){ onColor = c; offColor = c.darker(); } public void setOnColor(Color c){ onColor = c; } public void setOffColor(Color c){ offColor = c; } public void setHiliteColor(Color c){ hiliteColor = c; } public void setHighlight(boolean value){ hilighted=value; } public boolean isHighlighted() { return hilighted; } public void click(){ on=!on; } public void on() { on = true; } public void off() { on = false; } public boolean isOn() { return on; } public boolean contains (int x, int y){ return bounds.contains(x, y); } public int getSize() { return size; } public void setSize(int size) { this.size = size; bounds=new Rectangle(x,y,size, size); } public void draw(Graphics g){ if (hilighted){ g.setColor(hiliteColor); g.fillRect(bounds.x-2, bounds.y-2, bounds.width+4, bounds.height+4); } g.setColor(offColor); if (on) g.setColor(onColor); g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height); } }