net.sourceforge.foobase
Class DisplayBoard

java.lang.Object
  |
  +--java.awt.Component
        |
        +--net.sourceforge.foobase.DisplayBoard

public class DisplayBoard
extends java.awt.Component
implements IsFoobaseMouseSource

Graphical display of a chess position.

This class is the main component of Foobase applications presenting chess games graphically: a chessboard is drawn, pieces are placed on it and events are generated when a user clicks on a square.

See Also:
Serialized Form

Inner Class Summary
protected  class DisplayBoard.MouseFilter
           
 
Field Summary
static java.lang.String __version
           
protected  Board _board
          Board setup to draw
protected  java.awt.Color _border
          Color to use for drawing a border aroung the board
protected  int _brcx
          Bottom right corner X-coordinate of clipping area
protected  int _brcy
          Bottom right corner Y-coordinate of clipping area
protected  java.awt.Color _dark
          Color to use for drawing a dark square
protected  java.util.Vector _ears
          List of IsFoobaseMouseListener objects that must be notified in case we generate a FoobaseMouseEvent
protected  int _h
          Height of each board square.
protected  int _hicol
          Column number of currently highlighted square (A1 is on column zero)
protected  java.awt.Color _high
          Color to use for highlighting square
protected  int _hirow
          Row number of currently highlighted square (A1 is on row zero)
protected  java.awt.Color _light
          Color to use for drawing a light square
protected  int _maxh
          Maximum height used for any piece
protected  java.awt.Graphics _offgraph
          Offscreen Graphics
protected  java.awt.Image _offscreen
          Offscreen image on which actual drawing takes place.
protected  byte[] _oldsetup
          Local copy of pieces setup as drawn in offscreen image.
protected  java.awt.Image[] _pcs
          Images to use for drawing of pieces.
protected  java.awt.Dimension _size
          Board dimensions.
protected  java.awt.Font _txtfont
          Font to use for drawing row numbers and column letters
protected  int _ulcx
          Upper left corner X-coordinate for clipping area
protected  int _ulcy
          Upper right corner Y-coordinate for clipping area
protected  int _w
          Width of each board square.
protected  int _x0
          X-offset of left upper corner of the board inside its border.
protected  int _y0
          Y-offset of left upper corner of the board inside its border.
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface net.sourceforge.foobase.IsFoobaseMouseSource
__version
 
Constructor Summary
DisplayBoard()
          Default public constructor.
DisplayBoard(java.awt.Image[] pcs)
          Public constructor.
DisplayBoard(java.awt.Image[] pcs, Board board)
          Public constructor.
 
Method Summary
 void addFoobaseMouseListener(IsFoobaseMouseListener ear)
          Register an IsFoobaseMouseListener client object.
protected  void drawBoard(java.awt.Graphics g)
          Actual offscreen image drawing method.
 void drawSquare(int r, int c)
          (Re)draw the contents of one square.
protected  void drawSquare(int r, int c, java.awt.Graphics g)
          Helper method for drawing the contents of one square.
protected  void drawThickRectangle(int x, int y, int w, int h, int t, java.awt.Color c, java.awt.Graphics g)
          Helper method for drawing a rectangle with a specified with and color.
 int getHighlightColumn()
          Get column number of currently highlighted square.
 int getHighlightRow()
          Get row number of currently highlighted square.
 java.awt.Dimension getMaximumSize()
          Get maximum size for component.
 java.awt.Dimension getMinimumSize()
          Get minimum size for component.
 java.awt.Dimension getPreferredSize()
          Get preferred size for component.
 void highLight(int r, int c)
          Highlight one square.
protected  void highLight(int r, int c, java.awt.Graphics g)
          Helper method for highlighting a square.
 void markDirty()
          Mark that the current offscreen image should be recreated completely.
protected  void markRegion(int x, int y, int w, int h)
          Update clipping region.
 java.awt.Dimension maximumSize()
          Get maximum size for component.
 java.awt.Dimension minimumSize()
          Get minimum size for component.
protected  void newOffscreenImage()
          Helper method for creating an offscreen image.
 void paint(java.awt.Graphics g)
          Main drawing method.
 java.awt.Dimension preferredSize()
          Get preferred size for component.
 void removeFoobaseMouseListener(IsFoobaseMouseListener ear)
          Unregister an IsFoobaseMouseListener client object.
protected static void say(java.lang.String s)
          Log a message.
 void setBoard(Board board)
          Set new board to use from now on.
 void setBounds(int x, int y, int w, int h)
          Set new location and size for component.
 void setBounds(java.awt.Rectangle r)
          Set new location and size for component.
 void setFixedSize(int w, int h)
          Set new dimensions for component.
 void unHighLight()
          Undo any highlighting currently going on.
 void update(java.awt.Graphics g)
          Update the component.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_pcs

protected java.awt.Image[] _pcs
Images to use for drawing of pieces.

The first six are white pieces, next six are black pieces.

The order for the pieces per color is:

  1. king
  2. queen
  3. knight
  4. bishop
  5. rook
  6. pawn

_maxh

protected int _maxh
Maximum height used for any piece

_light

protected java.awt.Color _light
Color to use for drawing a light square

_dark

protected java.awt.Color _dark
Color to use for drawing a dark square

_high

protected java.awt.Color _high
Color to use for highlighting square

_border

protected java.awt.Color _border
Color to use for drawing a border aroung the board

For now, this color is always black.


_size

protected java.awt.Dimension _size
Board dimensions.

_x0

protected int _x0
X-offset of left upper corner of the board inside its border.

If this value is not yet set by a paint operation, it is set to -1


_y0

protected int _y0
Y-offset of left upper corner of the board inside its border.

If this value is not yet set by a paint operation, it is set to -1


_w

protected int _w
Width of each board square.

If this value is not yet set by a paint operation, it is set to -1


_h

protected int _h
Height of each board square.

If this value is not yet set by a paint operation, it is set to -1


_board

protected Board _board
Board setup to draw

_txtfont

protected java.awt.Font _txtfont
Font to use for drawing row numbers and column letters

For now, no board decoration is implemented.


_offscreen

protected java.awt.Image _offscreen
Offscreen image on which actual drawing takes place.

_offgraph

protected java.awt.Graphics _offgraph
Offscreen Graphics

_hirow

protected int _hirow
Row number of currently highlighted square (A1 is on row zero)

_hicol

protected int _hicol
Column number of currently highlighted square (A1 is on column zero)

_ears

protected java.util.Vector _ears
List of IsFoobaseMouseListener objects that must be notified in case we generate a FoobaseMouseEvent

_ulcx

protected int _ulcx
Upper left corner X-coordinate for clipping area

_ulcy

protected int _ulcy
Upper right corner Y-coordinate for clipping area

_brcx

protected int _brcx
Bottom right corner X-coordinate of clipping area

_brcy

protected int _brcy
Bottom right corner Y-coordinate of clipping area

_oldsetup

protected byte[] _oldsetup
Local copy of pieces setup as drawn in offscreen image.

__version

public static final java.lang.String __version
Constructor Detail

DisplayBoard

public DisplayBoard()
Default public constructor.

DisplayBoard

public DisplayBoard(java.awt.Image[] pcs)
Public constructor.

The pieces image array contains 12 elements: first six white pieces, followed by six black pieces. The order of the pieces for each color is:

  1. king
  2. queen
  3. knight
  4. bishop
  5. rook
  6. pawn

Also note that none of the input parameters is actually copied into local properties, but rather a reference to them is stored locally.

One implication of this approach is that if the caller modifies the board later on (e.g. by executing a move on the position), this object will automatically have the new board setup available too.

Parameters:
pcs - Image[] containing the images to use for drawing the different pieces. Note that the images must be available for immediate usage, that is: use a MediaTracker to ensure that all images have been properly loaded.

DisplayBoard

public DisplayBoard(java.awt.Image[] pcs,
                    Board board)
Public constructor.

The pieces image array contains 12 elements: first six white pieces, followed by six black pieces. The order of the pieces for each color is:

  1. king
  2. queen
  3. knight
  4. bishop
  5. rook
  6. pawn

Also note that none of the input parameters is actually copied into local properties, but rather a reference to them is stored locally.

One implication of this approach is that if the caller modifies the board later on (e.g. by executing a move on the position), this object will automatically have the new board setup available too.

Parameters:
pcs - Image[] containing the images to use for drawing the different pieces. Note that the images must be available for immediate usage, that is: use a MediaTracker to ensure that all images have been properly loaded.
board - Board containing the pieces setup.
Method Detail

say

protected static void say(java.lang.String s)
Log a message.
Parameters:
s - String to log

setBoard

public void setBoard(Board board)
Set new board to use from now on.
Parameters:
board - Board to use from now on.

paint

public void paint(java.awt.Graphics g)
Main drawing method.
Overrides:
paint in class java.awt.Component
Parameters:
g - Graphics to draw the board on.

update

public void update(java.awt.Graphics g)
Update the component.

The main reason for implementing this method is to avoid clearing of the component on screen first.

Overrides:
update in class java.awt.Component
Parameters:
g - Graphics to draw the board on.

markDirty

public void markDirty()
Mark that the current offscreen image should be recreated completely.

setFixedSize

public void setFixedSize(int w,
                         int h)
Set new dimensions for component.
Parameters:
w - int new width for component
h - int new height for component

setBounds

public void setBounds(int x,
                      int y,
                      int w,
                      int h)
Set new location and size for component.
Overrides:
setBounds in class java.awt.Component
Parameters:
x - int new X-coordinate for component
y - int new Y-coordinate for component
w - int new width for component
h - int new height for component

setBounds

public void setBounds(java.awt.Rectangle r)
Set new location and size for component.
Overrides:
setBounds in class java.awt.Component
Parameters:
r - Rectangle containing the new location and size for the component.

getPreferredSize

public java.awt.Dimension getPreferredSize()
Get preferred size for component.
Overrides:
getPreferredSize in class java.awt.Component
Returns:
Dimension containing the preferred size.

preferredSize

public java.awt.Dimension preferredSize()
Get preferred size for component.
Overrides:
preferredSize in class java.awt.Component
Returns:
Dimension containing the preferred size.

getMinimumSize

public java.awt.Dimension getMinimumSize()
Get minimum size for component.
Overrides:
getMinimumSize in class java.awt.Component
Returns:
Dimension containing the minimum size.

minimumSize

public java.awt.Dimension minimumSize()
Get minimum size for component.
Overrides:
minimumSize in class java.awt.Component
Returns:
Dimension containing the minimum size.

getMaximumSize

public java.awt.Dimension getMaximumSize()
Get maximum size for component.
Overrides:
getMaximumSize in class java.awt.Component
Returns:
Dimension containing the maximum size.

maximumSize

public java.awt.Dimension maximumSize()
Get maximum size for component.
Returns:
Dimension containing the maximum size.

highLight

public void highLight(int r,
                      int c)
Highlight one square.

Note that this method does not draw the "normal" contents of the square but only the surrounding highlighting rectangle

Parameters:
r - int containing the row number of the square to highlight (bottom row is number zero)
c - int containing the column number of the square to highlight (leftmost column is column zero)

unHighLight

public void unHighLight()
Undo any highlighting currently going on.

It is safe to call this method even if nothing is currently being highlighted.


drawSquare

public void drawSquare(int r,
                       int c)
(Re)draw the contents of one square.
Parameters:
r - int containing the row number of the square to highlight (bottom row is number zero)
c - int containing the column number of the square to highlight (leftmost column is column zero)

getHighlightRow

public int getHighlightRow()
Get row number of currently highlighted square.
Returns:
int containing the zero-based row number (A1 is on row zero) of the currently highlighted square, or -1 if no sqaure is being highlighted.

getHighlightColumn

public int getHighlightColumn()
Get column number of currently highlighted square.
Returns:
int containing the zero-based column number (A1 is on column zero) of the currently highlighted square, or -1 if no sqaure is being highlighted.

addFoobaseMouseListener

public void addFoobaseMouseListener(IsFoobaseMouseListener ear)
Register an IsFoobaseMouseListener client object.
Specified by:
addFoobaseMouseListener in interface IsFoobaseMouseSource
Parameters:
ear - IsFoobaseMouseListener to notify if relevant events are generated.

removeFoobaseMouseListener

public void removeFoobaseMouseListener(IsFoobaseMouseListener ear)
Unregister an IsFoobaseMouseListener client object.
Specified by:
removeFoobaseMouseListener in interface IsFoobaseMouseSource
Parameters:
ear - IsFoobaseMouseListener to unregister

drawBoard

protected void drawBoard(java.awt.Graphics g)
Actual offscreen image drawing method.
Parameters:
g - Graphics to draw the board on.

newOffscreenImage

protected void newOffscreenImage()
Helper method for creating an offscreen image.

Implicit input is the _size property and implicit output are _mis and _pix

Normally, a new image is required after every resizing operation.


markRegion

protected void markRegion(int x,
                          int y,
                          int w,
                          int h)
Update clipping region.
Parameters:
x - int X-coordinate of changed region
y - int Y-coordinate of changed region
w - int width of changed region
h - int height of changed region

drawThickRectangle

protected void drawThickRectangle(int x,
                                  int y,
                                  int w,
                                  int h,
                                  int t,
                                  java.awt.Color c,
                                  java.awt.Graphics g)
Helper method for drawing a rectangle with a specified with and color.
Parameters:
x - int containing the X-coordinate of the upper left corner
y - int containing the Y-coordinate of the upper left corner
w - int containing the width of the rectangle (that is: the outer width of the rectangle, not the thickness of the lines)
h - int containing the height of the rectangle (that is: the outer height of the rectangle, not the thickness of the lines)
t - int containing the thickness of the rectangle to draw
c - Color to use for drawing
g - Graphics to draw on.

highLight

protected void highLight(int r,
                         int c,
                         java.awt.Graphics g)
Helper method for highlighting a square.

Note that this method does not draw the "normal" contents of the square but only the surrounding highlighting rectangle

Parameters:
r - int containing the row number of the square to highlight (bottom row is number zero)
c - int containing the column number of the square to highlight (leftmost column is column zero)
g - Graphics to draw on

drawSquare

protected void drawSquare(int r,
                          int c,
                          java.awt.Graphics g)
Helper method for drawing the contents of one square.
Parameters:
r - int containing the row number of the square to highlight (bottom row is number zero)
c - int containing the column number of the square to highlight (leftmost column is column zero)
g - Graphics to draw on