uchicago.src.sim.gui
Class DisplaySurface

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by uchicago.src.sim.gui.DisplaySurface
All Implemented Interfaces:
java.awt.event.ComponentListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, SimEventListener, MediaProducer

public class DisplaySurface
extends javax.swing.JComponent
implements SimEventListener, MediaProducer, java.awt.event.ComponentListener

Handles the drawing of Displayables and the probing of probeables. Displayables are added to a DisplaySurface which is then responsible for drawing them, and handling probing (single left-click) of them. Displayables are drawn in the order they are added to the DisplaySurface. Typically, a DisplaySurface is created by the model and has displayables added to it. DisplaySurface also handles the creation of movies from and snapshots of displays.

When drawing discrete (cell-based displays) the actual drawing surface divided up into a number of cells equal to the DisplaySurface's width * the DisplaySuface's height (and in the future, the DisplaySurface's depth). Drawing at x, y coordinates via SimGraphics draws in the cell with these coordinates.

When drawing non-discrete displays(e.g Network2DDisplay), drawing is done to actual screen coordinates, where x and y refer to a screen coordinate.

Version:
$Revision: 1.27 $ $Date: 2005/08/05 18:15:23 $
Author:
Nick Collier (Modified by Michael J. North)
See Also:
Displayable, Probeable, Serialized Form

Nested Class Summary
 class DisplaySurface.DisplayableOrder
           
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  javax.swing.JMenuBar bar
          The menu bar for the DisplaySurface JFrame
protected  java.awt.Dimension defaultSize
           
protected  java.util.Hashtable displays
          Dictionary of the Displays contained by this DisplaySurface.
protected  javax.swing.event.MouseInputAdapter dsMouseAdapter
           
protected  java.awt.event.WindowAdapter dsWindowAdapter
           
protected  javax.swing.JFrame frame
          The JFrame in which this DisplaySurface is displayed.
protected  java.util.ArrayList keyListeners
          List of keyListeners for this DisplaySurface.
protected  Legend legend
           
protected  java.awt.Point location
           
protected  javax.swing.JMenu menu
          The option menu.
protected  SimModel model
          The model associated with this display surface.
protected  boolean mousePressed
          This is set to true via a mouse motion listener on the frame.
protected  MovieMaker movieMaker
           
protected  java.lang.String name
          The name of this display.
protected  boolean needsUpdate
          Whether or not this DisplaySurface needs to be updated.
protected  Painter painter
          The Painter used by this DisplaySurface to do the actual drawing.
protected  java.util.ArrayList probeables
          List of probeable displays.
protected  java.awt.Dimension size
           
protected  java.lang.String snapshotFile
          Filename for snapshots taken of this display surface.
protected  java.util.ArrayList zoomables
           
protected  uchicago.src.sim.gui.DisplaySurface.Rect zoomRect
           
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface uchicago.src.sim.gui.MediaProducer
MPEG, QUICK_TIME
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
DisplaySurface(java.awt.Dimension size, SimModel model, java.lang.String name)
          Creates a DisplaySurface of the specified size and with the specified model and the specified name.
DisplaySurface(SimModel model, java.lang.String name)
          Creates a DisplaySurface for the specified model and with the specified name.
DisplaySurface(SimModel model, java.lang.String name, Painter painter)
          Creates a DisplaySurface for the specified model, with the specified name and using the specified Painter.
 
Method Summary
 void addDisplayable(Displayable display, java.lang.String name)
          Adds a Displayable to the list of displayables that are displayed when updateDisplay is called.
 void addDisplayable(Displayable display, java.lang.String name, int order)
          Adds a Displayable to the list of displayables that are displayed when updateDisplay is called.
 void addDisplayableProbeable(Displayable display, java.lang.String name)
          Adds the specified Displayable to the displayables list and the probeables list.
 void addKeyListener(java.awt.event.KeyListener listener)
           
 void addLegendLabel(java.lang.String label, int iconType, java.awt.Color color, boolean hollow)
           
 void addLegendLabel(java.lang.String label, int iconType, java.awt.Color color, boolean hollow, int iconWidth, int iconHeight)
           
 void addMovieFrame()
          Adds the currently displayed image as frame to a movie. setMovieName must be called before this method is called.
 void addProbeable(Probeable probeable)
          Adds the specified Probeable to the list of probeables.
 void addZoomable(Zoomable zoomable)
           
 void closeMovie()
          Closes the movie, writing any remaining frames to the file.
 void componentHidden(java.awt.event.ComponentEvent e)
           
 void componentMoved(java.awt.event.ComponentEvent e)
           
 void componentResized(java.awt.event.ComponentEvent e)
           
 void componentShown(java.awt.event.ComponentEvent e)
           
 void createLegend(java.lang.String title)
           
 void display()
          Displays this DisplaySurface, that is, makes it visible on the screen in a JFrame etc.
 void dispose()
          Dispose this DisplaySurface
 java.awt.Dimension getDefaultSize()
          Returns the default dimension of this DisplaySurface.
 javax.swing.JFrame getFrame()
          Returns the JFrame for this DisplaySurface.
 javax.swing.JMenu getOptionsMenu()
          Returns the options menu for this DisplaySurface.
 java.awt.Dimension getPreferredSize()
          Gets the preferred size of this DisplaySurface.
 boolean isFrameVisible()
           
 void paint(java.awt.Graphics g)
          Paints this surface.
 void paintAll(java.awt.Graphics g)
           
 void paintComponents(java.awt.Graphics g)
           
 void removeDisplayable(Displayable display)
          Removes the specified Displayable from the list of displayables to display
 void removeKeyListener(java.awt.event.KeyListener listener)
           
 void removeProbeable(Probeable probeable)
          Removes the specified Probeable from the list of probeables.
 void removeProbeableDisplayable(Displayable display)
          Removes the specified Displayable from the ist of displayables to display.
 void setBackground(java.awt.Color c)
          Sets the background color for this display
 void setLocation(int x, int y)
          Sets the screen location for this OpenGraph.
 void setMovieName(java.lang.String fileName, java.lang.String movieType)
          Sets the name and type of a movie.
 void setSnapshotFileName(java.lang.String fileName)
          Sets the snapshot file name.
 void simEventPerformed(SimEvent evt)
          Repaints the display on a pause or a stop event.
 void takeSnapshot()
          Takes a snapshot of the current displayed image and writes it to the file specified by setSnapshotFileName(String fileName).
 void update(java.awt.Graphics g)
           
 void updateDisplay()
          Updates the display.
 void updateDisplayDirect()
          Updates the display.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setName, setSize, setSize, 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

painter

protected Painter painter
The Painter used by this DisplaySurface to do the actual drawing.


probeables

protected java.util.ArrayList probeables
List of probeable displays.


frame

protected javax.swing.JFrame frame
The JFrame in which this DisplaySurface is displayed.


needsUpdate

protected boolean needsUpdate
Whether or not this DisplaySurface needs to be updated.


snapshotFile

protected java.lang.String snapshotFile
Filename for snapshots taken of this display surface.


model

protected SimModel model
The model associated with this display surface.


displays

protected java.util.Hashtable displays
Dictionary of the Displays contained by this DisplaySurface. The menu text associated with the display is the key and the display itself is the value.


menu

protected javax.swing.JMenu menu
The option menu.


bar

protected javax.swing.JMenuBar bar
The menu bar for the DisplaySurface JFrame


name

protected java.lang.String name
The name of this display.


movieMaker

protected MovieMaker movieMaker

legend

protected Legend legend

keyListeners

protected java.util.ArrayList keyListeners
List of keyListeners for this DisplaySurface.


zoomables

protected java.util.ArrayList zoomables

location

protected java.awt.Point location

size

protected java.awt.Dimension size

defaultSize

protected java.awt.Dimension defaultSize

mousePressed

protected boolean mousePressed
This is set to true via a mouse motion listener on the frame.


zoomRect

protected uchicago.src.sim.gui.DisplaySurface.Rect zoomRect

dsWindowAdapter

protected java.awt.event.WindowAdapter dsWindowAdapter

dsMouseAdapter

protected javax.swing.event.MouseInputAdapter dsMouseAdapter
Constructor Detail

DisplaySurface

public DisplaySurface(java.awt.Dimension size,
                      SimModel model,
                      java.lang.String name)
Creates a DisplaySurface of the specified size and with the specified model and the specified name. The size of the display surface should be equal to the size of the largest displayable is going to display. For example,
Object2DDisplay display = new Object2DDisplay(someGrid);
DisplaySurface ds = new DisplaySurface(display.getSize(), someModel, "Display");
. The name appears in the title bar of the actual screen window that contains the display.

Parameters:
size - the size of the DisplaySurface
model - the model associated with this display surface
name - the name that appears in the title bar of the physical display

DisplaySurface

public DisplaySurface(SimModel model,
                      java.lang.String name,
                      Painter painter)
Creates a DisplaySurface for the specified model, with the specified name and using the specified Painter. The name appears in the title bar of the actual screen window that contains the display.

Note that specifying your own Painter is an advanced feature and shouldn't be done unless you know what you are doing. Of course, if you've written your own Painter then you probably do.

Parameters:
model - the model associated with this display surface
name - the name that appears in the title bar of the physical display
painter - the Painter object used to do the actual painting of the Displayables contained by this DisplaySurface.

DisplaySurface

public DisplaySurface(SimModel model,
                      java.lang.String name)
Creates a DisplaySurface for the specified model and with the specified name. The name appears in the title bar of the actual screen window that contains the display.

Parameters:
model - the model associated with this display surface
name - the name that appears in the title bar of the physical display
Method Detail

setBackground

public void setBackground(java.awt.Color c)
Sets the background color for this display

Overrides:
setBackground in class javax.swing.JComponent
Parameters:
c - the background color

addDisplayable

public void addDisplayable(Displayable display,
                           java.lang.String name)
Adds a Displayable to the list of displayables that are displayed when updateDisplay is called.

Parameters:
display - the displayable to add
name - the name of the displayable (is shown under the view menu)

addDisplayable

public void addDisplayable(Displayable display,
                           java.lang.String name,
                           int order)
Adds a Displayable to the list of displayables that are displayed when updateDisplay is called.

Parameters:
display - the displayable to add
name - the name of the displayable (is shown under the view menu)
order - the integer specifying the display order of the displayable

removeDisplayable

public void removeDisplayable(Displayable display)
Removes the specified Displayable from the list of displayables to display

Parameters:
display - the displayable to remove

addProbeable

public void addProbeable(Probeable probeable)
Adds the specified Probeable to the list of probeables.

Parameters:
probeable - the probeable to add

removeProbeable

public void removeProbeable(Probeable probeable)
Removes the specified Probeable from the list of probeables.

Parameters:
probeable - the probeable to remove

removeProbeableDisplayable

public void removeProbeableDisplayable(Displayable display)
Removes the specified Displayable from the ist of displayables to display. If this Displayable is also a Probeable it is removed from that list as well.

Parameters:
display - the Displayable to remove

addZoomable

public void addZoomable(Zoomable zoomable)

getDefaultSize

public java.awt.Dimension getDefaultSize()
Returns the default dimension of this DisplaySurface. The default dimension is the size of the DisplaySurface before any persistent or remembered size is applied.


addDisplayableProbeable

public void addDisplayableProbeable(Displayable display,
                                    java.lang.String name)
Adds the specified Displayable to the displayables list and the probeables list.

Parameters:
display - the Displayable to add to this to DisplaySurface.
name - the name of the displayable to add. Name will be shown on the view menu
Throws:
java.lang.IllegalArgumentException - if the specified object is not a probable and a displayable

updateDisplay

public void updateDisplay()
Updates the display. Painting all the displayables to the screen. This method is typically added to the schedule with an interval of however often the user wants the display to refresh.


updateDisplayDirect

public void updateDisplayDirect()
Updates the display. Painting all the displayables to the screen. Use this when you want to update the display in response to a gui event: button press, key typed, etc. Use updateDisplay to update the display from within your model.


paint

public void paint(java.awt.Graphics g)
Paints this surface.

Overrides:
paint in class javax.swing.JComponent

paintComponents

public void paintComponents(java.awt.Graphics g)
Overrides:
paintComponents in class java.awt.Container

paintAll

public void paintAll(java.awt.Graphics g)
Overrides:
paintAll in class java.awt.Component

update

public void update(java.awt.Graphics g)
Overrides:
update in class javax.swing.JComponent

isFrameVisible

public boolean isFrameVisible()

setLocation

public void setLocation(int x,
                        int y)
Sets the screen location for this OpenGraph.

Overrides:
setLocation in class java.awt.Component
Parameters:
x - the x screen coordinate
y - the y screen coordinate

display

public void display()
Displays this DisplaySurface, that is, makes it visible on the screen in a JFrame etc.


dispose

public void dispose()
Dispose this DisplaySurface


createLegend

public void createLegend(java.lang.String title)

addLegendLabel

public void addLegendLabel(java.lang.String label,
                           int iconType,
                           java.awt.Color color,
                           boolean hollow)

addLegendLabel

public void addLegendLabel(java.lang.String label,
                           int iconType,
                           java.awt.Color color,
                           boolean hollow,
                           int iconWidth,
                           int iconHeight)

setMovieName

public void setMovieName(java.lang.String fileName,
                         java.lang.String movieType)
Sets the name and type of a movie. Currently type can only be DisplaySurface.QUICK_TIME.

Specified by:
setMovieName in interface MediaProducer
Parameters:
fileName - the name of the movie
movieType - the type of movie (e.g. DisplaySurface.QUICK_TIME)

addMovieFrame

public void addMovieFrame()
Adds the currently displayed image as frame to a movie. setMovieName must be called before this method is called.

Specified by:
addMovieFrame in interface MediaProducer

closeMovie

public void closeMovie()
Closes the movie, writing any remaining frames to the file. This must be called if making a movie.

Specified by:
closeMovie in interface MediaProducer

getOptionsMenu

public javax.swing.JMenu getOptionsMenu()
Returns the options menu for this DisplaySurface.


getFrame

public javax.swing.JFrame getFrame()
Returns the JFrame for this DisplaySurface.


setSnapshotFileName

public void setSnapshotFileName(java.lang.String fileName)
Sets the snapshot file name.

Specified by:
setSnapshotFileName in interface MediaProducer
Parameters:
fileName - then file name to record to image to.

takeSnapshot

public void takeSnapshot()
Takes a snapshot of the current displayed image and writes it to the file specified by setSnapshotFileName(String fileName). The name of the file has the tickcount appended to it, as well as an appropriate extension. For example, a fileName of 'SugarScape', and calling takeSnapShot every 25 ticks would generate files like the following: SugarScape25.gif, SugarScape50.gif, and so on.

Specified by:
takeSnapshot in interface MediaProducer

getPreferredSize

public java.awt.Dimension getPreferredSize()
Gets the preferred size of this DisplaySurface.

Overrides:
getPreferredSize in class javax.swing.JComponent

addKeyListener

public void addKeyListener(java.awt.event.KeyListener listener)
Overrides:
addKeyListener in class java.awt.Component

removeKeyListener

public void removeKeyListener(java.awt.event.KeyListener listener)
Overrides:
removeKeyListener in class java.awt.Component

simEventPerformed

public void simEventPerformed(SimEvent evt)
Repaints the display on a pause or a stop event. Consequently, adding a DisplaySurface as SimEventListener to a SimModel causes the display to update whenever a simulation run is paused or stopped.

Specified by:
simEventPerformed in interface SimEventListener

componentShown

public void componentShown(java.awt.event.ComponentEvent e)
Specified by:
componentShown in interface java.awt.event.ComponentListener

componentHidden

public void componentHidden(java.awt.event.ComponentEvent e)
Specified by:
componentHidden in interface java.awt.event.ComponentListener

componentMoved

public void componentMoved(java.awt.event.ComponentEvent e)
Specified by:
componentMoved in interface java.awt.event.ComponentListener

componentResized

public void componentResized(java.awt.event.ComponentEvent e)
Specified by:
componentResized in interface java.awt.event.ComponentListener