org.strbio.util.graph
Class Graph2D

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by org.strbio.util.graph.Graph2D
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class Graph2D
extends javax.swing.JComponent

A component that displays a 2D graph, similar to gnuplot. The graph contains an area in the middle containing zero or more Graphable2D objects, optional title text, optional labels, and optional axis labels.

  Version 1.0, 4/20/99 - original version.
  

Version:
1.0, 4/20/99
Author:
JMC
See Also:
Serialized Form

Nested Class Summary
static interface Graph2D.ClickListener
          This is an interface for things that are interested in knowing when the graph is clicked.
 
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
 int BOX_ALL
          Draw box completely around graph region (default).
 int BOX_BOTTOM
          Draw line on bottom side of graph region.
 int BOX_LEFT
          Draw line on left side of graph region.
 int BOX_NONE
          Don't draw a box around the graph region.
 int BOX_RIGHT
          Draw line on right side of graph region.
 int BOX_TOP
          Draw line on top side of graph region.
static int FLIP_X
          Flip the plot left-right (i.e. 0 on the right, going left)
static int FLIP_Y
          Flip the plot up-down (i.e. 0 on the bottom, going up)
protected  boolean flipX
           
protected  boolean flipY
           
protected  double maxX
           
protected  double maxY
           
protected  double minX
           
protected  double minY
           
 
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
Graph2D()
          Start with nothing.
 
Method Summary
 void addGraph(Graphable2D gr)
          Add a graph to the view, setting its legend font to the current one.
protected  double calcMaxX()
          Returns the maximum x value, from all the graphs.
protected  double calcMaxY()
          Returns the maximum y value, from all the graphs.
protected  double calcMinX()
          Returns the minimum x value, from all the graphs.
protected  double calcMinY()
          Returns the minimum y value, from all the graphs.
 double getMaxX()
          Returns the maximum x value currently being plotted.
 double getMaxY()
          Returns the maximum y value currently being plotted.
 java.awt.Dimension getMinimumSize()
           
 double getMinX()
          Returns the minimum x value currently being plotted.
 double getMinY()
          Returns the minimum y value currently being plotted.
 java.awt.Dimension getPreferredSize()
           
 XAxisScale getXAxisScale()
          gets the x axis scale
 YAxisScale getYAxisScale()
          gets y axis scale
 void makeXAxisScale()
          make new x axis scale.
 void makeYAxisScale()
          make new y axis scale.
 void paintComponent(java.awt.Graphics g)
          display on screen.
 void paintGraph(java.awt.Graphics g, int width, int height)
          Displays everything on a given sized area (printer page, or screen).
 void print(java.awt.Frame fr)
          Print the graph to a printer.
 void removeGraph(Graphable2D gr)
          Remove a graph.
 void reRender()
          Repaint everything.
 void rescale()
          Rescale, if autoscaling.
 void setBox(int boxStyle)
          Sets box drawing style.
 void setFont(java.awt.Font f)
          Sets the font for all labels / titles.
 void setFormat(java.lang.String x)
          Sets the format string for printing axis scales.
 void setGraphSize(int x, int y)
          Set the preferred size of the graph window.
 void setListener(Graph2D.ClickListener l)
          Sets the object that listens for clicks on the graph.
 void setMaxX(double x)
          Set the maximum x value to plot... turns off autoscaling of the maximum.
 void setMaxY(double y)
          Set the maximum y value to plot... turns off autoscaling of the maximum.
 void setMinX(double x)
          Set the minimum X value to plot... turns off autoscaling.
 void setMinY(double y)
          Set the minimum Y value to plot... turns off autoscaling.
 void setOrientation(int orientation)
          Set the preferred orientation.
 void setTitle(java.lang.String x)
          Sets title.
 void setXAxisLabel(java.lang.String x)
          Sets x axis label
 void setYAxisLabel(java.lang.String x)
          Sets y axis label
 void transform(double x, double y, java.awt.Point p)
          Transforms a given value (x,y) in real coordinates into a point to be plotted on the screen, considering min, max, etc.
 
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, 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, paint, paintBorder, paintChildren, 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, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, 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, 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
 

Field Detail

minX

protected double minX

minY

protected double minY

maxX

protected double maxX

maxY

protected double maxY

BOX_NONE

public final int BOX_NONE
Don't draw a box around the graph region.

See Also:
Constant Field Values

BOX_LEFT

public final int BOX_LEFT
Draw line on left side of graph region.

See Also:
Constant Field Values

BOX_RIGHT

public final int BOX_RIGHT
Draw line on right side of graph region.

See Also:
Constant Field Values

BOX_TOP

public final int BOX_TOP
Draw line on top side of graph region.

See Also:
Constant Field Values

BOX_BOTTOM

public final int BOX_BOTTOM
Draw line on bottom side of graph region.

See Also:
Constant Field Values

BOX_ALL

public final int BOX_ALL
Draw box completely around graph region (default).

See Also:
Constant Field Values

FLIP_X

public static final int FLIP_X
Flip the plot left-right (i.e. 0 on the right, going left)

See Also:
Constant Field Values

FLIP_Y

public static final int FLIP_Y
Flip the plot up-down (i.e. 0 on the bottom, going up)

See Also:
Constant Field Values

flipY

protected boolean flipY

flipX

protected boolean flipX
Constructor Detail

Graph2D

public Graph2D()
Start with nothing.

Method Detail

setListener

public void setListener(Graph2D.ClickListener l)
Sets the object that listens for clicks on the graph. Only one object at a time can listen, for now.


setBox

public final void setBox(int boxStyle)
Sets box drawing style.


setTitle

public void setTitle(java.lang.String x)
Sets title.


setXAxisLabel

public void setXAxisLabel(java.lang.String x)
Sets x axis label


setYAxisLabel

public void setYAxisLabel(java.lang.String x)
Sets y axis label


getXAxisScale

public XAxisScale getXAxisScale()
gets the x axis scale


makeXAxisScale

public void makeXAxisScale()
make new x axis scale.


makeYAxisScale

public void makeYAxisScale()
make new y axis scale.


getYAxisScale

public YAxisScale getYAxisScale()
gets y axis scale


calcMaxX

protected double calcMaxX()
Returns the maximum x value, from all the graphs.


calcMinX

protected double calcMinX()
Returns the minimum x value, from all the graphs.


calcMaxY

protected double calcMaxY()
Returns the maximum y value, from all the graphs.


calcMinY

protected double calcMinY()
Returns the minimum y value, from all the graphs.


getMinX

public double getMinX()
Returns the minimum x value currently being plotted.


getMaxX

public double getMaxX()
Returns the maximum x value currently being plotted.


getMinY

public double getMinY()
Returns the minimum y value currently being plotted.


getMaxY

public double getMaxY()
Returns the maximum y value currently being plotted.


setMinX

public void setMinX(double x)
Set the minimum X value to plot... turns off autoscaling. When set to NaN, turns on autoscaling.


setMaxX

public void setMaxX(double x)
Set the maximum x value to plot... turns off autoscaling of the maximum. When set to NaN, turns on autoscaling.


setMinY

public void setMinY(double y)
Set the minimum Y value to plot... turns off autoscaling. When set to NaN, turns on autoscaling.


setMaxY

public void setMaxY(double y)
Set the maximum y value to plot... turns off autoscaling of the maximum. When set to NaN, turns on autoscaling.


rescale

public void rescale()
Rescale, if autoscaling. If axis scales, update them with new values.


addGraph

public void addGraph(Graphable2D gr)
Add a graph to the view, setting its legend font to the current one. If graph is a ClickListener, makes it the listener.


removeGraph

public void removeGraph(Graphable2D gr)
Remove a graph. It will no longer listen for clicks, if it was before.


getMinimumSize

public java.awt.Dimension getMinimumSize()
Overrides:
getMinimumSize in class javax.swing.JComponent

getPreferredSize

public java.awt.Dimension getPreferredSize()
Overrides:
getPreferredSize in class javax.swing.JComponent

setGraphSize

public void setGraphSize(int x,
                         int y)
Set the preferred size of the graph window.


setOrientation

public void setOrientation(int orientation)
Set the preferred orientation.


setFormat

public void setFormat(java.lang.String x)
Sets the format string for printing axis scales. i.e. "%.2f". If null, won't display numbers.


setFont

public void setFont(java.awt.Font f)
Sets the font for all labels / titles.

Overrides:
setFont in class javax.swing.JComponent

transform

public void transform(double x,
                      double y,
                      java.awt.Point p)
Transforms a given value (x,y) in real coordinates into a point to be plotted on the screen, considering min, max, etc. Note that this doesn't return a point because that would be really slow (object creation/deletion).


paintComponent

public void paintComponent(java.awt.Graphics g)
display on screen.

Overrides:
paintComponent in class javax.swing.JComponent

paintGraph

public void paintGraph(java.awt.Graphics g,
                       int width,
                       int height)
Displays everything on a given sized area (printer page, or screen).


reRender

public void reRender()
Repaint everything.


print

public void print(java.awt.Frame fr)
Print the graph to a printer. Needs a frame because of old java api.