Class Piece

java.lang.Object
  extended by java.util.Observable
      extended by Piece

public abstract class Piece
extends java.util.Observable

An abstract class that represents a playing piece. This class, together with Board, 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
Piece()
          Creates a Piece.
 
Method Summary
 boolean canMove(int deltaRow, int deltaColumn)
          Determines whether this piece can be moved to the new 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()
           
 int getColumn()
          Returns the column that this piece is in, or -1 if this piece is not on the board.
 int getRow()
          Returns the row that this piece is in, or -1 if this piece is not on the board.
 int getSpeed()
          Get the speed (in pixels/redraw) at which this piece will move.
 boolean isDraggable()
          Returns true if the piece can be dragged by the mouse.
 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 array location.
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)
          Determines whether the 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

Piece

public Piece()
Creates a Piece. The piece is not actually placed on any board; for that, see the methods board.place(piece, row, column) and piece.place(board, row, column).

Method Detail

getBoard

public Board getBoard()
Returns:
The board (if any) containing this piece.

toString

public java.lang.String toString()
Returns a printable String representing this Piece.

Overrides:
toString in class java.lang.Object
Returns:
A printable representation of this Piece.

setDraggable

public void setDraggable(boolean draggable)
Determines whether the piece can be dragged by the mouse.

Parameters:
draggable - Tell whether the piece can be dragged by user mouse movement.

isDraggable

public boolean isDraggable()
Returns true if the piece can be dragged by the mouse.

Returns:
true if the piece is draggable.

setSelectable

public void setSelectable(boolean selectable)
Determines whether the piece can be selected by the mouse.

Parameters:
selectable - Set to true if and only if the piece can be selected by clicking on it.

isSelectable

public boolean isSelectable()
Returns true if the piece can be selected by the mouse.

Returns:
true if the piece is selectable.

getRow

public int getRow()
Returns the row that this piece is in, or -1 if this piece is not on the board.

Returns:
The row number.

getColumn

public int getColumn()
Returns the column that this piece is in, or -1 if this piece is not on the board.

Returns:
The column number.

place

public void place(Board board,
                  int row,
                  int column)
Places this piece at the given row and column on the board.

Parameters:
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.

remove

public void remove()
Removes this piece from whatever board it may be on, but does not delete the piece itself.


setSpeed

public void setSpeed(int speed)
Sets the speed of movement (in squares/second) for this piece. Values of 1 to 10 are reasonable; very large values will cause this piece to "teleport" to the new location; a zero or negative value will cause the default speed (set by the board) to be used.

Parameters:
speed - The desired speed of movement, in pixels/redraw.

getSpeed

public int getSpeed()
Get the speed (in pixels/redraw) at which this piece will move.

Returns:
This Piece's speed of movement, in pixels/redraw.

paint

public 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.

Parameters:
g - The Graphics object on which painting should be done.
r - The rectangle in which to paint this piece.

moveToTop

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


move

public boolean move(int deltaRow,
                    int deltaColumn)
Moves this piece the given number of rows and columns.

Parameters:
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.
Returns:
False if the move would take the piece outside the boundaries of the board.

moveTo

public boolean moveTo(int newRow,
                      int newColumn)
Moves this piece to a new position on the board.

Parameters:
newRow - The destination row.
newColumn - The destination column.
Returns:
false if the destination is not a legal board position, or if the piece is already moving.

canMove

public boolean canMove(int deltaRow,
                       int deltaColumn)
Determines whether this piece can be moved to the new location. This method returns 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.

Parameters:
deltaRow - The number of squares to move down.
deltaColumn - The number of squares to move to the right.
Returns:
true if the move can be made.

canMoveTo

public boolean canMoveTo(int newRow,
                         int newColumn)
Determines whether this piece can be moved to the specified location. This method returns 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.

Parameters:
newRow - The desired row.
newColumn - The desired column.
Returns:
true if the move can be made.

redraw

public void redraw()
Causes this piece to be redrawn.


redraw

public void redraw(java.awt.Rectangle rect)
Causes the given rectangle to be redrawn.

Parameters:
rect - The area to be redrawn.

dump

public void dump()
Debugging method to print out the status of this Piece.