framework
Class Game

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by framework.Game
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Runnable, Accessible

public abstract class Game
extends Canvas
implements Runnable

Base class that all games should extend.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Canvas
Canvas.AccessibleAWTCanvas
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
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
Game()
          Constructs a new game.
Game(int width, int height)
          Constructs a new game with specified virtual size.
 
Method Summary
 void addGameEndListener(GameEndListener listener)
          Adds the specified game end listener to receive game end events from this Game.
 void addNotify()
          Overridden to start the game thread when added to a container.
 void dispose()
          Called just after the game loop ends.
abstract  void draw(Graphics2D g)
          Update the graphical display.
protected  void drawImpl()
          Sets up the graphics context and calls draw(Graphics2D).
 int getActingHeight()
          Get the height that the component thinks it is for the purposes of drawing.
 Dimension getActingSize()
          Get the size that the component thinks it is for the purposes of drawing.
 int getActingWidth()
          Get the width that the component thinks it is for the purposes of drawing.
protected  Graphics2D getBufferGraphics()
          Get a Graphics2D object that can be used to draw to the buffer.
 Dimension getPreferredSize()
          Overridden to return the virtual size if it is set.
 void init()
          Called just before the game loop is started.
 boolean isRunning()
           
 void processEndEvent(GameEndEvent e)
          Processes game end events by dispatching them to any registered GameEndListener objects.
 void processMouseEvent(MouseEvent e)
          Overridden to fix the mouse position if necessary.
 void processMouseMotionEvent(MouseEvent e)
          Overridden to fix the mouse position if necessary.
 void processMouseWheelEvent(MouseWheelEvent e)
          Overridden to fix the mouse position if necessary.
 void removeGameEndListener(GameEndListener listener)
          Removes the specified game end listener so that it no longer receives game end events from this Game.
 void removeNotify()
          Overridden to stop the game thread when removed from a container.
 void run()
          This method runs on the game thread and controls the game loop.
 void setActingSize(int width, int height)
          Sets the size that this component will pretend to be.
 void setVirtualSize(int width, int height)
          Deprecated.  
protected  void showBuffer()
          Makes the contents of the buffer visible.
abstract  void update(int time)
          Update the game state.
 
Methods inherited from class java.awt.Canvas
createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, paint, update
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Game

public Game()
Constructs a new game.


Game

public Game(int width,
            int height)
Constructs a new game with specified virtual size.

Method Detail

update

public abstract void update(int time)
Update the game state. Called once per iteration of the game loop.


draw

public abstract void draw(Graphics2D g)
Update the graphical display. Called once per iteration of the game loop.


init

public void init()
Called just before the game loop is started. The default implementation does nothing.


dispose

public void dispose()
Called just after the game loop ends. The default implementation does nothing.


isRunning

public boolean isRunning()

run

public void run()
This method runs on the game thread and controls the game loop.

Specified by:
run in interface Runnable

drawImpl

protected void drawImpl()
Sets up the graphics context and calls draw(Graphics2D). You can ignore this unless you want to override run().


getBufferGraphics

protected Graphics2D getBufferGraphics()
Get a Graphics2D object that can be used to draw to the buffer. You can ignore this unless you want to override drawImpl().


showBuffer

protected void showBuffer()
Makes the contents of the buffer visible. You can ignore this unless you want to override drawImpl().


processMouseEvent

public void processMouseEvent(MouseEvent e)
Overridden to fix the mouse position if necessary.

Overrides:
processMouseEvent in class Component

processMouseMotionEvent

public void processMouseMotionEvent(MouseEvent e)
Overridden to fix the mouse position if necessary.

Overrides:
processMouseMotionEvent in class Component

processMouseWheelEvent

public void processMouseWheelEvent(MouseWheelEvent e)
Overridden to fix the mouse position if necessary.

Overrides:
processMouseWheelEvent in class Component

processEndEvent

public void processEndEvent(GameEndEvent e)
Processes game end events by dispatching them to any registered GameEndListener objects.


addGameEndListener

public void addGameEndListener(GameEndListener listener)
Adds the specified game end listener to receive game end events from this Game. If the listener is null, no exception is thrown and no action is performed.

Parameters:
listener - the game end listener

removeGameEndListener

public void removeGameEndListener(GameEndListener listener)
Removes the specified game end listener so that it no longer receives game end events from this Game. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this component. If the listener is null, no exception is thrown and no action is performed.

Parameters:
listener - the game end listener

addNotify

public void addNotify()
Overridden to start the game thread when added to a container.

Overrides:
addNotify in class Canvas

removeNotify

public void removeNotify()
Overridden to stop the game thread when removed from a container.

Overrides:
removeNotify in class Component

getPreferredSize

public Dimension getPreferredSize()
Overridden to return the virtual size if it is set.

Overrides:
getPreferredSize in class Component

getActingSize

public Dimension getActingSize()
Get the size that the component thinks it is for the purposes of drawing. To get the physical size of the component, use Component.getSize().

See Also:
setActingSize(int, int)

getActingWidth

public int getActingWidth()
Get the width that the component thinks it is for the purposes of drawing. To get the physical width of the component, use Component.getWidth().

See Also:
setActingSize(int, int)

getActingHeight

public int getActingHeight()
Get the height that the component thinks it is for the purposes of drawing. To get the physical height of the component, use Component.getHeight().

See Also:
setActingSize(int, int)

setVirtualSize

public void setVirtualSize(int width,
                           int height)
Deprecated. 

Synonym for setActingSize(int, int).

See Also:
setActingSize(int, int)

setActingSize

public void setActingSize(int width,
                          int height)
Sets the size that this component will pretend to be. With this set, the game can ignore the physical size of the component and draw to it as though it had the specified width and height. Graphics will be automatically resized to fill the component, with a letterbox effect if the width/height ratio is different.

If either width or height are <= 0, this will unset both values.