simulation_MVC_Basic
Class View

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

public class View
extends java.awt.Canvas
implements ModelListener

This class encapsulates a very simple window in which an interactive game can be shown. This window provides the view of the game world, which is encapsulated by the game world model. This view depends upon the model. The model encapsulates the information about what is present in the game world (e.g., which sprites). This component encapsulates a ModelListener, and installs itself as an observer on the Model. This class also encapsulates being a KeyListener. This component's window dispatches events that are generated by the user pressing keys on the keyboard. A Controller component may install itself on the view in order to observe these events.

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
static java.util.logging.Logger logger
           
 
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
View(Model theModel)
          Construct a view of the world, using the passed Model as the source of information about the world.
 
Method Summary
 void changed()
          Method that is invoked when this listener wishes to respond or to react to a change that originated in whichever object this listener is listening to.
 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
 

Field Detail

logger

public static java.util.logging.Logger logger
Constructor Detail

View

public View(Model theModel)
Construct a view of the world, using the passed Model as the source of information about the world. Install itself on the Model as a listener, to detect when the underlying model has changed.

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()
Description copied from interface: ModelListener
Method that is invoked when this listener wishes to respond or to react to a change that originated in whichever object this listener is listening to. In this version of the listener, the interface stipulates precisely what this object is.

Specified by:
changed in interface ModelListener