edu.brook.ascape.view
Class CanvasView

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--edu.brook.ascape.view.CanvasView
Direct Known Subclasses:
BaseCellView, CounterView, SimplexView

public abstract class CanvasView
extends java.awt.Canvas
implements ComponentView

A base class for a canvas that acts as an observer of scapes. Provides for double buffer and notifiation of scape updates.

Since:
1.0
Version:
1.5
See Also:
Serialized Form

Field Summary
protected  boolean awaitingUpdate
          Has the buffer been modified? Are we waiting for the paint method to update it to the onscreen image?
protected  java.awt.Graphics bufferedGraphics
          The garphics for double-buffering.
protected  java.awt.Image bufferedImage
          The image for double-buffering.
protected  int currentUpdate
          The update currently being drawn.
protected  int drawEveryNUpdates
          Specifies the number of updates that occur between each draw.
protected  ViewFrameBridge frame
          The frame this view is displayed in.
protected  java.lang.String name
          The name of the view.
protected  GraphicsRecorder recorder
          A recorder for the graphic of this view.
protected  boolean recordNext
          Are we waiting for a record to occur?
protected  Scape scape
          The scape being viewed.
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
CanvasView()
          Constructs a canvas view.
CanvasView(java.lang.String name)
          Constructs a canvas view.
 
Method Summary
 void addNotify()
          Override addNotify to build buffer.
 void addScape(Scape scape)
          Add a scape to this listener.
 void build()
          Prepare the component view, once the view's scape has been created.
 void finalize()
          Attempts to free buffer memory as soon as possible upon canvas destruction.
 int getDrawEveryNUpdates()
          Returns a name for the view as defined by set name.
 GraphicsRecorder getGraphicsRecorder()
          Returns a recorder that can be used to record the graphics of this view.
 java.lang.String getName()
          Returns a name for the view as defined by set name.
 java.awt.Dimension getPreferredSize()
          Returns default preferred size of width 300 and height 200.
 Scape getScape()
          Returns the scape this canvas views.
 ViewFrameBridge getViewFrame()
          Returns the frame this view occupies, null if none.
protected  void initializeBuffer(int width, int height)
          Initialize the buffer to the supplied width and height.
 void onIterate()
          Called when scape is iterated.
 void onSetup()
          Method called when the scape is ready for setup.
 void onStart()
          Method called when the scape is started.
 void onStop()
          Method called when the scape is stopped.
 void onUpdate()
          Called when scape reports an update event.
 void paint(java.awt.Graphics g)
          Paints the canvas.
 void removeScape(Scape scape)
          Removes the scape from this listener.
 void scapeUpdated(ScapeEvent scapeEvent)
          Notifies this view that its scape has been updated.
 void setDrawEveryNUpdates(int drawEveryNUpdates)
          Sets the name of this view.
 void setGraphicsRecorder(GraphicsRecorder recorder)
          Sets the recorder that can be used to record the graphics of this view.
 void setName(java.lang.String name)
          Sets the name of this view.
 void setViewFrame(ViewFrameBridge frame)
          Sets the frame this view appears in.
 java.lang.String toString()
          Returns a short description of this view.
 void update(java.awt.Graphics g)
          Override the Canvas update method so that the canvas is not automatically cleared before painting.
 void updated()
          Called (typically from paint) when this view has been updated.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getParent, getPeer, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, 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, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setSize, setSize, setVisible, show, show, size, transferFocus, validate
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

bufferedImage

protected transient java.awt.Image bufferedImage
The image for double-buffering.

bufferedGraphics

protected transient java.awt.Graphics bufferedGraphics
The garphics for double-buffering.

scape

protected Scape scape
The scape being viewed.

frame

protected ViewFrameBridge frame
The frame this view is displayed in. Null if this view isn't in a frame.

recorder

protected GraphicsRecorder recorder
A recorder for the graphic of this view. Usually null, unless this view is being recorded.

awaitingUpdate

protected boolean awaitingUpdate
Has the buffer been modified? Are we waiting for the paint method to update it to the onscreen image?

drawEveryNUpdates

protected int drawEveryNUpdates
Specifies the number of updates that occur between each draw. Default is 1, so that every update is drawn.

currentUpdate

protected int currentUpdate
The update currently being drawn.

name

protected java.lang.String name
The name of the view.

recordNext

protected boolean recordNext
Are we waiting for a record to occur?
Constructor Detail

CanvasView

public CanvasView()
Constructs a canvas view.

CanvasView

public CanvasView(java.lang.String name)
Constructs a canvas view.
Parameters:
name - a user relevant name for this view
Method Detail

build

public void build()
Prepare the component view, once the view's scape has been created.
Specified by:
build in interface ComponentView

addNotify

public void addNotify()
Override addNotify to build buffer.
Overrides:
addNotify in class java.awt.Canvas

initializeBuffer

protected void initializeBuffer(int width,
                                int height)
Initialize the buffer to the supplied width and height. Used by scapeUpdated.
Parameters:
width - the width of the buffer
height - the height of the buffer

getPreferredSize

public java.awt.Dimension getPreferredSize()
Returns default preferred size of width 300 and height 200.
Overrides:
getPreferredSize in class java.awt.Component

getScape

public Scape getScape()
Returns the scape this canvas views.
Specified by:
getScape in interface ComponentView

addScape

public void addScape(Scape scape)
              throws java.util.TooManyListenersException
Add a scape to this listener. A listener can be assigned only one scape.
Parameters:
scape - the scape to make this canvas the observer of
Throws:
java.util.TooManyListenersException - on attempt to add a scape when one is allready added

removeScape

public void removeScape(Scape scape)
Removes the scape from this listener. Null expected.
Parameters:
scape - the scape to remove

getViewFrame

public ViewFrameBridge getViewFrame()
Returns the frame this view occupies, null if none.
Specified by:
getViewFrame in interface ComponentView

setViewFrame

public void setViewFrame(ViewFrameBridge frame)
Sets the frame this view appears in.
Specified by:
setViewFrame in interface ComponentView

onIterate

public void onIterate()
Called when scape is iterated.

onSetup

public void onSetup()
Method called when the scape is ready for setup.

onUpdate

public void onUpdate()
Called when scape reports an update event.

onStart

public void onStart()
Method called when the scape is started.

onStop

public void onStop()
Method called when the scape is stopped.

scapeUpdated

public void scapeUpdated(ScapeEvent scapeEvent)
Notifies this view that its scape has been updated. This view then has a chance to update itself, and this super method then notifies the scape that the view itself has been updated. By default, calls the onStart, onUpdate, or onStop method as appropriate, and then notifies scape.
Parameters:
scapeEvent - a scape event update

updated

public void updated()
Called (typically from paint) when this view has been updated. The view calls its observed scape and notifies it that the view has finished updating its state.

getDrawEveryNUpdates

public int getDrawEveryNUpdates()
Returns a name for the view as defined by set name.

setDrawEveryNUpdates

public void setDrawEveryNUpdates(int drawEveryNUpdates)
Sets the name of this view.
Parameters:
name - a user relevant name for this view

getName

public java.lang.String getName()
Returns a name for the view as defined by set name.
Overrides:
getName in class java.awt.Component

setName

public void setName(java.lang.String name)
Sets the name of this view.
Parameters:
name - a user relevant name for this view
Overrides:
setName in class java.awt.Component

getGraphicsRecorder

public GraphicsRecorder getGraphicsRecorder()
Returns a recorder that can be used to record the graphics of this view. If null (default, and typical), there is no recorder for this view.
Specified by:
getGraphicsRecorder in interface ComponentView

setGraphicsRecorder

public void setGraphicsRecorder(GraphicsRecorder recorder)
Sets the recorder that can be used to record the graphics of this view. If null, do not record.
Specified by:
setGraphicsRecorder in interface ComponentView

toString

public java.lang.String toString()
Returns a short description of this view. Sames as name unless overridden.
Overrides:
toString in class java.awt.Component

update

public void update(java.awt.Graphics g)
Override the Canvas update method so that the canvas is not automatically cleared before painting. (Clearing is unneccesary, because we are repainting entire canvas from buffer.)
Overrides:
update in class java.awt.Component

paint

public void paint(java.awt.Graphics g)
Paints the canvas. Ordinarily, you should not do painting here, but into the buffer in the scape updated method. Paint draws the buffer into the canvas. By default, we just copy the buffer in. Override to provide some other kind of behavior, making sure to call this super method, so that the view can report that it has been updated. Otherwise, the whole model will stop, waiting in vain for this view to update.
Specified by:
paint in interface ComponentView
Parameters:
g - the graphics context.
Overrides:
paint in class java.awt.Canvas

finalize

public void finalize()
Attempts to free buffer memory as soon as possible upon canvas destruction.
Overrides:
finalize in class java.lang.Object

(c) 1998-2000 The Brookings Insitution
Webpage