import java.awt.Button; // We extend this class import java.awt.Color; // So we can show different colors import java.awt.Dimension; // So we can tell the frame about preferred // width and height import java.awt.Frame; // For building a test frame import java.awt.GridLayout; // The board will be a grid import java.awt.Graphics; // When to paint import java.awt.Rectangle; // Where to paint (returned by getBounds) import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import OthelloRules; /** * A simple test of drawing on buttons. You need to hit control-C to * quit. * * @author Samuel A. Rebelsky * @version 1.0 of March 2000 */ public class PieceButton extends Button implements ActionListener { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The color of this object. */ protected Color color; /** The type of rules */ protected OthelloRules gamerules; /** The x-coordinate of the piece */ protected int xcor; /** the y-coordinate of the piece */ protected int ycor; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ /** * Create a new button with a specific color piece. */ public PieceButton(OthelloRules rulethisbiznitch,int x,int y) { this.gamerules = rulethisbiznitch; this.xcor = x; this.ycor = y; this.addActionListener(this); } // PieceButton(Color) // +--------------------------+-------------------------------- // | Overriden Button Methods | // +--------------------------+ public void paint(Graphics paintBrush) { if (!(this.color == null)) { Rectangle bounds = this.getBounds(); paintBrush.setColor(this.color); paintBrush.fillOval(0, 0, bounds.width-3, bounds.height-3); paintBrush.setColor(this.color); paintBrush.fillOval(2, 2, bounds.width-3, bounds.height-3); } } // paint(Graphics) public Dimension getPreferredSize() { return new Dimension(20,20); } // getPreferredSize() // +----------------+------------------------------------------ // | Helpers| // +----------------+ protected boolean nullColor() { return (this.color == null); } //nullColor // +----------------+------------------------------------------ // | Event Handlers | // +----------------+ public void actionPerformed(ActionEvent e) { if (gamerules.legalMove(gamerules.getPlayer(),xcor,ycor)) { if (this.nullColor()) { if (gamerules.getPlayer() == 1) { this.color = Color.black; } //if else { if (gamerules.getPlayer() == 2) { this.color = Color.white; } //if else { if (gamerules.getPlayer() == 3) { this.color = Color.red; } //if else { if (gamerules.getPlayer() == 4) { this.color = Color.blue; } //if }//else }//else }//else }//if }//if this.repaint(); // tell rules/board that new piece exists and that will report // back what corrollary pieces are to be flipped!!! } // +---------+------------------------------------------------- // | Helpers | // +---------+ /** * */ public void flip(Color c) { this.color = c; this.repaint(); } // oppositeColor(Color) } // PieceButton