gameComponents
Class GameCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by gameComponents.GameCanvas
All Implemented Interfaces:
ModelListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class GameCanvas
extends java.awt.Canvas
implements ModelListener

This class encapsulates a very simple window in which our shooter game can be shown. This window provides the view of the game system. The view depends upon the model in order to "know" which sprites are present. At present, this class also encapsulates being an ActionListener and a KeyListener. This window is capable of listening for events that indicate it should be redrawn (in the case of multiple frames). This window is capable of listening to events that are generated by the user pressing keys on the keyboard. For these capabilities to actually be enacted, additional services must used by the event dispatcher (to connect their ability to dispatch events to this component's ability to listen)

Author:
mb
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.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
GameCanvas(java.awt.Dimension dim, SpriteDataModel theModel)
          Construct a game canvas with the passed dimensions
 
Method Summary
 void changed()
          The method alerts this view that the model upon which it depends has changed.
 void drawFrame(java.awt.Graphics2D g)
          This method implements the functionality so that the scene is drawn on the back buffer, and then the entire back buffer is shown at once.
 void drawOnScreenElements(java.awt.Graphics2D g)
          This method draws all of the required on-screen elements on the passed graphics context.
 void paint(java.awt.Graphics g)
          This method accomplishes the painting of this canvas.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, 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, getPreferredSize, 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, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, 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

GameCanvas

public GameCanvas(java.awt.Dimension dim,
                  SpriteDataModel theModel)
Construct a game canvas with the passed dimensions

Method Detail

paint

public void paint(java.awt.Graphics g)
This method accomplishes the painting of this canvas. This method is called when the contents of the component should be painted; such as when the component is first being shown or is damaged and in need of repair. It should be understood that the main method of an app typically does not directly invoke this method. Rather, the application indicates that the canvas is in need of painting via the invalidate() method, which signals that the canvas needs to be redrawn. For instance, when the game state changes (like the user has moved a sprite or fired a shot or whatever). The window manager, once alerted to the fact that a window needs to be redrawn, will trigger a process that will ultimately result in this method being invoked.

Overrides:
paint in class java.awt.Canvas

drawFrame

public void drawFrame(java.awt.Graphics2D g)
This method implements the functionality so that the scene is drawn on the back buffer, and then the entire back buffer is shown at once. This method accomplishes a performance improvement. Instead of the graphics being drawn repeatedly, for each time an on-screen element is specified (incrementally), the buffer is rendered all at once. Implementation note: this method delegates to another method the specification of what the screen elements are.

Parameters:
g - the Graphics2D object that is encapsulating the graphics context on which the on-screen elements are being drawn.

drawOnScreenElements

public void drawOnScreenElements(java.awt.Graphics2D g)
This method draws all of the required on-screen elements on the passed graphics context. Implementation note: the client should not assume that the graphics context corresponds to what is shown on-screen. In fact, it may be a graphics buffer that will be rendered at some other point.

Parameters:
g - as specified above

changed

public void changed()
The method alerts this view that the model upon which it depends has changed. As well, a statement is printed to the console "View detects that model has changed and signals it needs to be redrawn."

Specified by:
changed in interface ModelListener