public abstract class Board
extends javax.swing.JPanel
implements java.lang.Runnable, java.awt.event.KeyListener, java.awt.event.MouseListener
MainWindow
.
This class creates the game thread, consumes the mouse and keyboard
events and contains the game loop. The game loop periodically calls
the updateGame()
, drawBackground(Graphics)
and drawGame(Graphics)
methods.
A minimal game has to overwrite at least two methods:
updateGame()
to periodically update the state of
all game objects.drawGame(Graphics)
to draw the game objects.
When the game is over, the framework calls the
drawGameOver(Graphics)
to display a game over message.
You have to overwrite this method to control this message.
By default, the game background is monochrome and in the color provided
in the constructor. If you want to change this, overwrite the
drawBackground(Graphics)
method.
To ensure the desired performance of the game, the game loop measures the frames per second and skips the drawing if necessary to avoid a drop in the game performance. Nevertheless, the game update method is called.
If your game should use mouse or keyboard for interaction, you have to
register a mouse or keyboard listener using the
addMouseListener(MouseListener)
or the
addKeyListener(KeyListener)
methods.
javax.swing.JPanel.AccessibleJPanel
javax.swing.JComponent.AccessibleJComponent
Modifier and Type | Field and Description |
---|---|
protected long |
delay
Time for each iteration (frame) in nano seconds.
|
protected java.awt.Dimension |
dimension
Dimension of the board.
|
protected boolean |
gameRunning
Indicator that the game is still running.
|
protected java.lang.Thread |
thread
The thread.
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
Board(int delay,
java.awt.Dimension dimension,
java.awt.Color color)
Create a new board.
|
Board(int delay,
int width,
int height,
java.awt.Color color)
Create a new board.
|
Modifier and Type | Method and Description |
---|---|
void |
addKeyListener(java.awt.event.KeyListener l) |
void |
addMouseListener(java.awt.event.MouseListener l) |
void |
addNotify() |
protected void |
centerText(java.awt.Graphics g,
java.lang.String msg)
Draw a text centered on the screen.
|
protected void |
drawBackground(java.awt.Graphics g)
Draw the background.
|
abstract void |
drawGame(java.awt.Graphics g)
The graphics output of the game.
|
protected void |
drawGameOver(java.awt.Graphics g)
Draw game over message.
|
java.awt.Dimension |
getDimension()
Returns the dimension of the board.
|
int |
getHeight() |
int |
getWidth() |
void |
keyPressed(java.awt.event.KeyEvent e) |
void |
keyReleased(java.awt.event.KeyEvent e) |
void |
keyTyped(java.awt.event.KeyEvent e) |
void |
mouseClicked(java.awt.event.MouseEvent e) |
void |
mouseEntered(java.awt.event.MouseEvent e) |
void |
mouseExited(java.awt.event.MouseEvent e) |
void |
mousePressed(java.awt.event.MouseEvent e) |
void |
mouseReleased(java.awt.event.MouseEvent e) |
void |
run() |
void |
stopGame()
Stops the game.
|
abstract boolean |
updateGame()
The game loop for the game logic.
|
protected void |
writeText(java.awt.Graphics g,
int x,
int y,
java.lang.String msg)
Writes a text at the given position.
|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, 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, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
protected final long delay
protected final java.awt.Dimension dimension
protected volatile boolean gameRunning
protected java.lang.Thread thread
public Board(int delay, java.awt.Dimension dimension, java.awt.Color color)
delay
- delay between two frames in millisecondsdimension
- dimension of the boardcolor
- background color of the boardpublic Board(int delay, int width, int height, java.awt.Color color)
delay
- delay between two frames in millisecondswidth
- width of the board in pixelheight
- height of the board in pixelcolor
- background color of the boardpublic final void addKeyListener(java.awt.event.KeyListener l)
addKeyListener
in class java.awt.Component
public final void addMouseListener(java.awt.event.MouseListener l)
addMouseListener
in class java.awt.Component
public void addNotify()
addNotify
in class javax.swing.JComponent
JComponent.addNotify()
protected final void centerText(java.awt.Graphics g, java.lang.String msg)
g
- the graphics contextmsg
- the message to be shownprotected void drawBackground(java.awt.Graphics g)
g
- the graphics contextpublic abstract void drawGame(java.awt.Graphics g)
g
- Graphics context.protected void drawGameOver(java.awt.Graphics g)
g
- the graphics contextpublic final java.awt.Dimension getDimension()
public final int getHeight()
getHeight
in class javax.swing.JComponent
JComponent.getHeight()
public final int getWidth()
getWidth
in class javax.swing.JComponent
JComponent.getWidth()
public void keyPressed(java.awt.event.KeyEvent e)
keyPressed
in interface java.awt.event.KeyListener
KeyListener.keyPressed(java.awt.event.KeyEvent)
public void keyReleased(java.awt.event.KeyEvent e)
keyReleased
in interface java.awt.event.KeyListener
KeyListener.keyReleased(java.awt.event.KeyEvent)
public void keyTyped(java.awt.event.KeyEvent e)
keyTyped
in interface java.awt.event.KeyListener
KeyListener.keyTyped(java.awt.event.KeyEvent)
public void mouseClicked(java.awt.event.MouseEvent e)
mouseClicked
in interface java.awt.event.MouseListener
MouseListener.mouseClicked(java.awt.event.MouseEvent)
public void mouseEntered(java.awt.event.MouseEvent e)
mouseEntered
in interface java.awt.event.MouseListener
MouseListener.mouseEntered(java.awt.event.MouseEvent)
public void mouseExited(java.awt.event.MouseEvent e)
mouseExited
in interface java.awt.event.MouseListener
MouseListener.mouseExited(java.awt.event.MouseEvent)
public void mousePressed(java.awt.event.MouseEvent e)
mousePressed
in interface java.awt.event.MouseListener
MouseListener.mousePressed(java.awt.event.MouseEvent)
public void mouseReleased(java.awt.event.MouseEvent e)
mouseReleased
in interface java.awt.event.MouseListener
MouseListener.mouseReleased(java.awt.event.MouseEvent)
public final void run()
run
in interface java.lang.Runnable
Runnable.run()
public void stopGame()
public abstract boolean updateGame()
protected final void writeText(java.awt.Graphics g, int x, int y, java.lang.String msg)
g
- graphics contextx
- x position of the texty
- y position of the textmsg
- the message to be drawn