Contest1
Class GameBoard

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JPanel
                          |
                          +--Contest1.GameBoard

class GameBoard
extends JPanel
implements Runnable

A class that represents a Game Board.

See Also:
Serialized Form

Inner Class Summary
(package private)  class GameBoard.Mlistener
          A class that handles mouse events on the board.
 
Inner classes inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Inner classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent, JComponent.IntVector, JComponent.KeyboardBinding, JComponent.KeyboardState
 
Inner classes inherited from class java.awt.Component
Component.AWTTreeLock
 
Field Summary
private  Rectangle boundary
          The boundary of the virtual board.
private  int ccols
          The number of visible cell columns.
private  Color cell_color
          The color of the cells.
private  Cell[][] cells
          The board's cells.
private  int crows
          The number of visible cell rows.
private  long generation
          The current generation of the board.
private  Rectangle[] row_rect
          An array of rectangles that describes the boundary of each cell row.
private  Thread t
          Animation thread.
private  boolean threadSuspended
          State of the animation thread.
private  int vcols
          The number of virtual cell columns.
private  int vrows
          The number of virtual cell rows.
 
Fields inherited from class javax.swing.JPanel
defaultLayout, uiClassID
 
Fields inherited from class javax.swing.JComponent
_bounds, accessibleContext, alignmentX, alignmentY, ANCESTOR_USING_BUFFER, ancestorNotifier, autoscroller, border, changeSupport, clientProperties, flags, HAS_FOCUS, IS_DOUBLE_BUFFERED, IS_OPAQUE, IS_PAINTING_TILE, KEYBOARD_BINDINGS_KEY, listenerList, maximumSize, minimumSize, NEXT_FOCUS, paintImmediatelyClip, preferredSize, readObjectCallbacks, REQUEST_FOCUS_DISABLED, tmpRect, TOOL_TIP_TEXT_KEY, ui, uiClassID, UNDEFINED_CONDITION, vetoableChangeSupport, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
component, containerListener, containerSerializedDataVersion, dispatcher, layoutMgr, maxSize, ncomponents, serialVersionUID
 
Fields inherited from class java.awt.Component
actionListenerK, adjustmentListenerK, appContext, assert, background, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, changeSupport, componentListener, componentListenerK, componentOrientation, componentSerializedDataVersion, containerListenerK, cursor, dropTarget, enabled, eventMask, focusListener, focusListenerK, font, foreground, hasFocus, height, incRate, inputMethodListener, inputMethodListenerK, isInc, isPacked, itemListenerK, keyListener, keyListenerK, LEFT_ALIGNMENT, locale, LOCK, minSize, mouseListener, mouseListenerK, mouseMotionListener, mouseMotionListenerK, name, nameExplicitlySet, newEventsOnly, ownedWindowK, parent, peer, peerFont, popups, prefSize, RIGHT_ALIGNMENT, serialVersionUID, textListenerK, TOP_ALIGNMENT, valid, visible, width, windowListenerK, x, y
 
Constructor Summary
(package private) GameBoard()
          The default constructor.
(package private) GameBoard(int crows, int ccols)
          Constructor.
 
Method Summary
(package private)  void clearBoard()
          Clear the game board.
private  Rectangle getBoundary()
          Gets the boundary spanned by the virtual rows and columns.
(package private)  Cell[][] getCells()
          Gets this game board's cells.
(package private)  long getGeneration()
          Gets the current generation.
(package private)  int[] getVisibleRowColumnOffset()
          Gets the row and column offset of the origin of the visible portion of the game board.
(package private)  void newGeneration()
          Advances the board state one generation, obeying the rules of the game.
(package private)  void nextGeneration()
          Advances the game one generation.
 void paintComponent(Graphics g)
          Overrides paintComponent() of JPanel to handle painting the board's graphics Context.
(package private)  void pauseGame()
          Pauses the game thread.
(package private)  void resumeGame()
          Resumes the game thread.
 void run()
          Animate the play.
(package private)  void startGame()
          Starts the game thread.
(package private)  void stopGame()
          Stops the game thread.
 
Methods inherited from class javax.swing.JPanel
, getAccessibleContext, getUIClassID, paramString, updateUI, writeObject
 
Methods inherited from class javax.swing.JComponent
_paintImmediately, addAncestorListener, addNotify, addPropertyChangeListener, addVetoableChangeListener, adjustPaintFlags, alwaysOnTop, bindingForKeyStroke, computeVisibleRect, computeVisibleRect, contains, createToolTip, enableSerialization, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperties, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getFlag, getGraphics, getHeight, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, keyboardBindings, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paintWithBuffer, processComponentKeyEvent, processFocusEvent, processKeyBinding, processKeyBindings, processKeyBindingsForAllComponents, processKeyEvent, processMouseMotionEvent, putClientProperty, readObject, rectangleIsObscured, registerKeyboardAction, registerKeyboardAction, registerWithKeyboardManager, removeAncestorListener, removeNotify, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setEnabled, setFlag, setFont, setForeground, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVisible, shouldDebugGraphics, superProcessMouseMotionEvent, unregisterKeyboardAction, unregisterWithKeyboardManager, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyOrientation, countComponents, deliverEvent, dispatchEventImpl, dispatchEventToSelf, doLayout, eventEnabled, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents_NoClientCode, getComponents, getLayout, getMouseEventTarget, getWindow, initIDs, insets, invalidate, invalidateTree, isAncestorOf, layout, lightweightPrint, list, list, locate, minimumSize, nextFocus, paintComponents, postProcessKeyEvent, postsOldMouseEvents, preferredSize, preProcessKeyEvent, print, printComponents, printOneComponent, processContainerEvent, processEvent, proxyEnableEvents, proxyRequestFocus, remove, remove, removeAll, removeContainerListener, setFocusOwner, setLayout, transferFocus, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, areInputMethodsEnabled, bounds, checkImage, checkImage, coalesceEvents, constructComponentName, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getSize, getToolkit, getToolkitImpl, getTreeLock, getWindowForObject, gotFocus, handleEvent, hide, imageUpdate, inside, isDisplayable, isEnabled, isEnabledImpl, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processInputMethodEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

cells

private Cell[][] cells
The board's cells.
See Also:
Cell

crows

private int crows
The number of visible cell rows.

ccols

private int ccols
The number of visible cell columns.

vrows

private int vrows
The number of virtual cell rows.

vcols

private int vcols
The number of virtual cell columns.

row_rect

private Rectangle[] row_rect
An array of rectangles that describes the boundary of each cell row.
See Also:
Rectangle

boundary

private Rectangle boundary
The boundary of the virtual board.

cell_color

private Color cell_color
The color of the cells.

t

private Thread t
Animation thread.

threadSuspended

private boolean threadSuspended
State of the animation thread.

generation

private long generation
The current generation of the board.
Constructor Detail

GameBoard

GameBoard()
The default constructor. Simply calls the specific constructor with default number of rows and columns.

GameBoard

GameBoard(int crows,
          int ccols)
Constructor. The size of the cells is determined by the lesser of two ratios: preferred height divided by number of rows, or preferred width divided by number of columns/2.
Parameters:
crows - The number of cell rows for this board.
ccols - The number of cell columns for this board.
See Also:
Config
Method Detail

getBoundary

private Rectangle getBoundary()
Gets the boundary spanned by the virtual rows and columns.
Returns:
A rectangle describing the boundary.

startGame

void startGame()
Starts the game thread.

pauseGame

void pauseGame()
Pauses the game thread.

stopGame

void stopGame()
Stops the game thread.

resumeGame

void resumeGame()
Resumes the game thread.

nextGeneration

void nextGeneration()
Advances the game one generation.

getCells

Cell[][] getCells()
Gets this game board's cells.
Returns:
Two-dimensional array of cells.

getGeneration

long getGeneration()
Gets the current generation.

getVisibleRowColumnOffset

int[] getVisibleRowColumnOffset()
Gets the row and column offset of the origin of the visible portion of the game board.
Returns:
Row index is eturned in first array element, column index in second.

paintComponent

public void paintComponent(Graphics g)
Overrides paintComponent() of JPanel to handle painting the board's graphics Context.
Parameters:
g - Graphics context of this board.
Overrides:
paintComponent in class JComponent

clearBoard

void clearBoard()
Clear the game board. This simply sets the isalive state of each cell to false.

newGeneration

void newGeneration()
Advances the board state one generation, obeying the rules of the game.
See Also:
JavaWorld Code Masters Programming Challenge

run

public void run()
Animate the play.
Specified by:
run in interface Runnable