//Field.java //A GUI element displayed on the Chess applet. An extended version of //GoodButotn that can keep track of pieces but other than that has no //analytical methods //public int col, row; range from 1 to 8 //private Piece piece; reference to piece that stands on this square , null if square empty // //constructor - public Square(Color c, int w, int h, int col, int row) //(first 3 parameters - fot the GoodButton constructor) //only this GoodButtoon's constructor is used so far //user must take care to give correct col, row // //presents to the user methods: //public String coords() returns its coordinates as "a1","h8", etc. //public void setPiece(Piece p) puts the given piece on this square, drawing its image //public void Piece getPiece() returns the piece, null if none //public void clearPiece() sets piece to null regardess of current content // //Methods setPiece() and clearPiece() are should only be accessed by Piece //class. DO NOT attempt to call them directly! import java.awt.*; import java.awt.event.*; import java.util.*; public class Field extends GoodButton { public int col, row; public Piece piece = null; //range from 1 to 8 public Field(int col, int row) { super(Color.green,33,33); //Color.green is just to feed some color to super() before we find what it really should be String s = properColor(col,row); //"white/black" Color c = (s.equals("white")) ? Colors.dwsquare : Colors.dbsquare; resetColor(c); //inherited from GoodButton this.col = col; this.row = row; } //receives coordinates in one string like "a1", etc. String must have the right format public Field(String nm) { super(Color.green,33,33); //Color.green is just to feed some color to super() before we find what it really should be int col = (int) (nm.charAt(0) - 'a' + 1); int row = (int) (nm.charAt(1) - '1' + 1); String s = properColor(col,row); //"white/black" Color c = (s.equals("white")) ? Colors.dwsquare : Colors.dbsquare; resetColor(c); //inherited from GoodButton this.col = col; this.row = row; } public Field(Color c, int col, int row) { super(c,33,33); this.col = col; this.row = row; } public Field(Color c, int w, int h, int col, int row) { super(c,w,h); this.col = col; this.row = row; } /*public void paint(Graphics g) { removeImage(); if (piece != null) paintImage(piece.getShape(),piece.getColor()); }*/ //put the specified piece on this square public void setPiece(Piece p) { removeImage(); //from GoodButton; delete whatever may be currently displayed if (p != null) paintImage(p.getShape(),p.getColor()); //from GoodButton; draw the shape of this piece rememberPiece(p); //remember the piece that I am holding now } public void rememberPiece(Piece p) {piece = p;} public Piece getPiece() {return piece;} public void clearPiece() {piece = null;} public String coords() {return "" + (char)('a' + col - 1) + row;} public int getCol() {return col;} public int getRow() {return row;} public boolean empty() {if (piece == null) return true; else return false;} //determines which color i ought to have based on my coordinates private String properColor(int col, int row) { if ((col==1) || (col==3) || (col==5) || (col==7)) //a,c,e,g file if ((row==1) || (row==3) || (row==5) || (row==7)) return "black"; else return "white"; else //b,d,f,h file if ((row==1) || (row==3) || (row==5) || (row==7)) return "white"; else return "black"; } private boolean onBoard(int i, int j) { if ((i < 1) || (i > 8) || (j < 1) || (j > 8)) return false; else return true; } }