Class Board

java.lang.Object
  extended by java.util.Observable
      extended by Board
All Implemented Interfaces:
java.util.Observer

public class Board
extends java.util.Observable
implements java.util.Observer

A generic board that, together with Piece, can be used to display board games. This class is only for display purposes; logic for the game should be in user-defined classes.


Field Summary
protected  boolean panelHasBeenResized
           
 
Constructor Summary
Board(int rows, int columns)
          Creates a playing board with the given number of rows and columns.
 
Method Summary
protected  void changePositionOnBoard(Piece piece, int oldRow, int oldColumn, int newRow, int newColumn)
          Internal method to move a Piece from one position to another.
 int columnToX(int columnNumber)
          Returns the X coordinate of the left side of cells in the given column of this Board.
protected  void dump()
          Displays the board contents (for debugging).
protected  Piece findPiece(int x, int y)
          Given x-y coordinates, finds and returns the topmost piece at that location on this board, or null if there is no such piece.
protected  int getCellHeight()
          Returns the approximate current height, in pixels, of a single cell on this Board.
protected  int getCellWidth()
          Returns the approximate current width, in pixels, of a single cell on this Board.
 int getColumns()
          Returns the number of columns in this Board.
 javax.swing.JPanel getJPanel()
          Returns the JPanel on which this board is displayed.
 Piece getPiece(int row, int column)
          Returns the topmost piece at the given row and column in this Board, or null if the given location is empty.
 java.util.Stack getPieces(int row, int column)
          Returns a (possibly empty) Stack of all the pieces in the given position.
 int getRows()
          Returns the number of rows in this Board.
 int[] getSelectedSquare()
          Returns the currently selected square, or null if no square is selected.
 int getSpeed()
          Returns the default speed (in squares per second) of pieces on this board.
 boolean isEmpty(int row, int column)
          Returns true if the given row and column on this board contains no Pieces.
 boolean isLegalPosition(int row, int column)
          Determines whether the given row and column denote a legal position on this Board.
protected  void moveToTop(Piece piece)
          Ensures that the given piece will be drawn on top of any other pieces in the same array location.
 void paint(java.awt.Graphics g)
          Paints th1s board itself, not including the pieces.
 void place(Piece piece, int row, int column)
          Places the given piece at the given location in this board.
 Piece remove(int row, int column)
          Removes the top piece at the given row and column on this Board.
 boolean remove(Piece piece)
          Removes this piece from the board.
 int rowToY(int rowNumber)
          Returns the Y coordinate of the top side of cells in the given column of this Board.
 void setSelectedSquare(int[] selection)
          Selects the square on this Board whose coordinates are given in the parameter.
 void setSpeed(int speed)
          Sets the default speed of movement for pieces on this board, in squares per second.
 void update(java.util.Observable changedPiece, java.lang.Object rectangle)
          Redraws this Board whenever a Piece is modified.
 int xToColumn(int x)
          Given an x coordinate, determines which column it is in.
 int yToRow(int y)
          Given a y coordinate, determines which row it is in.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

panelHasBeenResized

protected boolean panelHasBeenResized
Constructor Detail

Board

public Board(int rows,
             int columns)
Creates a playing board with the given number of rows and columns. This board is a Swing JPanel and may be used wherever a JPanel may be used.

Parameters:
rows - Desired number of rows.
columns - Desired number of columns.
Method Detail

getJPanel

public javax.swing.JPanel getJPanel()
Returns the JPanel on which this board is displayed.

Returns:
The JPanel on which this Board is displayed.

getRows

public int getRows()
Returns the number of rows in this Board.

Returns:
The number of rows.

getColumns

public int getColumns()
Returns the number of columns in this Board.

Returns:
The number of columns.

getPiece

public Piece getPiece(int row,
                      int column)
Returns the topmost piece at the given row and column in this Board, or null if the given location is empty.

Parameters:
row - The row number.
column - The column number.
Returns:
The Piece in the given [row][column], or null if that location is empty. If the board location contains more than one piece, the "topmost" piece is returned.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

getPieces

public java.util.Stack getPieces(int row,
                                 int column)
Returns a (possibly empty) Stack of all the pieces in the given position. The top element of the stack is the topmost element in that board location.

Parameters:
row - A row number on this board.
column - A column number on this board.
Returns:
The pieces in this board location.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

isEmpty

public boolean isEmpty(int row,
                       int column)
Returns true if the given row and column on this board contains no Pieces.

Parameters:
row - The row to examine.
column - The column to examine.
Returns:
true if this location is empty.

findPiece

protected Piece findPiece(int x,
                          int y)
Given x-y coordinates, finds and returns the topmost piece at that location on this board, or null if there is no such piece.

Parameters:
x - The local x coordinate.
y - The local y coordinate.
Returns:
The Piece in the [row][column] containing the given (x, y) coordinates, or null if that location is empty. If the board location contains more than one piece, the "topmost" piece is returned.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

xToColumn

public int xToColumn(int x)
Given an x coordinate, determines which column it is in.

Parameters:
x - A local x coordinate.
Returns:
The number of the column containing the given x coordinate.

yToRow

public int yToRow(int y)
Given a y coordinate, determines which row it is in.

Parameters:
y - A local y coordinate.
Returns:
The number of the row containing the given y coordinate.

columnToX

public int columnToX(int columnNumber)
Returns the X coordinate of the left side of cells in the given column of this Board.

Parameters:
columnNumber - A column number.
Returns:
The X coordinate of the left side of that column.

rowToY

public int rowToY(int rowNumber)
Returns the Y coordinate of the top side of cells in the given column of this Board.

Parameters:
rowNumber - A row number.
Returns:
The Y coordinate of the top side of that row.

place

public void place(Piece piece,
                  int row,
                  int column)
Places the given piece at the given location in this board. It is possible to place more than one piece in a given board location, in which case later pieces go "on top of" earlier pieces.

Parameters:
piece - The Piece to be placed.
row - The row in which to place the piece.
column - The column in which to place the piece.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

remove

public Piece remove(int row,
                    int column)
Removes the top piece at the given row and column on this Board.

Parameters:
row - The row of the piece to be removed.
column - The column of the piece to be removed.
Returns:
The Piece that was removed.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.

changePositionOnBoard

protected void changePositionOnBoard(Piece piece,
                                     int oldRow,
                                     int oldColumn,
                                     int newRow,
                                     int newColumn)
Internal method to move a Piece from one position to another.

Parameters:
piece - The Piece to be moved.
oldRow - The original row number of the Piece.
oldColumn - The original column number of the Piece.
newRow - The new row number of the Piece.
newColumn - The new column number of the Piece.

remove

public boolean remove(Piece piece)
Removes this piece from the board. Does nothing if the piece is not, in fact, on the board.

Parameters:
piece - The piece to be removed.
Returns:
The removed Piece.

moveToTop

protected void moveToTop(Piece piece)
Ensures that the given piece will be drawn on top of any other pieces in the same array location.

Parameters:
piece - The piece to promote to the top.

setSpeed

public void setSpeed(int speed)
Sets the default speed of movement for pieces on this board, in squares per second. This value is used only for pieces that do not specify their own speed.

Parameters:
speed - The default speed for pieces on this board.

getSpeed

public int getSpeed()
Returns the default speed (in squares per second) of pieces on this board.

Returns:
The default speed for pieces on this board.

getCellWidth

protected int getCellWidth()
Returns the approximate current width, in pixels, of a single cell on this Board. The value is approximate because, due to roundoff errors, cells may vary slightly in size. This value will change if this Board is resized.

Returns:
The approximate width, in pixels, of one square on this Board.

getCellHeight

protected int getCellHeight()
Returns the approximate current height, in pixels, of a single cell on this Board. The value is approximate because, due to roundoff errors, cells may vary slightly in size. This value will change if this Board is resized.

Returns:
The approximate height, in pixels, of one square on this Board.

isLegalPosition

public boolean isLegalPosition(int row,
                               int column)
Determines whether the given row and column denote a legal position on this Board.

Parameters:
row - The given row number.
column - The given column number.
Returns:
true if the given row and column number represent a valid location on this board

update

public void update(java.util.Observable changedPiece,
                   java.lang.Object rectangle)
Redraws this Board whenever a Piece is modified. This method should not be overridden.

Specified by:
update in interface java.util.Observer
Parameters:
changedPiece - The Piece that needs to be redrawn.
rectangle - The Rectangle within which the Piece should be redrawn.

paint

public void paint(java.awt.Graphics g)
Paints th1s board itself, not including the pieces.

Parameters:
g - The Graphics context on which this board is painted.

dump

protected void dump()
Displays the board contents (for debugging).


getSelectedSquare

public int[] getSelectedSquare()
Returns the currently selected square, or null if no square is selected.

Returns:
The currently selected square.

setSelectedSquare

public void setSelectedSquare(int[] selection)
Selects the square on this Board whose coordinates are given in the parameter.

Parameters:
selection - The coordinates of the square to select.