|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.util.Observable
boardGame.Piece
public abstract class Piece
An abstract class that represents a playing piece. This class, together with Board, can be used to display board games. This class maintains information about board positions, but does not include any game-specific logic.
| Constructor Summary | |
|---|---|
Piece()
Creates a piece. |
|
Piece(java.lang.String name)
Creates a piece with the given name. |
|
| Method Summary | |
|---|---|
boolean |
canMove(int deltaRow,
int deltaColumn)
Determines whether this piece can be moved to the specified location. |
boolean |
canMoveTo(int newRow,
int newColumn)
Determines whether this piece can be moved to the specified location. |
void |
dump()
Debugging method to print out the status of this piece. |
Board |
getBoard()
Returns the board on which this piece has been placed. |
int |
getColumn()
Returns the column that this piece is in, or -1 if this piece is not currently on some board. |
java.lang.String |
getName()
Returns the name of this piece; possibly useful for debugging. |
int |
getRow()
Returns the row that this piece is in, or -1 if this piece is not currently on some board. |
int |
getSpeed()
Get the speed (in squares/second) at which this piece will move. |
boolean |
isDraggable()
Returns true if the piece can be dragged by the mouse. |
boolean |
isOnBoard()
Tests whether this piece is on some (any) board. |
boolean |
isOnBoard(Board board)
Tests whether this piece is on the specified board. |
boolean |
isSelectable()
Returns true if the piece can be selected by the mouse. |
boolean |
move(int deltaRow,
int deltaColumn)
Moves this piece the given number of rows and columns. |
boolean |
moveTo(int newRow,
int newColumn)
Moves this piece to a new position on the board. |
void |
moveToTop()
Ensures that this piece will be drawn on top of any other pieces in the same location on the board. |
abstract void |
paint(java.awt.Graphics g,
java.awt.Rectangle r)
Paints this piece on the board within the given rectangle; must be implemented by a subclass. |
void |
place(Board board,
int row,
int column)
Places this piece at the given row and column on the board. |
void |
redraw()
Causes this piece to be redrawn. |
void |
redraw(java.awt.Rectangle rect)
Causes the given rectangle to be redrawn. |
void |
remove()
Removes this piece from whatever board it may be on, but does not delete the piece itself. |
void |
setDraggable(boolean draggable)
Determines whether the piece can be dragged by the mouse. |
void |
setSelectable(boolean selectable)
Sets whether this piece can be selected by the mouse. |
void |
setSpeed(int speed)
Sets the speed of movement (in squares/second) for this piece. |
java.lang.String |
toString()
Returns a printable String representing this piece. |
| 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, wait, wait, wait |
| Constructor Detail |
|---|
public Piece()
board.place(piece, row, column) and
piece.place(board, row, column).
public Piece(java.lang.String name)
board.place(piece, row, column) and
piece.place(board, row, column).
name - The user-defined name of this piece.| Method Detail |
|---|
public java.lang.String getName()
public Board getBoard()
public java.lang.String toString()
toString in class java.lang.Objectpublic void setDraggable(boolean draggable)
draggable - Tell whether the piece can be dragged by user mouse
movement.public boolean isDraggable()
true if the piece can be dragged by the mouse.
true if the piece is draggable.public void setSelectable(boolean selectable)
selectable - A value of true means that the piece can
be selected with the mouse.public boolean isSelectable()
true if the piece can be selected by the mouse.
true if the piece is selectable.public int getRow()
public int getColumn()
public void place(Board board,
int row,
int column)
board - The board on which to place this piece.row - The row in which to place this piece.column - The column in which to place this piece.public void remove()
public boolean isOnBoard()
true if this piece is currently on a board.public boolean isOnBoard(Board board)
true if this piece is currently on
the given board.public void setSpeed(int speed)
speed - The desired speed of movement, in pixels/redraw.public int getSpeed()
public abstract void paint(java.awt.Graphics g,
java.awt.Rectangle r)
g - The Graphics object on which painting should be done.r - The rectangle in which to paint this piece.public void moveToTop()
public boolean move(int deltaRow,
int deltaColumn)
deltaRow - The number of rows down to move this piece; a negative number will
move the piece up.deltaColumn - The number of columns to move this piece to the right; a negative
number will move the piece left.
public boolean moveTo(int newRow,
int newColumn)
newRow - The destination row.newColumn - The destination column.
false if the destination is not a legal board
position, or if the piece is already moving.
public final boolean canMove(int deltaRow,
int deltaColumn)
true
if this piece is on some board, and the specified location
exists on that board.
This method delegates the decision to the canMoveTo
method. To ensure consistency, this method may not be overridden;
but overriding canMoveTo with more specific tests
will alter the behavior of both methods.
deltaRow - The number of squares to move down.deltaColumn - The number of squares to move to the right.
true if the move can be made.
public boolean canMoveTo(int newRow,
int newColumn)
true if this
piece is on a board and the location is legal for that board.
This method can be overridden with more specific tests.
newRow - The desired row.newColumn - The desired column.
true if the move can be made.public void redraw()
public void redraw(java.awt.Rectangle rect)
rect - The area to be redrawn.public void dump()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||