Class MarbleGameCanvas

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--MarbleGameCanvas
All Implemented Interfaces:
javax.accessibility.Accessible, DoubleBufferedComponent, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class MarbleGameCanvas
extends java.awt.Canvas
implements DoubleBufferedComponent

MarbleGameCanvas is the canvas object which all the drawings and animations of the marble game takes place. Double-buffering is handled by the DoubleBufferHandler. Based on the PoolTable class implemented by Steve Revilak.

Version:
1.0
Author:
Andi Sutedja
See Also:
Serialized Form

Inner classes inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.AWTTreeLock
 
Field Summary
 ShooterMarble player0Marble
          The player 1's shooter.
 ShooterMarble player1Marble
          The player 2's shooter.
 
Fields inherited from class java.awt.Component
accessibleContext, actionListenerK, adjustmentListenerK, appContext, background, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, componentListener, componentListenerK, componentOrientation, containerListenerK, cursor, dropTarget, enabled, eventMask, focusListener, focusListenerK, font, foreground, graphicsConfig, hasFocus, height, hierarchyBoundsListener, hierarchyBoundsListenerK, hierarchyListener, hierarchyListenerK, incRate, inputMethodListener, inputMethodListenerK, isInc, isPacked, itemListenerK, keyListener, keyListenerK, LEFT_ALIGNMENT, locale, LOCK, metrics, minSize, mouseListener, mouseListenerK, mouseMotionListener, mouseMotionListenerK, newEventsOnly, ownedWindowK, parent, peer, peerFont, popups, prefSize, RIGHT_ALIGNMENT, textListenerK, TOP_ALIGNMENT, valid, visible, width, windowClosingException, windowListenerK, x, y
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
MarbleGameCanvas()
           
 
Method Summary
 boolean changeTurn()
          Check if a player's turn has been changed.
 void clearMarks()
          Clear the marks on this canvas.
 int getBorderWidth()
          Get the width of the border.
 void initCanvas(Player p0, Player p1, java.awt.Image image)
          Initialize this canvas.
 boolean isGameOver()
          Check if the game is over.
 void paint(java.awt.Graphics g)
          Draw objects on the given Graphics.
 void paintFrame(java.awt.Graphics g)
          Update drawing/animation frame on the given Graphics object.
 void resetCanvas()
          Reset this canvas to the game start state.
 void resetLagCanvas()
          Reset this canvas to the Lagging session state.
 void setChangeTurn(boolean flag)
          Set the changeTurn flag.
 int setMark(int x, int y)
          Ask this canvas to set a marker at the given coordinates.
 void shootMarble(int playerID)
          Shoot the marble.
 int shootShooterMarble(int playerID)
          For handling lag shots
 void update(java.awt.Graphics g)
          Update the drawing of the objects on the given Graphics for double-buffering.
 
Methods inherited from class java.awt.Canvas
, addNotify, constructComponentName, getAccessibleContext, postsOldMouseEvents
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, areInputMethodsEnabled, bounds, checkGD, checkImage, checkImage, checkWindowClosingException, coalesceEvents, contains, contains, createChildHierarchyEvents, createHierarchyEvents, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, dispatchEventImpl, doLayout, enable, enable, enableEvents, enableInputMethods, eventEnabled, firePropertyChange, getAccessibleIndexInParent, getAccessibleStateSet, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getInputContext, getInputMethodRequests, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen_NoTreeLock, getLocationOnScreen, getMaximumSize, getMinimumSize, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getPreferredSize, getSize, getSize, getToolkit, getToolkitImpl, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isEnabledImpl, isFocusTraversable, isLightweight, isOpaque, isRecursivelyVisible, isShowing, isValid, isVisible, keyDown, keyUp, layout, lightweightPaint, lightweightPrint, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, numListening, paintAll, paintHeavyweightComponents, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, printHeavyweightComponents, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, resetGC, reshape, resize, resize, setBackground, setBounds, setBounds, 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
 
Methods inherited from interface DoubleBufferedComponent
createImage, getSize
 

Field Detail

player0Marble

public ShooterMarble player0Marble
The player 1's shooter.

player1Marble

public ShooterMarble player1Marble
The player 2's shooter.
Constructor Detail

MarbleGameCanvas

public MarbleGameCanvas()
Method Detail

initCanvas

public void initCanvas(Player p0,
                       Player p1,
                       java.awt.Image image)
Initialize this canvas.
Parameters:
p0 - the Player (player 1) for the game
p1 - the Player (player 2) for the game
image - the image to be drawn (in this case, the ring image)

paint

public void paint(java.awt.Graphics g)
Draw objects on the given Graphics.
Overrides:
paint in class java.awt.Canvas
Parameters:
g - the Graphics object

update

public void update(java.awt.Graphics g)
Update the drawing of the objects on the given Graphics for double-buffering.
Overrides:
update in class java.awt.Component
Parameters:
g - the Graphics object

paintFrame

public void paintFrame(java.awt.Graphics g)
Update drawing/animation frame on the given Graphics object.
Specified by:
paintFrame in interface DoubleBufferedComponent
Parameters:
g - the Graphics object.

getBorderWidth

public int getBorderWidth()
Get the width of the border.
Returns:
the width of the border

setChangeTurn

public void setChangeTurn(boolean flag)
Set the changeTurn flag.
Parameters:
flag - the changeTurn flag to set to

changeTurn

public boolean changeTurn()
Check if a player's turn has been changed.
Returns:
true if a player's turn has been changed, false otherwise

isGameOver

public boolean isGameOver()
Check if the game is over.
Returns:
true if the game is over, false otherwise.

resetCanvas

public void resetCanvas()
Reset this canvas to the game start state.

resetLagCanvas

public void resetLagCanvas()
Reset this canvas to the Lagging session state.

setMark

public int setMark(int x,
                   int y)
Ask this canvas to set a marker at the given coordinates. Return the number of marks on the canvas (including the one we just set). These are the marks used to position shots. There may be 0, 1, or 2 set at any given time. If two marks are set, do nothing. otherwise, test to see if the mark is within canvas boundaries, and set it.
Parameters:
x - the x coordinate of the mark
y - the y coordinate of the mark
Returns:
the number of marks on the canvas

clearMarks

public void clearMarks()
Clear the marks on this canvas.

shootMarble

public void shootMarble(int playerID)
Shoot the marble. We project a line pointing from the second point to the first point, and see if that vector strikes the shooter marble. If so, we have the marbles in play. Marbles other than the shooter marbles are ignored. To avoid rounding funniness near the perpendiculars, we treat the path of the shot as a vector of increasing length that moves away from the first mark, where the direction is equal to the direction between the second and the first marks.
Parameters:
playerID - the ID of the Player.

shootShooterMarble

public int shootShooterMarble(int playerID)
For handling lag shots
Parameters:
playerID - the ID of the Player
Returns:
the y coordinate of the shooter