org.jogre.client.awt
Class AbstractBoardComponent

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byorg.jogre.client.awt.JogreComponent
                  extended byorg.jogre.client.awt.AbstractBoardComponent
All Implemented Interfaces:
ImageObserver, MenuContainer, Observer, Serializable

public abstract class AbstractBoardComponent
extends JogreComponent
implements Observer

This graphical component can be extended to create a number of graphical boards such as chess boards, etc. Various attributes can be set such as the number of cells, the cell size and cell spacing (in pixels). Also the width of the border can be specified, and the option of drawing letters/text on the border. Also all the colours can be set of the board.

To use and override the paintComponent (Graphics g) method as follows:

 paintComponent (Graphics g) {
   // call paintComponent in AbstractBoardComponent which will draw an empty board
   super (g);

   // Insert specifiec custom graphic drawing here
 }
 

Version:
Alpha 0.2.3
Author:
Bob Marks
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  int borderWidth
          Width of the border in pixels.
protected  Color cellColour1
          Colour of cell one.
protected  Color cellColour2
          Colour of cell two.
protected  int cellSize
          Size of cell in pixels.
protected  int cellSpacing
          Spacing between cells in pixels.
static int DEFAULT_BORDER_WIDTH
          Default border width (15 pixels).
static int DEFAULT_CELL_SIZE
          Default cell size (36 pixels).
static int DEFAULT_CELL_SPACING
          Default cell spacing (1 pixel).
static int DEFAULT_MOUSE_BORDER_WIDTH
          Default mouse border width (2 pixels).
protected  Point dragPoint
          Stores the value of where the point is being dragged.
protected  boolean isAlternateBackground
          Boolean to indicate alternate style background.
protected  boolean isDrawLetters
          If true letters/numbers are drawn on the left and bottom of the board.
protected  boolean isReversed
          If this variable is true the board will be flipped.
protected  Color mouseBorderColour
          Colour of the mouse border.
protected  int mouseBorderWidth
          Width of the mouse border.
protected  int numOfCols
          Number of columns in the board.
protected  int numOfRows
          Number of rows in the board.
static Point OFF_SCREEN_POINT
          Constant to show a cell point is off the board (not selected).
protected  Point pressedPoint
          Stores the value of a mouse down.
protected  Color spacingColour
          Colour of spacing colour.
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
AbstractBoardComponent(int numOfRows, int numOfCols)
          Default constructor which sets the number of rows and the number of columns and everything else uses it default value.
AbstractBoardComponent(int numOfRows, int numOfCols, int cellSize, int cellSpacing, int borderWidth, int mouseBorderWidth, boolean isReversed, boolean alternateBackground, boolean drawLetters)
          This constructor sets all the various things of a board such as its number of rows/columns, cell size, spacing between cells, border width, mouse border width and the option to draw letters down the side.
 
Method Summary
protected  void drawBoardBackground(Graphics g)
          Draws the board background (all the various cells).
protected  void drawBorder(Graphics g)
          This method wipes the board and draws the border.
 Point getBoardCoords(int screenX, int screenY)
          This methods returns a board co-ordinate (in the top-left) from a screen point.
 int getBorderWidth()
          Return the width of the border in pixels.
 int getCellSize()
          Return the size of each cell in pixels.
 int getCellSpacing()
          Return the spacing between each cell in pixels.
 Point getDragPoint()
          Sets the drag point of a mouse.
 Point getPressedPoint()
          Return the point where a user has pressed on the board.
 Point getScreenCoords(int boardX, int boardY)
          This methods returns a screen co-ordinate (in the top-left) from a board point.
 boolean isReversed()
          Returns true if a board is reversed.
 void paintComponent(Graphics g)
          Draws the board.
 void resetPoints()
          Reset the dragPoint and pressedPoint variables.
 void setColours(Color cellColour1, Color cellColour2, Color spacingColour, Color mouseBorderColour)
          Set up colours for this board.
 void setDragPoint(Point dragPoint)
          Sets the drag point.
 void setPressedPoint(Point pressedPoint)
          Sets pressed point in the board.
 void setReversed(boolean isReversed)
          Sets if a board is reversed or not.
 
Methods inherited from class org.jogre.client.awt.JogreComponent
setController, update
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Observer
update
 

Field Detail

OFF_SCREEN_POINT

public static final Point OFF_SCREEN_POINT
Constant to show a cell point is off the board (not selected).


DEFAULT_CELL_SIZE

public static final int DEFAULT_CELL_SIZE
Default cell size (36 pixels).

See Also:
Constant Field Values

DEFAULT_CELL_SPACING

public static final int DEFAULT_CELL_SPACING
Default cell spacing (1 pixel).

See Also:
Constant Field Values

DEFAULT_BORDER_WIDTH

public static final int DEFAULT_BORDER_WIDTH
Default border width (15 pixels).

See Also:
Constant Field Values

DEFAULT_MOUSE_BORDER_WIDTH

public static final int DEFAULT_MOUSE_BORDER_WIDTH
Default mouse border width (2 pixels).

See Also:
Constant Field Values

numOfRows

protected int numOfRows
Number of rows in the board.


numOfCols

protected int numOfCols
Number of columns in the board.


cellSize

protected int cellSize
Size of cell in pixels.


cellSpacing

protected int cellSpacing
Spacing between cells in pixels.


borderWidth

protected int borderWidth
Width of the border in pixels.


mouseBorderWidth

protected int mouseBorderWidth
Width of the mouse border.


isDrawLetters

protected boolean isDrawLetters
If true letters/numbers are drawn on the left and bottom of the board.


isAlternateBackground

protected boolean isAlternateBackground
Boolean to indicate alternate style background. i.e. chess style board.


isReversed

protected boolean isReversed
If this variable is true the board will be flipped.


cellColour1

protected Color cellColour1
Colour of cell one.


cellColour2

protected Color cellColour2
Colour of cell two.


spacingColour

protected Color spacingColour
Colour of spacing colour.


mouseBorderColour

protected Color mouseBorderColour
Colour of the mouse border.


pressedPoint

protected Point pressedPoint
Stores the value of a mouse down.


dragPoint

protected Point dragPoint
Stores the value of where the point is being dragged.

Constructor Detail

AbstractBoardComponent

public AbstractBoardComponent(int numOfRows,
                              int numOfCols)
Default constructor which sets the number of rows and the number of columns and everything else uses it default value.

Parameters:
numOfRows - Number of rows in the board.
numOfCols - Number of columns in the board.

AbstractBoardComponent

public AbstractBoardComponent(int numOfRows,
                              int numOfCols,
                              int cellSize,
                              int cellSpacing,
                              int borderWidth,
                              int mouseBorderWidth,
                              boolean isReversed,
                              boolean alternateBackground,
                              boolean drawLetters)
This constructor sets all the various things of a board such as its number of rows/columns, cell size, spacing between cells, border width, mouse border width and the option to draw letters down the side. Also a boolean can be set to indicate if the board is reversed.

Parameters:
numOfRows - Number of rows in the board.
numOfCols - Number of columns in the board.
cellSize - Size of each cell in pixels.
cellSpacing - Spacing between each cell in pixels.
borderWidth - Size of the border in pixels.
mouseBorderWidth - Size of the mouse border in pixels.
isReversed - Boolean to indicate if the board is displayed in reverse.
alternateBackground - Alternate style background e.g. chess style.
drawLetters - If true draw letters horizontally and numbers vertically.
Method Detail

paintComponent

public void paintComponent(Graphics g)
Draws the board. This method calls the drawBorder (Graphics g) and drawBoardBackground (Graphics g) methods. The drawBoardLetters (Graphics g) method is also called if the drawLetters boolean is set to true. To customise this board then each of these methods can be over written in the sub class.

See Also:
JComponent.paintComponent(java.awt.Graphics)

setColours

public final void setColours(Color cellColour1,
                             Color cellColour2,
                             Color spacingColour,
                             Color mouseBorderColour)
Set up colours for this board.

Parameters:
cellColour1 - Colour for the first cell colour.
cellColour2 - Colour for the second cell colour.
spacingColour - Colour of the spacing between the cells.
mouseBorderColour - Colour of the mouse border colour.

drawBoardBackground

protected void drawBoardBackground(Graphics g)
Draws the board background (all the various cells). This can be overwritten to draw images for example.

Parameters:
g - Graphics object.

drawBorder

protected void drawBorder(Graphics g)
This method wipes the board and draws the border. This can be overwritten in a sub class to draw custom borders.

Parameters:
g -

getScreenCoords

public Point getScreenCoords(int boardX,
                             int boardY)
This methods returns a screen co-ordinate (in the top-left) from a board point.

Parameters:
boardX - 0 < boardX < numOfColumns
boardY - 0 < boardY < numOfRows
Returns:
Screen co-ordinates fromm a board co-ordinate.

getBoardCoords

public Point getBoardCoords(int screenX,
                            int screenY)
This methods returns a board co-ordinate (in the top-left) from a screen point.

Parameters:
screenX - X co-ordinate in pixels.
screenY - Y co-ordinate in pixels.
Returns:
Return board co-ordinates

isReversed

public boolean isReversed()
Returns true if a board is reversed.

Returns:
If board is reversed (e.g. black side in chess).

getBorderWidth

public int getBorderWidth()
Return the width of the border in pixels.

Returns:
Pixel width of border.

getCellSize

public int getCellSize()
Return the size of each cell in pixels.

Returns:
Pixel size of cell.

getCellSpacing

public int getCellSpacing()
Return the spacing between each cell in pixels.

Returns:
Pixel size of cell spacing.

getPressedPoint

public Point getPressedPoint()
Return the point where a user has pressed on the board.

Returns:
Point where user has pressed.

getDragPoint

public Point getDragPoint()
Sets the drag point of a mouse.

Returns:
Point where user has dragged.

setReversed

public void setReversed(boolean isReversed)
Sets if a board is reversed or not.

Parameters:
isReversed - True if reversed (e.g. black side in chess).

setPressedPoint

public void setPressedPoint(Point pressedPoint)
Sets pressed point in the board.

Parameters:
pressedPoint - Point on board where pressed.

setDragPoint

public void setDragPoint(Point dragPoint)
Sets the drag point.

Parameters:
dragPoint - Point on board where dragged.

resetPoints

public void resetPoints()
Reset the dragPoint and pressedPoint variables.