org.PingOO.DLAI.lib.graph
Class Graph2D

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--org.PingOO.DLAI.lib.graph.Graph2D
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.12 Date: 1996/09/24 05:23:41
Author:
Leigh Brookshaw (http://www.sci.usq.edu.au/staff/leighb/graph/) (leighb@usq.edu.au)
See Also:
Serialized Form

Inner Class Summary
protected  class Graph2D.Graph2DKeyListener
           
protected  class Graph2D.LoadMessage
          This is a separate thread that flashes a message on the Graph2D canvas that data is loading
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AWTTreeLock
 
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
private  java.awt.Color DefaultBackground
          Default Background Color
protected  java.awt.event.KeyListener defaultKeyListener
           
 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  Graph2D.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.Canvas
base, graphicsConfig, nameCounter, serialVersionUID
 
Fields inherited from class java.awt.Component
actionListenerK, adjustmentListenerK, appContext, assert, background, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, changeSupport, componentListener, componentListenerK, componentOrientation, componentSerializedDataVersion, containerListenerK, cursor, dropTarget, enabled, eventMask, focusListener, focusListenerK, font, foreground, hasFocus, height, incRate, inputMethodListener, inputMethodListenerK, isInc, isPacked, itemListenerK, keyListener, keyListenerK, LEFT_ALIGNMENT, locale, LOCK, minSize, mouseListener, mouseListenerK, mouseMotionListener, mouseMotionListenerK, name, nameExplicitlySet, newEventsOnly, ownedWindowK, parent, peer, peerFont, popups, prefSize, RIGHT_ALIGNMENT, serialVersionUID, textListenerK, TOP_ALIGNMENT, valid, visible, width, windowListenerK, x, y
 
Constructor Summary
Graph2D()
           
 
Method Summary
 void attachAxis(Axis a)
          Attaches a previously created Axis.
 void attachDataSet(DataSet d)
          Attaches a DataSet to the graph.
 Axis createAxis(int position)
          Creates and attaches an Axis to the graph.
 void detachAxes()
          Detach All attached Axes.
 void detachAxis(Axis a)
          Detach a previously attached Axis.
 void detachDataSet(DataSet d)
          Detaches the DataSet from the class.
 void detachDataSets()
          Detaches 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)
          Draws a frame around the data area.
 void finishedloading()
          Decrement the loading Data counter by one.
protected  java.awt.Rectangle ForceSquare(java.awt.Graphics g, java.awt.Rectangle r)
          Forces 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)
          Calculates the rectangle occupied by the data.
 Markers getMarkers()
          Get the markers
 double getXmax()
          Gets the Maximum X value of all attached DataSets.
 double getXmin()
          Gets the Minimum X value of all attached DataSets.
 double getYmax()
          Gets the Maximum Y value of all attached DataSets.
 double getYmin()
          Gets the Minimum Y value of all attached DataSets.
 DataSet loadDataSet(double[] data, int n)
          Loads and Attaches a DataSet from an array.
 DataSet loadFile(java.net.URL file)
          Loads and Attaches 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)
          Sets the background color for the data window.
 void setGraphBackground(java.awt.Color c)
          Sets 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, constructComponentName, postsOldMouseEvents
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, areInputMethodsEnabled, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, dispatchEventImpl, doLayout, enable, enable, enableEvents, enableInputMethods, eventEnabled, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont_NoClientCode, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getName, getNativeContainer, getParent_NoClientCode, getParent, getPeer, getPreferredSize, getSize, getSize, getToolkit, getToolkitImpl, getTreeLock, getWidth, getWindowForObject, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, initIDs, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isEnabledImpl, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, lightweightPrint, 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, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, readObject, remove, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, validate, writeObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

DefaultBackground

private java.awt.Color DefaultBackground
Default Background Color

axis

protected java.util.Vector axis
A vector list of All the axes attached
See Also:
org.PingOO.DLAI.lib.graph.Graph2d#attachAxis

dataset

protected java.util.Vector dataset
A vector list of All the DataSets attached
See Also:
org.PingOO.DLAI.lib.graph.Graph2d#attachDataSet, DataSet

markers

protected Markers markers
The markers that may have been loaded
See Also:
setMarkers(org.PingOO.DLAI.lib.graph.Markers)

load_thread

protected Graph2D.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.

defaultKeyListener

protected java.awt.event.KeyListener defaultKeyListener
Constructor Detail

Graph2D

public Graph2D()
Method Detail

loadFile

public DataSet loadFile(java.net.URL file)
Loads and Attaches 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)
Loads and Attaches 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)
Attaches 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)
Detaches the DataSet from the class.
Data associated with the DataSet will no longer be plotted.
Parameters:
d - The DataSet to detach.

detachDataSets

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

createAxis

public Axis createAxis(int position)
Creates and attaches 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)
Attaches a previously created Axis.
Only Axes that have been attached will be drawn .
Parameters:
the - Axis to attach.

detachAxis

public void detachAxis(Axis a)
Detach a previously attached Axis.
Parameters:
the - Axis to dettach.

detachAxes

public void detachAxes()
Detach All attached Axes.

getXmax

public double getXmax()
Gets the Maximum X value of all attached DataSets.
Returns:
The maximum value

getYmax

public double getYmax()
Gets the Maximum Y value of all attached DataSets.
Returns:
The maximum value

getXmin

public double getXmin()
Gets the Minimum X value of all attached DataSets.
Returns:
The minimum value

getYmin

public double getYmin()
Gets 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)
Sets the background color for the entire canvas.
Parameters:
c - The color to set the canvas

setDataBackground

public void setDataBackground(java.awt.Color c)
Sets 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.
Parameters:
g - Graphics state.
Overrides:
paint in class java.awt.Canvas

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.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(java.lang.String), Graph2D.LoadMessage

finishedloading

public void finishedloading()
Decrement the loading Data counter by one.
When it is zero resume plotting.
See Also:
startedloading(), loadmessage(java.lang.String), Graph2D.LoadMessage

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(), Graph2D.LoadMessage

ForceSquare

protected java.awt.Rectangle ForceSquare(java.awt.Graphics g,
                                         java.awt.Rectangle r)
Forces 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.
Parameters:
g -  
r -  

getDataRectangle

protected java.awt.Rectangle getDataRectangle(java.awt.Graphics g,
                                              java.awt.Rectangle r)
Calculates 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)
Draws a frame around the data area.