org.sape.carbon.services.swing.graph
Class GraphCanvas

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JPanel
                          |
                          +--org.sape.carbon.services.swing.graph.GraphCanvas
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable

public class GraphCanvas
extends JPanel

This class provides a consistent space within to graph real numbers. It provides features such as auto-centering and real-time scaling. A user of this graph provides data by creating one or more tracks and then adding real points to those. Calling translate periodically allows you to create a scrolling graph as well.

This graph will also maintain tick marks that resize and can be stuck to the sides of the screen so they are always visible even if the origin is off-screen.

Copyright 2001 Sapient

Version:
$Revision: 1.4 $ ($Author: dvoet $ / $Date: 2003/05/05 21:21:26 $)
Author:
Greg Hinkle
See Also:
Serialized Form

Nested Class Summary
static class GraphCanvas.Track
           
 
Field Summary
protected  Color backgroundColor
          The background color for this graph
protected static NumberFormat bigNumberLabelFormat
           
private static double BORDER_PERCENT
          The portion of the entire height that should be researved as a border, above and below the highest and lowest track points
protected  Rectangle2D graphBounds
          The current graph bounds that are visible
protected static NumberFormat labelFormat
           
private  Map tracks
          A list of Track's that are a part of this graph
 
Fields inherited from class javax.swing.JPanel
 
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.Container
 
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
GraphCanvas()
          Instantiates a graph canvas
 
Method Summary
 void addPoint(String track, Point2D point)
           
 void addTrack(String trackName)
           
 void addTrack(String trackName, Color color)
           
 void clear()
           
 void clearAll()
           
 void clipOld()
           
protected  void drawAxes(Graphics2D g, AffineTransform transform)
          This draws the axes
protected  void drawKey(Graphics2D g, AffineTransform transform)
          Draw the key to the tracks by calling thier toString
protected  void drawLines(Graphics2D g, AffineTransform transform)
          This draws the tick marks in the graph
protected  void drawTracks(Graphics2D g, AffineTransform transform)
           
static Point2D floorPoint(Point2D point, Rectangle2D rect)
          This finds the closest point on a rectangle's edge to a point outside the rectangle or if that point is within the rectangle it is returned.
 Rectangle2D getGraphBounds()
          Gets the bounds of the graphing space that are currently showing on the screen.
 GraphCanvas.Track getTrack(String trackName)
           
 AffineTransform getTransform()
           
static void main(String[] args)
           
 void paint(Graphics gg)
           
 void setBackgroundColor(Color color)
          Sets the background color of this graph
 void setGraphBounds(Rectangle2D rect)
          Sets the bounds that this graph is displaying
 void translate(double x, double y)
          Translates the main graph rect by x and y, horizontally and vertically respectively.
 void verticalBound()
          Bounds the graph to the limits of the tracks verticaly providing a useful scaling.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, 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, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, 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, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, 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, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, 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
 

Field Detail

tracks

private Map tracks
A list of Track's that are a part of this graph


graphBounds

protected Rectangle2D graphBounds
The current graph bounds that are visible


BORDER_PERCENT

private static final double BORDER_PERCENT
The portion of the entire height that should be researved as a border, above and below the highest and lowest track points

See Also:
Constant Field Values

backgroundColor

protected Color backgroundColor
The background color for this graph


labelFormat

protected static NumberFormat labelFormat

bigNumberLabelFormat

protected static NumberFormat bigNumberLabelFormat
Constructor Detail

GraphCanvas

public GraphCanvas()
Instantiates a graph canvas

Method Detail

setBackgroundColor

public void setBackgroundColor(Color color)

Sets the background color of this graph

Parameters:
color - the Color to set the background to

getGraphBounds

public Rectangle2D getGraphBounds()
Gets the bounds of the graphing space that are currently showing on the screen.

Returns:
Rectangle2D The bounds of the currently visible graph

setGraphBounds

public void setGraphBounds(Rectangle2D rect)
Sets the bounds that this graph is displaying

Parameters:
rect - the Rectangle2D of the desired graph points

getTransform

public AffineTransform getTransform()

clear

public void clear()

addTrack

public void addTrack(String trackName)

addTrack

public void addTrack(String trackName,
                     Color color)

addPoint

public void addPoint(String track,
                     Point2D point)

getTrack

public GraphCanvas.Track getTrack(String trackName)

clearAll

public void clearAll()

paint

public void paint(Graphics gg)
Overrides:
paint in class JComponent

drawKey

protected void drawKey(Graphics2D g,
                       AffineTransform transform)

Draw the key to the tracks by calling thier toString

Parameters:
transform - the Affine Transform to use to determine how to draw

drawTracks

protected void drawTracks(Graphics2D g,
                          AffineTransform transform)

drawAxes

protected void drawAxes(Graphics2D g,
                        AffineTransform transform)
This draws the axes


floorPoint

public static Point2D floorPoint(Point2D point,
                                 Rectangle2D rect)

This finds the closest point on a rectangle's edge to a point outside the rectangle or if that point is within the rectangle it is returned.

Parameters:
point - The point to rectangularly floor
rect - The rectangle to floor within

drawLines

protected void drawLines(Graphics2D g,
                         AffineTransform transform)

This draws the tick marks in the graph


verticalBound

public void verticalBound()

Bounds the graph to the limits of the tracks verticaly providing a useful scaling. A more intelligent implementation could have minimum bounds to limit the bouncyness to startup.


clipOld

public void clipOld()

translate

public void translate(double x,
                      double y)

Translates the main graph rect by x and y, horizontally and vertically respectively.


main

public static void main(String[] args)
                 throws Exception
Exception


Copyright 1999-2003 Sapient Corporation. All Rights Reserved.