edu.southern.computing.oopj
Class Viewport

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Window
              extended by java.awt.Frame
                  extended by javax.swing.JFrame
                      extended by edu.southern.computing.oopj.Viewport
All Implemented Interfaces:
InteractiveArea, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.RootPaneContainer, javax.swing.WindowConstants

public class Viewport
extends javax.swing.JFrame
implements InteractiveArea

A convenience superclass for creating simple graphical applications.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JFrame
javax.swing.JFrame.AccessibleJFrame
 
Nested classes/interfaces inherited from class java.awt.Frame
java.awt.Frame.AccessibleAWTFrame
 
Nested classes/interfaces inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow
 
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.JFrame
accessibleContext, EXIT_ON_CLOSE, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface edu.southern.computing.oopj.InteractiveArea
BLACK, BLUE, CROSSHAIR, CYAN, DARK_GRAY, GRAY, GREEN, HAND, LIGHT_GRAY, MAGENTA, MOVE, ORANGE, PINK, RED, TRANSPARENT, VERSION, WHITE, YELLOW
 
Fields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
Viewport(java.lang.String title, int x, int y, int width, int height)
          Creates a new application window.
 
Method Summary
 java.awt.Component add(java.awt.Component c)
          Adds a graphical component to the viewport.
 void clear()
          Clears the viewport, erasing any existing drawings
 void draw()
          Renders the contents of the viewport.
 void draw(java.awt.Graphics g)
          Sets the transient graphics context and then calls the no-parameter version of draw().
 void drawLine(int startX, int startY, int endX, int endY)
          Draws a line with the current color.
 void drawLine(int startX, int startY, int endX, int endY, java.awt.Color color)
          Draws a line with the given color.
 void drawOval(int left, int top, int width, int height)
          Draws an oval with the current drawing color.
 void drawPoint(double x, double y)
          Draws a point (pixel) with the current color.
 void drawPoint(int x, int y)
          Draws a point (pixel) with the current color.
 void drawPoint(int x, int y, java.awt.Color color)
          Draws a point (pixel) with the given color.
 void drawPolygon(int... coords)
          Draws a polygon with the current color.
 void drawRectangle(double left, double top, double width, double height)
          Draws a rectangle with double-precision floating point coordinates.
 void drawRectangle(int left, int top, int width, int height)
          Draws a rectangle with the current drawing color.
 void drawString(java.lang.String message, int x, int y)
          Draws a string with the current color.
 void fillOval(int left, int top, int width, int height)
          Paints a filled oval with the current color.
 void fillOval(int left, int top, int width, int height, java.awt.Color color)
          Paints a filled oval with the given color.
 void fillPolygon(int... coords)
          Paints a filled polygon with the current color.
 void fillRectangle(int left, int top, int width, int height)
          Paints a filled rectangle with the current color.
 void fillRectangle(int left, int top, int width, int height, java.awt.Color color)
          Paints a filled rectangle with the given color.
 char getKeyTyped()
          Returns the key typed by the user during the last keyboard interaction event.
 int getMouseX()
          Returns the x position of the latest mouse event.
 int getMouseY()
          Returns the y position of the latest mouse event.
 javax.swing.JPanel getViewportPanel()
          Returns the underlying content pane of the viewport.
 void keyPressed(java.awt.event.KeyEvent e)
          Called when the user presses a key when the viewport has keyboard focus.
 void keyReleased(java.awt.event.KeyEvent e)
          Called when the user releases a key when the viewport has keyboard focus.
 void keyTyped()
          Called when the user types (presses and releases) a key when the viewport has keyboard focus.
 void keyTyped(java.awt.event.KeyEvent e)
          Called when the user types (presses and releases) a key when the viewport has keyboard focus.
 void mouseClicked()
          Called when the mouse button is clicked (pressed and released) while the mouse pointer is within the viewport area.
 void mouseClicked(java.awt.event.MouseEvent e)
          Called when the mouse is clicked (pressed and released).
 void mouseDragged()
          Called when the mouse is after its button was depressed over the viewport.
 void mouseDragged(java.awt.event.MouseEvent e)
          Called when the mouse is moved when the button is down.
 void mouseEntered()
          Called when the mouse pointer enters the viewport's area.
 void mouseEntered(java.awt.event.MouseEvent e)
          Called when the mouse pointer enters the component.
 void mouseExited()
          Called when the mouse pointer exits the viewport's area.
 void mouseExited(java.awt.event.MouseEvent e)
          Called when the mouse pointer exits the component.
 void mouseMoved()
          Called when the mouse is moved over the viewport and no buttons are depressed.
 void mouseMoved(java.awt.event.MouseEvent e)
          Called when the mouse is moved without a button depressed.
 void mousePressed()
          Called when the mouse button is depressed over the component.
 void mousePressed(java.awt.event.MouseEvent e)
          Called when the mouse button is pressed.
 void mouseReleased()
          Called when the mouse button is released over the viewport.
 void mouseReleased(java.awt.event.MouseEvent e)
          Called when the mouse button is released.
 void setBackground(java.awt.Color color)
          Sets the background color for the display area of the viewport.
 void setBoxLayout(int orient)
          Sets the layout manager for the drawing area the BoxLayout.
 void setColor(java.awt.Color color)
          Draws a rectangle with the current drawing color.
 void setContextMenu(ContextMenu menu)
          Sets the context-sensitive popup menu for the viewport.
 
Methods inherited from class javax.swing.JFrame
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update
 
Methods inherited from class java.awt.Frame
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated
 
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOwnedWindows, getOwner, getOwnerlessWindows, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setVisible, show, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, 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, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.MenuContainer
getFont, postEvent
 

Constructor Detail

Viewport

public Viewport(java.lang.String title,
                int x,
                int y,
                int width,
                int height)
Creates a new application window. The client provides application-specific information.

Parameters:
title - the title of the window; appears in the window's title bar
x - the x coordinate of the window's top-left corner
y - the y coordinate of the window's top-left corner
width - the window's width
height - the window's height
panel - the application's drawing area and application-specific functionality
Method Detail

clear

public void clear()
Clears the viewport, erasing any existing drawings


setColor

public void setColor(java.awt.Color color)
Draws a rectangle with the current drawing color.

Parameters:
color - the color to use as the current drawing color.

drawPoint

public void drawPoint(int x,
                      int y,
                      java.awt.Color color)
Draws a point (pixel) with the given color.

Parameters:
x - x coordinate of the pixel.
y - y coordinate of the pixel.
color - color of the point.

drawPoint

public void drawPoint(int x,
                      int y)
Draws a point (pixel) with the current color.

Parameters:
x - x coordinate of the pixel.
y - y coordinate of the pixel.
color - color of the point.

drawPoint

public void drawPoint(double x,
                      double y)
Draws a point (pixel) with the current color.

Parameters:
x - x coordinate of the pixel.
y - y coordinate of the pixel.
color - color of the point.

drawLine

public void drawLine(int startX,
                     int startY,
                     int endX,
                     int endY,
                     java.awt.Color color)
Draws a line with the given color.

Parameters:
startX - x coordinate of the beginning endpoint.
startY - y coordinate of the beginning endpoint.
endX - x coordinate of the ending endpoint.
endY - y coordinate of the ending endpoint.
color - color of the line.

drawLine

public void drawLine(int startX,
                     int startY,
                     int endX,
                     int endY)
Draws a line with the current color.

Parameters:
startX - x coordinate of the beginning endpoint.
startY - y coordinate of the beginning endpoint.
endX - x coordinate of the ending endpoint.
endY - y coordinate of the ending endpoint.

drawRectangle

public void drawRectangle(int left,
                          int top,
                          int width,
                          int height)
Draws a rectangle with the current drawing color.

Parameters:
left - the x coordinate of the rectangle's left-top corner.
top - the y coordinate of the rectangle's left-top corner.
width - the rectangle's width.
height - the rectangle's height.

drawRectangle

public void drawRectangle(double left,
                          double top,
                          double width,
                          double height)
Draws a rectangle with double-precision floating point coordinates.

Parameters:
left - the x coordinate of the rectangle's left-top corner.
top - the y coordinate of the rectangle's left-top corner.
width - the rectangle's width.
height - the rectangle's height.

fillRectangle

public void fillRectangle(int left,
                          int top,
                          int width,
                          int height,
                          java.awt.Color color)
Paints a filled rectangle with the given color.

Parameters:
left - the x coordinate of the rectangle's left-top corner.
top - the y coordinate of the rectangle's left-top corner.
width - the rectangle's width.
height - the rectangle's height.
color - the fill color.

fillRectangle

public void fillRectangle(int left,
                          int top,
                          int width,
                          int height)
Paints a filled rectangle with the current color.

Parameters:
left - the x coordinate of the rectangle's left-top corner.
top - the y coordinate of the rectangle's left-top corner.
width - the rectangle's width.
height - the rectangle's height.

drawOval

public void drawOval(int left,
                     int top,
                     int width,
                     int height)
Draws an oval with the current drawing color.

Parameters:
left - the x coordinate of the left-top corner of the oval's bounding box.
top - the y coordinate of the left-top corner of the oval's bounding box.
width - the oval's width.
height - the oval's height.

fillOval

public void fillOval(int left,
                     int top,
                     int width,
                     int height,
                     java.awt.Color color)
Paints a filled oval with the given color.

Parameters:
left - the x coordinate of the left-top corner of the oval's bounding box.
top - the y coordinate of the left-top corner of the oval's bounding box.
width - the oval's width.
height - the oval's height.
color - the fill color.

fillOval

public void fillOval(int left,
                     int top,
                     int width,
                     int height)
Paints a filled oval with the current color.

Parameters:
left - the x coordinate of the left-top corner of the oval's bounding box.
top - the y coordinate of the left-top corner of the oval's bounding box.
width - the oval's width.
height - the oval's height.

drawPolygon

public void drawPolygon(int... coords)
Draws a polygon with the current color. Pass the (x,y) coordinates of the polygon's vertices: x1, y1, x2, y2, x3, y3, ...


fillPolygon

public void fillPolygon(int... coords)
Paints a filled polygon with the current color. Pass the (x,y) coordinates of the polygon's vertices: x1, y1, x2, y2, x3, y3, ...


drawString

public void drawString(java.lang.String message,
                       int x,
                       int y)
Draws a string with the current color.

Parameters:
message - the string to render.
x - the x coordinate of the baseline of the string's beginning.
y - the y coordinate of the baseline.

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Called when the mouse is clicked (pressed and released). Subclasses ordinarily should override the no parameter version of this method.

Specified by:
mouseClicked in interface java.awt.event.MouseListener
Parameters:
e - stores information about the mouse event (position, modifiers, etc.)

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Called when the mouse pointer enters the component. Subclasses ordinarily should override the no parameter version of this method.

Specified by:
mouseEntered in interface java.awt.event.MouseListener
Parameters:
e - stores information about the mouse event (position, modifiers, etc.)

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Called when the mouse pointer exits the component. Subclasses ordinarily should override the no parameter version of this method.

Specified by:
mouseExited in interface java.awt.event.MouseListener
Parameters:
e - stores information about the mouse event (position, modifiers, etc.)

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Called when the mouse button is pressed. Subclasses ordinarily should override the no parameter version of this method.

Specified by:
mousePressed in interface java.awt.event.MouseListener
Parameters:
e - stores information about the mouse event (position, modifiers, etc.)

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Called when the mouse button is released. Subclasses ordinarily should override the no parameter version of this method.

Specified by:
mouseReleased in interface java.awt.event.MouseListener
Parameters:
e - stores information about the mouse event (position, modifiers, etc.)

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Called when the mouse is moved when the button is down. Subclasses ordinarily should override the no parameter version of this method.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener
Parameters:
e - stores information about the mouse event (position, modifiers, etc.)

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Called when the mouse is moved without a button depressed. Subclasses ordinarily should override the no parameter version of this method.

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener
Parameters:
e - stores information about the mouse event (position, modifiers, etc.)

mouseClicked

public void mouseClicked()
Called when the mouse button is clicked (pressed and released) while the mouse pointer is within the viewport area. Subclasses can override this method to install a custom, application-specific action.


mouseEntered

public void mouseEntered()
Called when the mouse pointer enters the viewport's area. Subclasses can override this method to install a custom, application-specific action.


mouseExited

public void mouseExited()
Called when the mouse pointer exits the viewport's area. Subclasses can override this method to install a custom, application-specific action.


mousePressed

public void mousePressed()
Called when the mouse button is depressed over the component. Subclasses can override this method to install a custom action to result from pressing the mouse button when the mouse pointer is over the viewport.


mouseReleased

public void mouseReleased()
Called when the mouse button is released over the viewport. Subclasses can override this method to install a custom action to result from releasing the mouse button when the mouse is over the viewport.


mouseDragged

public void mouseDragged()
Called when the mouse is after its button was depressed over the viewport. Subclasses can override this method to customize dragging behavior.


mouseMoved

public void mouseMoved()
Called when the mouse is moved over the viewport and no buttons are depressed. Subclasses can override this method to customize mouse movement responses.


getMouseX

public int getMouseX()
Returns the x position of the latest mouse event.


getMouseY

public int getMouseY()
Returns the y position of the latest mouse event.


keyPressed

public void keyPressed(java.awt.event.KeyEvent e)
Called when the user presses a key when the viewport has keyboard focus.

Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent e)
Called when the user releases a key when the viewport has keyboard focus.

Specified by:
keyReleased in interface java.awt.event.KeyListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
Called when the user types (presses and releases) a key when the viewport has keyboard focus. Subclasses normally override the version of this method that requires no parameters.

Specified by:
keyTyped in interface java.awt.event.KeyListener

keyTyped

public void keyTyped()
Called when the user types (presses and releases) a key when the viewport has keyboard focus. Subclasses normally override this method to add custom behavior when the user interacts with the keyboard.


getKeyTyped

public char getKeyTyped()
Returns the key typed by the user during the last keyboard interaction event.

Returns:

draw

public void draw(java.awt.Graphics g)
Sets the transient graphics context and then calls the no-parameter version of draw(). Nullifies the graphics context after calling draw()>/tt>.

Parameters:
g - the graphics context to use for rendering.

draw

public void draw()
Renders the contents of the viewport. Subclasses should override this method if the viewport must draw any application-specific graphics.


add

public java.awt.Component add(java.awt.Component c)
Adds a graphical component to the viewport. Forces a repaint after the addition.

Overrides:
add in class java.awt.Container
Parameters:
c - the component to add.
Returns:
the component that was added.

getViewportPanel

public javax.swing.JPanel getViewportPanel()
Returns the underlying content pane of the viewport.

Returns:
the viewports content pane

setBoxLayout

public void setBoxLayout(int orient)
Sets the layout manager for the drawing area the BoxLayout.

Parameters:
orient - is the orientation.

setContextMenu

public void setContextMenu(ContextMenu menu)
Sets the context-sensitive popup menu for the viewport.

Specified by:
setContextMenu in interface InteractiveArea
Parameters:
the - context-sensitive menu to apply.

setBackground

public void setBackground(java.awt.Color color)
Sets the background color for the display area of the viewport.

Overrides:
setBackground in class java.awt.Component
Parameters:
color - the background color to use