|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectjava.util.Observable
Board
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.
| Constructor Summary | |
Board(int rows,
int columns)
Creates a playing board with the given number of rows and columns. |
|
| Method Summary | |
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 |
getSelectedColumn()
Returns the column number (counting from zero) of the currently selected square, or -1 if none is selected. |
Piece |
getSelectedPiece()
Returns the currently selected Piece, or null if
no Piece has been selected on this Board. |
int |
getSelectedRow()
Returns the row number (counting from zero) of the currently selected square, or -1 if none 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. |
boolean |
isSelectable(int row,
int column)
Returns true to indicate that this square of the
board can be selected; override this method to provide different
behavior. |
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 and returns the top piece at the given row and column on this Board. |
boolean |
remove(Piece piece)
Removes this piece from the board. |
void |
setSelectedPiece(Piece selectedPiece)
Mark the given Piece as the selected one; unmark any previously selected Piece. |
void |
setSelectedSquare(int row,
int column)
If the given square is selectable, select it, otherwise do nothing. |
void |
setSpeed(int speed)
Sets the default speed of movement for pieces on this board, in squares per second. |
void |
unselectPiece()
Unselects the currently selected Piece on this Board. |
void |
unselectSquare()
Unselects the currently selected square, if any. |
void |
update(java.util.Observable changedPiece,
java.lang.Object rectangle)
Redraws this Board whenever a Piece is modified. |
| Methods inherited from class java.util.Observable |
addObserver, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public Board(int rows,
int columns)
JPanel and may be used wherever a
JPanel may be used.
rows - Desired number of rows.columns - Desired number of columns.| Method Detail |
public javax.swing.JPanel getJPanel()
public int getRows()
public int getColumns()
public Piece getPiece(int row,
int column)
row - The row number.column - The column number.
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.
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.
public java.util.Stack getPieces(int row,
int column)
row - A row number on this board.column - A column number on this board.
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.
public boolean isEmpty(int row,
int column)
true if the given row and column on this board
contains no Pieces.
row - The row to examine.column - The column to examine.
true if this location is empty.
public void place(Piece piece,
int row,
int column)
piece - The Piece to be placed.row - The row in which to place the piece.column - The column in which to place the piece.
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.
public Piece remove(int row,
int column)
row - The row of the piece to be removed.column - The column of the piece to be removed.
java.lang.ArrayIndexOutOfBoundsException - If the specified location does not exist.public boolean remove(Piece piece)
piece - The piece to be removed.
public void setSpeed(int speed)
speed - The default speed for pieces on this board.public int getSpeed()
public boolean isLegalPosition(int row,
int column)
row - The given row number.column - The given column number.
true if the given row and column number represent
a valid location on this board
public void update(java.util.Observable changedPiece,
java.lang.Object rectangle)
update in interface java.util.ObserverchangedPiece - The piece that needs to be redrawn (not used).rectangle - The area that needs to be redrawn.public void paint(java.awt.Graphics g)
g - The Graphics context on which this board is painted.public void setSelectedPiece(Piece selectedPiece)
selectedPiece - The Piece to be "selected."public Piece getSelectedPiece()
null if
no Piece has been selected on this Board.
public void unselectPiece()
public boolean isSelectable(int row,
int column)
true to indicate that this square of the
board can be selected; override this method to provide different
behavior.
row - The row index of the square to consider.column - The column index of the square to consider.
true.public int getSelectedRow()
public int getSelectedColumn()
public void setSelectedSquare(int row,
int column)
row - The row number of the square being selected.column - The column number of the square being selected.public void unselectSquare()
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||