Class AirHockey

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by AirHockey
All Implemented Interfaces:
KeyListener, ImageObserver, MenuContainer, Serializable, Runnable, EventListener, Accessible

public class AirHockey
extends Applet
implements Runnable, KeyListener

This is the main class for the Air Hockey applet. This game is very similar to the classic arcade game pong.

Version:
20050603
Author:
Colin Moore, Peter Gadfort
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.applet.Applet
Applet.AccessibleApplet
 
Nested classes/interfaces inherited from class java.awt.Panel
Panel.AccessibleAWTPanel
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static int COMPUTER_PLAYER
          Position of the computer player paddle.
static int GAMESPEED
          The speed at which the game is played.
static int GOAL_HEIGHT
          The height of the goals
static double GOAL_WIDTH
          Percent of the total width of the goal
static int HUMAN_PLAYER
          Position of the human player paddle This value is a percentage of the screen
static double LEFT_GOAL
          Percent of the left screeen not occupied by the goal
 
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
AirHockey()
           
 
Method Summary
 void checkCollision()
          Checks for a collision between the strikers and the puck.
 void checkScore()
          Checks if there has been a score or not
 void drawBackground(Graphics g)
          Draws the background for our game.
 void init()
          The init() method is the entry point for the game.
 void keyPressed(KeyEvent e)
          We must use this method because we "implement" KeyListener.
 void keyReleased(KeyEvent e)
          We must use this method because we "implement" KeyListener.
 void keyTyped(KeyEvent e)
          We must use this method because we "implement" KeyListener.
 void paint(Graphics g)
          Called automatically by our Component (our Applet).
 void resetGame()
          This is the code we want to execute when we reset the game.
 void run()
          The run() method is called when we create a thread in our applet.
 void stop()
          The stop method is called when the user navigates away from the webpage.
 void update(Graphics g)
          Depending on the value of our switch, we either call our special code, or just call the default code.
 void updateDoubleBufffered(Graphics g)
          Try to do the drawing to an offscreen buffer.
 
Methods inherited from class java.applet.Applet
destroy, getAccessibleContext, getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameter, getParameterInfo, isActive, newAudioClip, play, play, resize, resize, setStub, showStatus, start
 
Methods inherited from class java.awt.Panel
addNotify
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, 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, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

COMPUTER_PLAYER

public static final int COMPUTER_PLAYER
Position of the computer player paddle. This value is a percentage of the screen

See Also:
Constant Field Values

GAMESPEED

public static final int GAMESPEED
The speed at which the game is played. The higher the number the faster the refresh rate

See Also:
Constant Field Values

GOAL_HEIGHT

public static final int GOAL_HEIGHT
The height of the goals

See Also:
Constant Field Values

GOAL_WIDTH

public static final double GOAL_WIDTH
Percent of the total width of the goal

See Also:
Constant Field Values

HUMAN_PLAYER

public static final int HUMAN_PLAYER
Position of the human player paddle This value is a percentage of the screen

See Also:
Constant Field Values

LEFT_GOAL

public static final double LEFT_GOAL
Percent of the left screeen not occupied by the goal

See Also:
Constant Field Values
Constructor Detail

AirHockey

public AirHockey()
Method Detail

checkCollision

public void checkCollision()
Checks for a collision between the strikers and the puck. Makes the puck bounce if collision exists.


checkScore

public void checkScore()
Checks if there has been a score or not


drawBackground

public void drawBackground(Graphics g)
Draws the background for our game.

Parameters:
g - The Graphics object we use to draw.

init

public void init()
The init() method is the entry point for the game. AirHockey.java is the only *.java file that has this method in our program. It is called automatically by the Applet in place of a "main" method.

Overrides:
init in class Applet

keyPressed

public void keyPressed(KeyEvent e)
We must use this method because we "implement" KeyListener. This method gets called whenever any key is pressed. We then specify specific actions based on what keys we care about.

Specified by:
keyPressed in interface KeyListener
Parameters:
e - The object representing information about the Key Event

keyReleased

public void keyReleased(KeyEvent e)
We must use this method because we "implement" KeyListener. This method gets called whenever a key is released. We don't have any code to execute when this happens.

Specified by:
keyReleased in interface KeyListener
Parameters:
e - The object representing information about the Key Event

keyTyped

public void keyTyped(KeyEvent e)
We must use this method because we "implement" KeyListener. This method gets called whenever a key is typed (pressed and released). We don't have any code to execute when this happens.

Specified by:
keyTyped in interface KeyListener
Parameters:
e - The object representing information about the Key Event

paint

public void paint(Graphics g)
Called automatically by our Component (our Applet). This is called whenever the Applet thinks it needs to repaint itself, or whenever we call paint explicitly via the repaint() method.

Overrides:
paint in class Container
Parameters:
g - The Graphics object we use to draw

resetGame

public void resetGame()
This is the code we want to execute when we reset the game. It is called at the beginning of the game and whenever a score occurs.


run

public void run()
The run() method is called when we create a thread in our applet. Inside the run() method we want to loop until the applet is done or the user leaves.

Specified by:
run in interface Runnable

stop

public void stop()
The stop method is called when the user navigates away from the webpage. This is so that our game does not run unnecessarily. It is good to be kind to the user.

Overrides:
stop in class Applet

update

public void update(Graphics g)
Depending on the value of our switch, we either call our special code, or just call the default code.

Overrides:
update in class Container
Parameters:
g - The Graphics object we use to draw.

updateDoubleBufffered

public void updateDoubleBufffered(Graphics g)
Try to do the drawing to an offscreen buffer.

Parameters:
g - The Graphics object we use to draw.