net.percederberg.tetris
Class SquareBoard

java.lang.Object
  |
  +--net.percederberg.tetris.SquareBoard

public class SquareBoard
extends java.lang.Object

A Tetris square board. The board is rectangular and contains a grid of colored squares. The board is considered to be constrained to both sides (left and right), and to the bottom. There is no constraint to the top of the board, although colors assigned to positions above the board are not saved.

Version:
1.2
Author:
Per Cederberg, per@percederberg.net

Constructor Summary
SquareBoard(int width, int height)
          Creates a new square board with the specified size.
 
Method Summary
 void clear()
          Clears the board, i.e. removes all the colored squares.
 int getBoardHeight()
          Returns the board height (in squares).
 int getBoardWidth()
          Returns the board width (in squares).
 java.awt.Component getComponent()
          Returns a graphical component to draw the board.
 int getRemovedLines()
          Returns the number of lines removed since the last clear().
 java.awt.Color getSquareColor(int x, int y)
          Returns the color of an individual square on the board.
 boolean hasFullLines()
          Checks if the board contains any full lines.
 boolean isLineEmpty(int y)
          Checks if a specified line is empty, i.e. only contains empty squares.
 boolean isLineFull(int y)
          Checks if a specified line is full, i.e. only contains no empty squares.
 boolean isSquareEmpty(int x, int y)
          Checks if a specified square is empty, i.e. if it is not marked with a color.
 void removeFullLines()
          Removes all full lines.
 void setMessage(java.lang.String message)
          Sets a message to display on the square board.
 void setSquareColor(int x, int y, java.awt.Color color)
          Changes the color of an individual square on the board.
 void update()
          Updates the graphical component.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SquareBoard

public SquareBoard(int width,
                   int height)
Creates a new square board with the specified size. The square board will initially be empty.
Parameters:
width - the width of the board (in squares)
height - the height of the board (in squares)
Method Detail

isSquareEmpty

public boolean isSquareEmpty(int x,
                             int y)
Checks if a specified square is empty, i.e. if it is not marked with a color. If the square is outside the board, false will be returned in all cases except when the square is directly above the board.
Parameters:
x - the horizontal position (0 <= x < width)
y - the vertical position (0 <= y < height)
Returns:
true if the square is emtpy, or false otherwise

isLineEmpty

public boolean isLineEmpty(int y)
Checks if a specified line is empty, i.e. only contains empty squares. If the line is outside the board, false will always be returned.
Parameters:
y - the vertical position (0 <= y < height)
Returns:
true if the whole line is empty, or false otherwise

isLineFull

public boolean isLineFull(int y)
Checks if a specified line is full, i.e. only contains no empty squares. If the line is outside the board, true will always be returned.
Parameters:
y - the vertical position (0 <= y < height)
Returns:
true if the whole line is full, or false otherwise

hasFullLines

public boolean hasFullLines()
Checks if the board contains any full lines.
Returns:
true if there are full lines on the board, or false otherwise

getComponent

public java.awt.Component getComponent()
Returns a graphical component to draw the board. The component returned will automatically be updated when changes are made to this board. Multiple calls to this method will return the same component, as a square board can only have a single graphical representation.
Returns:
a graphical component that draws this board

getBoardHeight

public int getBoardHeight()
Returns the board height (in squares). This method returns, i.e, the number of vertical squares that fit on the board.
Returns:
the board height in squares

getBoardWidth

public int getBoardWidth()
Returns the board width (in squares). This method returns, i.e, the number of horizontal squares that fit on the board.
Returns:
the board width in squares

getRemovedLines

public int getRemovedLines()
Returns the number of lines removed since the last clear().
Returns:
the number of lines removed since the last clear call

getSquareColor

public java.awt.Color getSquareColor(int x,
                                     int y)
Returns the color of an individual square on the board. If the square is empty or outside the board, null will be returned.
Parameters:
x - the horizontal position (0 <= x < width)
y - the vertical position (0 <= y < height)
Returns:
the square color, or null for none

setSquareColor

public void setSquareColor(int x,
                           int y,
                           java.awt.Color color)
Changes the color of an individual square on the board. The square will be marked as in need of a repaint, but the graphical component will NOT be repainted until the update() method is called.
Parameters:
x - the horizontal position (0 <= x < width)
y - the vertical position (0 <= y < height)
color - the new square color, or null for empty

setMessage

public void setMessage(java.lang.String message)
Sets a message to display on the square board. This is supposed to be used when the board is not being used for active drawing, as it slows down the drawing considerably.
Parameters:
message - a message to display, or null to remove a previous message

clear

public void clear()
Clears the board, i.e. removes all the colored squares. As side-effects, the number of removed lines will be reset to zero, and the component will be repainted immediately.

removeFullLines

public void removeFullLines()
Removes all full lines. All lines above a removed line will be moved downward one step, and a new empty line will be added at the top. After removing all full lines, the component will be repainted.
See Also:
hasFullLines()

update

public void update()
Updates the graphical component. Any squares previously changed will be repainted by this method.