info.gridworld.gui
Class GridPanel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by info.gridworld.gui.GridPanel
All Implemented Interfaces:
PseudoInfiniteViewport.Pannable, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible, javax.swing.Scrollable

public class GridPanel
extends javax.swing.JPanel
implements javax.swing.Scrollable, PseudoInfiniteViewport.Pannable

A GridPanel is a panel containing a graphical display of the grid occupants.
This code is not tested on the AP CS A and AB exams. It contains GUI implementation details that are not intended to be understood by AP CS students.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
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.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
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 java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
GridPanel(DisplayMap map, java.util.ResourceBundle res)
          Construct a new GridPanel object with no grid.
 
Method Summary
 Location getCurrentLocation()
          Gets the current location.
 java.awt.Dimension getMinimumSize()
          Returns the minimum size of the display, for use by layout manager.
 java.awt.Dimension getPreferredScrollableViewportSize()
           
 java.awt.Dimension getPreferredSize()
          Returns the desired size of the display, for use by layout manager.
 int getScrollableBlockIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
           
 boolean getScrollableTracksViewportHeight()
           
 boolean getScrollableTracksViewportWidth()
           
 int getScrollableUnitIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
           
 java.lang.String getToolTipText(java.awt.event.MouseEvent evt)
          Given a MouseEvent, determine what text to place in the floating tool tip when the the mouse hovers over this location.
 boolean isPannableUnbounded()
           
 Location locationForPoint(java.awt.Point p)
          Given a Point determine which grid location (if any) is under the mouse.
 void moveLocation(int dr, int dc)
          Moves the current location by a given amount.
 void paintComponent(java.awt.Graphics g)
          Paint this component.
 void panBy(int hDelta, int vDelta)
           
 java.awt.Point pointForLocation(Location loc)
           
 void recenter(Location loc)
          Pans the display back to the origin, so that 0, 0 is at the the upper left of the visible viewport.
 void setCurrentLocation(Location loc)
          Sets the current location.
 void setGrid(Grid<?> gr)
          Sets the grid being displayed.
 void setToolTipsEnabled(boolean flag)
          Enables/disables showing of tooltip giving information about the occupant beneath the mouse.
 void showPanTip()
          Shows a tool tip over the upper left corner of the viewport with the contents of the pannable view's pannable tip text (typically a string identifiying the corner point).
 void showTip(java.lang.String tipText, java.awt.Point pt)
          Show a tool tip.
 void zoomIn()
          Zooms in the display by doubling the current cell size.
 void zoomOut()
          Zooms out the display by halving the current cell size.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, 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, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
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, paintComponents, 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, addKeyListener, 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, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, 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
 

Constructor Detail

GridPanel

public GridPanel(DisplayMap map,
                 java.util.ResourceBundle res)
Construct a new GridPanel object with no grid. The view will be empty.

Method Detail

paintComponent

public void paintComponent(java.awt.Graphics g)
Paint this component.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - the Graphics object to use to render this component

setToolTipsEnabled

public void setToolTipsEnabled(boolean flag)
Enables/disables showing of tooltip giving information about the occupant beneath the mouse.

Parameters:
flag - true/false to enable/disable tool tips

setGrid

public void setGrid(Grid<?> gr)
Sets the grid being displayed. Reset the cellSize to be the largest that fits the entire grid in the current visible area (use default if grid is too large).

Parameters:
gr - the grid to display

getPreferredSize

public java.awt.Dimension getPreferredSize()
Returns the desired size of the display, for use by layout manager.

Overrides:
getPreferredSize in class javax.swing.JComponent
Returns:
preferred size

getMinimumSize

public java.awt.Dimension getMinimumSize()
Returns the minimum size of the display, for use by layout manager.

Overrides:
getMinimumSize in class javax.swing.JComponent
Returns:
minimum size

zoomIn

public void zoomIn()
Zooms in the display by doubling the current cell size.


zoomOut

public void zoomOut()
Zooms out the display by halving the current cell size.


recenter

public void recenter(Location loc)
Pans the display back to the origin, so that 0, 0 is at the the upper left of the visible viewport.


locationForPoint

public Location locationForPoint(java.awt.Point p)
Given a Point determine which grid location (if any) is under the mouse. This method is used by the GUI when creating Fish by clicking on cells in the display.

Parameters:
p - the Point in question (in display's coordinate system)
Returns:
the Location beneath the event (which may not be a valid location in the grid)

pointForLocation

public java.awt.Point pointForLocation(Location loc)

getToolTipText

public java.lang.String getToolTipText(java.awt.event.MouseEvent evt)
Given a MouseEvent, determine what text to place in the floating tool tip when the the mouse hovers over this location. If the mouse is over a valid grid cell. we provide some information about the cell and its contents. This method is automatically called on mouse-moved events since we register for tool tips.

Overrides:
getToolTipText in class javax.swing.JComponent
Parameters:
evt - the MouseEvent in question
Returns:
the tool tip string for this location

setCurrentLocation

public void setCurrentLocation(Location loc)
Sets the current location.

Parameters:
loc - the new location

getCurrentLocation

public Location getCurrentLocation()
Gets the current location.

Returns:
the currently selected location (marked with a bold square)

moveLocation

public void moveLocation(int dr,
                         int dc)
Moves the current location by a given amount.

Parameters:
dr - the number of rows by which to move the location
dc - the number of columns by which to move the location

showTip

public void showTip(java.lang.String tipText,
                    java.awt.Point pt)
Show a tool tip.

Parameters:
tipText - the tool tip text
pt - the pixel position over which to show the tip

getScrollableUnitIncrement

public int getScrollableUnitIncrement(java.awt.Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Specified by:
getScrollableUnitIncrement in interface javax.swing.Scrollable

getScrollableBlockIncrement

public int getScrollableBlockIncrement(java.awt.Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Specified by:
getScrollableBlockIncrement in interface javax.swing.Scrollable

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Specified by:
getScrollableTracksViewportWidth in interface javax.swing.Scrollable

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Specified by:
getScrollableTracksViewportHeight in interface javax.swing.Scrollable

getPreferredScrollableViewportSize

public java.awt.Dimension getPreferredScrollableViewportSize()
Specified by:
getPreferredScrollableViewportSize in interface javax.swing.Scrollable

panBy

public void panBy(int hDelta,
                  int vDelta)
Specified by:
panBy in interface PseudoInfiniteViewport.Pannable

isPannableUnbounded

public boolean isPannableUnbounded()
Specified by:
isPannableUnbounded in interface PseudoInfiniteViewport.Pannable

showPanTip

public void showPanTip()
Shows a tool tip over the upper left corner of the viewport with the contents of the pannable view's pannable tip text (typically a string identifiying the corner point). Tip is removed after a short delay.

Specified by:
showPanTip in interface PseudoInfiniteViewport.Pannable