org.mbari.siam.moos.utils.chart.graph
Class Graph2D

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Canvas
          extended by org.mbari.siam.moos.utils.chart.graph.Graph2D
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible
Direct Known Subclasses:
G2Dint

public class Graph2D
extends java.awt.Canvas

This is the main plotting class. It partitions the canvas to contain the specified axes with the remaining space taken with the plotting region. Axes are packed against the walls of the canvas. The paint and update methods of this class handle all the drawing operations of the graph. This means that independent components like Axis and DataSets must be registered with this class to be incorporated into the plot.

Version:
$Revision: 1.2 $, $Date: 2011/08/17 19:09:20 $
Author:
Leigh Brookshaw
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
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
protected  java.util.Vector axis
          A vector list of All the axes attached
 int borderBottom
          The width of the border at the bottom of the canvas.
 int borderLeft
          The width of the border at the left of the canvas.
 int borderRight
          The width of the border at the right of the canvas.
 int borderTop
          The width of the border at the top of the canvas.
 boolean clearAll
          If set true (the default) the canvas will be set to the background color (erasing the plot) when the update method is called.
protected  java.awt.Color DataBackground
          The background color for the data window
 java.awt.Rectangle datarect
          The rectangle that the data will be plotted within.
protected  java.util.Vector dataset
          A vector list of All the DataSets attached
 boolean drawgrid
          If set true (the default) a grid will be drawn over the data window.
 boolean drawzero
          If set true (the default) a grid line will be drawn across the data window at the zeros of the innermost axes.
 boolean frame
          If set true a frame will be drawn around the data window.
 java.awt.Color framecolor
          The color of the frame to be drawn
 java.awt.Color gridcolor
          The color of the grid to be drawn
 TextLine lastText
          Text to be painted Last onto the Graph Canvas.
protected  org.mbari.siam.moos.utils.chart.graph.LoadMessage load_thread
          The blinking "data loading" thread
 int loadingData
          If this is greater than zero it means that data loading threads are active so the message "loading data" is flashed on the plot canvas.
protected  Markers markers
          The markers that may have been loaded
 boolean paintAll
          If set true (the default) everything associated with the plot will be drawn when the update method or paint method are called.
 boolean square
          Modify the position of the axis and the range of the axis so that the aspect ratio of the major tick marks are 1 and the plot is square on the screen
 java.awt.Color zerocolor
          The color of the zero grid lines.
 
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()
           
 
Method Summary
 void attachAxis(Axis a)
          Attach a previously created Axis.
 void attachDataSet(DataSet d)
          Attach a DataSet to the graph.
 Axis createAxis(int position)
          Create and attach an Axis to the graph.
 void detachAxes()
          Detach All attached Axes.
 void detachAxis(Axis a)
          Detach a previously attached Axis.
 void detachDataSet(DataSet d)
          Detach the DataSet from the class.
 void detachDataSets()
          Detach All the DataSets from the class.
protected  java.awt.Rectangle drawAxis(java.awt.Graphics g, java.awt.Rectangle r)
          Draw the Axis.
protected  void drawFrame(java.awt.Graphics g, int x, int y, int width, int height)
           
 void finishedloading()
          Decrement the loading Data counter by one.
protected  java.awt.Rectangle ForceSquare(java.awt.Graphics g, java.awt.Rectangle r)
          Force the plot to have an aspect ratio of 1 by forcing the axes to have the same range.
protected  java.awt.Rectangle getDataRectangle(java.awt.Graphics g, java.awt.Rectangle r)
          Calculate the rectangle occupied by the data
 Markers getMarkers()
          Get the markers
 double getXmax()
          Get the Maximum X value of all attached DataSets.
 double getXmin()
          Get the Minimum X value of all attached DataSets.
 double getYmax()
          Get the Maximum Y value of all attached DataSets.
 double getYmin()
          Get the Minimum Y value of all attached DataSets.
 boolean keyDown(java.awt.Event e, int key)
          Handle keyDown events.
 DataSet loadDataSet(double[] data, int n)
          Load and Attach a DataSet from an array.
 DataSet loadFile(java.net.URL file)
          Load and Attach a DataSet from a File.
 void loadmessage(java.lang.String s)
          Change the message to be flashed on the canvas
 void paint(java.awt.Graphics g)
          This paints the entire plot.
 void paintBeforeData(java.awt.Graphics g, java.awt.Rectangle r)
          A hook into the Graph2D.paint method.
 void paintFirst(java.awt.Graphics g, java.awt.Rectangle r)
          A hook into the Graph2D.paint method.
 void paintLast(java.awt.Graphics g, java.awt.Rectangle r)
          A hook into the Graph2D.paint method.
 void setDataBackground(java.awt.Color c)
          Set the background color for the data window.
 void setGraphBackground(java.awt.Color c)
          Set the background color for the entire canvas.
 void setMarkers(Markers m)
          Set the markers for the plot.
 void startedloading()
          Calling this method pauses the plot and displays a flashing message on the screen.
 void update(java.awt.Graphics g)
          This method is called via the Graph2D.repaint() method.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

axis

protected java.util.Vector axis
A vector list of All the axes attached

See Also:
attachAxis(Axis a)

dataset

protected java.util.Vector dataset
A vector list of All the DataSets attached

See Also:
attachDataSet(DataSet d), DataSet

markers

protected Markers markers
The markers that may have been loaded

See Also:
setMarkers(Markers m)

load_thread

protected org.mbari.siam.moos.utils.chart.graph.LoadMessage load_thread
The blinking "data loading" thread

See Also:
startedloading()

DataBackground

protected java.awt.Color DataBackground
The background color for the data window


loadingData

public int loadingData
If this is greater than zero it means that data loading threads are active so the message "loading data" is flashed on the plot canvas. When it is back to zero the plot progresses normally


borderTop

public int borderTop
The width of the border at the top of the canvas. This allows slopover from axis labels, legends etc.


borderBottom

public int borderBottom
The width of the border at the bottom of the canvas. This allows slopover from axis labels, legends etc.


borderLeft

public int borderLeft
The width of the border at the left of the canvas. This allows slopover from axis labels, legends etc.


borderRight

public int borderRight
The width of the border at the right of the canvas. This allows slopover from axis labels, legends etc.


frame

public boolean frame
If set true a frame will be drawn around the data window. Any axes will overlay this frame.


framecolor

public java.awt.Color framecolor
The color of the frame to be drawn


drawgrid

public boolean drawgrid
If set true (the default) a grid will be drawn over the data window. The grid will align with the major tic marks of the Innermost axes.


gridcolor

public java.awt.Color gridcolor
The color of the grid to be drawn


drawzero

public boolean drawzero
If set true (the default) a grid line will be drawn across the data window at the zeros of the innermost axes.


zerocolor

public java.awt.Color zerocolor
The color of the zero grid lines.


datarect

public java.awt.Rectangle datarect
The rectangle that the data will be plotted within. This is an output variable only.


clearAll

public boolean clearAll
If set true (the default) the canvas will be set to the background color (erasing the plot) when the update method is called. This would only be changed for special effects.


paintAll

public boolean paintAll
If set true (the default) everything associated with the plot will be drawn when the update method or paint method are called. Normally only modified for special effects


square

public boolean square
Modify the position of the axis and the range of the axis so that the aspect ratio of the major tick marks are 1 and the plot is square on the screen


lastText

public TextLine lastText
Text to be painted Last onto the Graph Canvas.

Constructor Detail

Graph2D

public Graph2D()
Method Detail

loadFile

public DataSet loadFile(java.net.URL file)
Load and Attach a DataSet from a File. The method loads the data into a DataSet class and attaches the class to the graph for plotting. The data is assumed to consist (at this stage) 2 ASCII columns of numbers x, y. As always blank lines are ignored and everything following # is ignored as a comment.

Parameters:
file - The URL of the data file to read.
Returns:
The DataSet constructed containing the data read.

loadDataSet

public DataSet loadDataSet(double[] data,
                           int n)
Load and Attach a DataSet from an array. The method loads the data into a DataSet class and attaches the class to the graph for plotting. The data is assumed to be stored in the form x,y,x,y,x,y.... A local copy of the data is made.

Parameters:
data - The data to be loaded in the form x,y,x,y,...
n - The number of (x,y) data points. This means that the minimum length of the data array is 2*n.
Returns:
The DataSet constructed containing the data read.

attachDataSet

public void attachDataSet(DataSet d)
Attach a DataSet to the graph. By attaching the data set the class can draw the data through its paint method.


detachDataSet

public void detachDataSet(DataSet d)
Detach the DataSet from the class. Data associated with the DataSet will nolonger be plotted.

Parameters:
d - The DataSet to detach.

detachDataSets

public void detachDataSets()
Detach All the DataSets from the class.


createAxis

public Axis createAxis(int position)
Create and attach an Axis to the graph. The position of the axis is one of Axis.TOP, Axis.BOTTOM, Axis.LEFT or Axis.RIGHT.

Parameters:
position - Position of the axis in the drawing window.

attachAxis

public void attachAxis(Axis a)
Attach a previously created Axis. Only Axes that have been attached will be drawn

Parameters:
a - The Axis to attach.

detachAxis

public void detachAxis(Axis a)
Detach a previously attached Axis.

Parameters:
a - The Axis to dettach.

detachAxes

public void detachAxes()
Detach All attached Axes.


getXmax

public double getXmax()
Get the Maximum X value of all attached DataSets.

Returns:
The maximum value

getYmax

public double getYmax()
Get the Maximum Y value of all attached DataSets.

Returns:
The maximum value

getXmin

public double getXmin()
Get the Minimum X value of all attached DataSets.

Returns:
The minimum value

getYmin

public double getYmin()
Get the Minimum Y value of all attached DataSets.

Returns:
The minimum value

setMarkers

public void setMarkers(Markers m)
Set the markers for the plot.

Parameters:
m - Marker class containing the defined markers
See Also:
Markers

getMarkers

public Markers getMarkers()
Get the markers

Returns:
defined Marker class
See Also:
Markers

setGraphBackground

public void setGraphBackground(java.awt.Color c)
Set the background color for the entire canvas.

Parameters:
c - The color to set the canvas

setDataBackground

public void setDataBackground(java.awt.Color c)
Set the background color for the data window.

Parameters:
c - The color to set the data window.

paint

public void paint(java.awt.Graphics g)
This paints the entire plot. It calls the draw methods of all the attached axis and data sets. The order of drawing is - Axis first, data legends next, data last.

Overrides:
paint in class java.awt.Canvas
Parameters:
g - Graphics state.

paintFirst

public void paintFirst(java.awt.Graphics g,
                       java.awt.Rectangle r)
A hook into the Graph2D.paint method. This is called before anything is plotted. The rectangle passed is the dimension of the canvas minus the border dimensions.

Parameters:
g - Graphics state
r - Rectangle containing the graph

paintBeforeData

public void paintBeforeData(java.awt.Graphics g,
                            java.awt.Rectangle r)
A hook into the Graph2D.paint method. This is called before the data is drawn but after the axis. The rectangle passed is the dimension of the data window.

Parameters:
g - Graphics state
r - Rectangle containing the data

paintLast

public void paintLast(java.awt.Graphics g,
                      java.awt.Rectangle r)
A hook into the Graph2D.paint method. This is called after everything has been drawn. The rectangle passed is the dimension of the data window.

Parameters:
g - Graphics state
r - Rectangle containing the data

update

public void update(java.awt.Graphics g)
This method is called via the Graph2D.repaint() method. All it does is blank the canvas (with the background color) before calling paint.

Overrides:
update in class java.awt.Canvas

keyDown

public boolean keyDown(java.awt.Event e,
                       int key)
Handle keyDown events. Only one event is handled the pressing of the key 'r' - this will repaint the canvas.

Overrides:
keyDown in class java.awt.Component

startedloading

public void startedloading()
Calling this method pauses the plot and displays a flashing message on the screen. Mainly used when data is being loaded across the net. Everytime this routine is called a counter is incremented the method Graph2D.finishedloading() decrements the counter. When the counter is back to zero the plotting resumes.

See Also:
finishedloading(), loadmessage(String s)

finishedloading

public void finishedloading()
Decrement the loading Data counter by one. When it is zero resume plotting.

See Also:
startedloading(), loadmessage(String s)

loadmessage

public void loadmessage(java.lang.String s)
Change the message to be flashed on the canvas

Parameters:
s - String contining the new message.
See Also:
startedloading(), finishedloading()

ForceSquare

protected java.awt.Rectangle ForceSquare(java.awt.Graphics g,
                                         java.awt.Rectangle r)
Force the plot to have an aspect ratio of 1 by forcing the axes to have the same range. If the range of the axes are very different some extremely odd things can occur. All axes are forced to have the same range, so more than 2 axis is pointless.


getDataRectangle

protected java.awt.Rectangle getDataRectangle(java.awt.Graphics g,
                                              java.awt.Rectangle r)
Calculate the rectangle occupied by the data


drawAxis

protected java.awt.Rectangle drawAxis(java.awt.Graphics g,
                                      java.awt.Rectangle r)
Draw the Axis. As each axis is drawn and aligned less of the canvas is avaliable to plot the data. The returned Rectangle is the canvas area that the data is plotted in.


drawFrame

protected void drawFrame(java.awt.Graphics g,
                         int x,
                         int y,
                         int width,
                         int height)


Copyright © 2003 MBARI.

The Monterey Bay Aquarium Research Institute (MBARI) provides this documentation and code "as is", with no warranty, express or implied, of its quality or consistency. It is provided without support and without obligation on the part of MBARI to assist in its use, correction, modification, or enhancement.