jwo.utils.gui
Class JWGraph

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JPanel
                  extended byjwo.utils.gui.JWGraph
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable
Direct Known Subclasses:
BlockGraph, Histogram, LineGraph, PolarGraph, Scatterplot

public class JWGraph
extends JPanel

Simple graph object. Originally based on the code given in the tutorial Chart your way to custom graph components, by Maria Winslow, 1997 (JavaWorld, July, 1997). See www.javaworld.com/javaworld/jw-07-1997/jw-07-step.html. Modified to use Swing and additional axis labelling.

Version:
2.1, 19th October, 2002.
Author:
Jo Wood
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JPanel
JPanel.AccessibleJPanel
 
Nested classes inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  int bottom
          Bottom position of graph.
protected  int centreX
          X coord of centre of graph.
protected  int centreY
          Y coord of centre of graph.
static int INTERVAL
          Measurement scale of data.
protected  Vector items
          Array of graphic items in graph.
protected  int left
          Left position of graph.
static int NOMINAL
          Measurement scale of data.
protected  Image offscreenImage
          Image for offscreen double buffering.
protected  Graphics og
          Offscreen double buffering context.
static int ORDINAL
          Measurement scale of data.
static int RATIO
          Measurement scale of data.
protected  int right
          Right position of graph.
protected  int top
          Top position of graph.
 
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
JWGraph()
          Minimal advance graph layout (axes only).
JWGraph(String title, float yMin, float yMax)
          Lays out the graph title, labels etc.
JWGraph(String title, float xMin, float xMax, float yMin, float yMax)
          Lays out the graph title, labels etc.
JWGraph(String title, float rMin, float rMax, float thetaMin, float thetaMax, boolean polar)
          Lays out the graph title, labels etc as a polar plot.
JWGraph(String title, float xMin, float xMax, float yMin, float yMax, String[] labels)
          Lays out the graph title, labels etc.
JWGraph(String title, float xMin, float xMax, float yMin, float yMax, String[] labels, int measurementScale)
          Lays out the graph title, labels etc.
JWGraph(String title, float xMin, float xMax, String[] labels, float yMin, float yMax)
          Lays out the graph title, labels etc.
JWGraph(String title, float xMin, float xMax, String[] labels, float yMin, float yMax, int measurementScale)
          Lays out the graph title, labels etc.
 
Method Summary
 void addItem(float xValue, float yValue)
          Adds a graphical element to the graph.
 void addItem(float xValue, float yValue, Color col)
          Adds a (coloured) graphical element to the graph.
 void erase()
          Clears the current graph area and redraws it.
 FontMetrics getFM()
          Reports the font metric used by the graph.
 Dimension getPreferredSize()
          Reports the preferred size of the graph
 float getXMax()
          Reports the maximum X value of the graph.
 int getXMeasurementScale()
          Reports on the measurement scale of the data on the X-axis.
 float getXMin()
          Reports the minimum X value of the graph.
 float getYMax()
          Reports the maximum Y value of the graph.
 int getYMeasurementScale()
          Reports on the measurement scale of the data on the Y-axis.
 float getYMin()
          Reports the minimum Y value of the graph.
 Color makeTransparent(Color inColour)
          Modifies a given colour by the currently set opacity level.
 void paintComponent(Graphics g)
          Overrides the paint method to draw the graph components.
 void removeItem(float xValue, float yValue)
          Removes graphical element(s) from the graph (identified by x,y values).
 void removeItems()
          Removes all graphical elements from the graph.
 void setAspectRatio(float aspectRatio)
          Sets the aspect ratio for the graph.
 void setOpacity(int opacity)
          Sets the opacity level for the graph.
 void setTitle(String title)
          Sets the title of the graph and updates the display.
 void setXAxis(float min, float max)
          Sets the x-axis to the given range.
 void setXAxis(float min, float max, String[] xLabels, int scale)
          Sets the x-axis to the given range using the given measurement scale.
 void setXMeasurementScale(int scale)
          Sets the measurement scale of the data on the X-axis.
 void setYAxis(float min, float max)
          Sets the y-axis to the given range.
 void setYAxis(float min, float max, int scale)
          Sets the y-axis to the given range using the given measurement scale.
 void setYMeasurementScale(int scale)
          Sets the measurement scale of the data on the Y-axis.
 
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, 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, paint, paintBorder, paintChildren, 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

NOMINAL

public static final int NOMINAL
Measurement scale of data.

See Also:
Constant Field Values

ORDINAL

public static final int ORDINAL
Measurement scale of data.

See Also:
Constant Field Values

INTERVAL

public static final int INTERVAL
Measurement scale of data.

See Also:
Constant Field Values

RATIO

public static final int RATIO
Measurement scale of data.

See Also:
Constant Field Values

offscreenImage

protected Image offscreenImage
Image for offscreen double buffering.


og

protected Graphics og
Offscreen double buffering context.


top

protected int top
Top position of graph.


bottom

protected int bottom
Bottom position of graph.


left

protected int left
Left position of graph.


right

protected int right
Right position of graph.


centreX

protected int centreX
X coord of centre of graph.


centreY

protected int centreY
Y coord of centre of graph.


items

protected Vector items
Array of graphic items in graph.

Constructor Detail

JWGraph

public JWGraph()
Minimal advance graph layout (axes only).


JWGraph

public JWGraph(String title,
               float yMin,
               float yMax)
Lays out the graph title, labels etc. Assumes Y range is known in advance.

Parameters:
title - Title of the graph.
yMin - Minimum Y value.
yMax - Maximum Y value.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               float yMin,
               float yMax)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
yMin - Minimum Y value.
yMax - Maximum Y value.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               float yMin,
               float yMax,
               String[] labels)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance and Y axis contains nominal scale labels.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
yMin - Minimum Y value.
yMax - Maximum Y value.
labels - Array holding labels for Y axis.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               float yMin,
               float yMax,
               String[] labels,
               int measurementScale)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance and Y axis contains scale labels on the given measurement scale.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
yMin - Minimum Y value.
yMax - Maximum Y value.
labels - Array holding labels for Y axis.
measurementScale - Measurement scale of the labelled axis.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               String[] labels,
               float yMin,
               float yMax)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance and X axis contains nominal scale labels.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
labels - Array holding lables of X axis.
yMin - Minimum Y value.
yMax - Maximum Y value.

JWGraph

public JWGraph(String title,
               float xMin,
               float xMax,
               String[] labels,
               float yMin,
               float yMax,
               int measurementScale)
Lays out the graph title, labels etc. Assumes both X and Y range are known in advance and X axis contains labels on the given measurement scale.

Parameters:
title - Title of the graph.
xMin - Minimum X value.
xMax - Maximum X value.
labels - Array holding lables of X axis.
yMin - Minimum Y value.
yMax - Maximum Y value.
measurementScale - Measurement scale of the labelled axis.

JWGraph

public JWGraph(String title,
               float rMin,
               float rMax,
               float thetaMin,
               float thetaMax,
               boolean polar)
Lays out the graph title, labels etc as a polar plot. Assumes both radius and angular range are known in advance.

Parameters:
title - Title of the graph.
rMin - Minimum X value.
rMax - Maximum X value.
thetaMin - Starting angular value.
thetaMax - Ending angular value.
polar - Indicates a polar plot.
Method Detail

setOpacity

public void setOpacity(int opacity)
Sets the opacity level for the graph.

Parameters:
opacity - Opacity level for items drawn on graph from 0 (invisible) to 255 (opaque).

erase

public void erase()
Clears the current graph area and redraws it.


makeTransparent

public Color makeTransparent(Color inColour)
Modifies a given colour by the currently set opacity level.

Parameters:
inColour - Colour to modify.
Returns:
Colour modified to show currently set opacity.

setAspectRatio

public void setAspectRatio(float aspectRatio)
Sets the aspect ratio for the graph. Values greater than 1 indicate larger width than height, less than 1 indicate greater height and 0 indicates no locked aspect ratio.

Parameters:
aspectRatio - Ratio of width over height or 0 for no fixed ratio.

addItem

public void addItem(float xValue,
                    float yValue)
Adds a graphical element to the graph.

Parameters:
xValue - x-value associated with item.
yValue - y-value associated with item.

addItem

public void addItem(float xValue,
                    float yValue,
                    Color col)
Adds a (coloured) graphical element to the graph.

Parameters:
xValue - x-value associated with item.
yValue - y-value associated with item.
col - Colour of item.

removeItem

public void removeItem(float xValue,
                       float yValue)
Removes graphical element(s) from the graph (identified by x,y values).

Parameters:
xValue - x-value associated with item.
yValue - y-value associated with item.

removeItems

public void removeItems()
Removes all graphical elements from the graph.


getYMin

public float getYMin()
Reports the minimum Y value of the graph.

Returns:
Minimum Y value.

getYMax

public float getYMax()
Reports the maximum Y value of the graph.

Returns:
Maximum Y value.

getXMin

public float getXMin()
Reports the minimum X value of the graph.

Returns:
Minimum X value.

getXMax

public float getXMax()
Reports the maximum X value of the graph.

Returns:
Maximum X value.

getFM

public FontMetrics getFM()
Reports the font metric used by the graph.

Returns:
Font metric.

getXMeasurementScale

public int getXMeasurementScale()
Reports on the measurement scale of the data on the X-axis.

Returns:
Mesurement scale (NOMINAL, ORDINAL, INTERVAL or RATIO).

getYMeasurementScale

public int getYMeasurementScale()
Reports on the measurement scale of the data on the Y-axis.

Returns:
Mesurement scale (NOMINAL, ORDINAL, INTERVAL or RATIO).

setXMeasurementScale

public void setXMeasurementScale(int scale)
Sets the measurement scale of the data on the X-axis.

Parameters:
scale - Mesurement scale (NOMINAL, ORDINAL, INTERVAL or RATIO).

setYMeasurementScale

public void setYMeasurementScale(int scale)
Sets the measurement scale of the data on the Y-axis.

Parameters:
scale - Mesurement scale (NOMINAL, ORDINAL, INTERVAL or RATIO).

setXAxis

public void setXAxis(float min,
                     float max)
Sets the x-axis to the given range.

Parameters:
min - Minimum axis value.
max - Maximum axis value.

setXAxis

public void setXAxis(float min,
                     float max,
                     String[] xLabels,
                     int scale)
Sets the x-axis to the given range using the given measurement scale.

Parameters:
min - Minimum axis value.
max - Maximum axis value.
xLabels - Labels for x axis.
scale - Axis measurement scale.

setYAxis

public void setYAxis(float min,
                     float max)
Sets the y-axis to the given range.

Parameters:
min - Minimum axis value.
max - Maximum axis value.

setYAxis

public void setYAxis(float min,
                     float max,
                     int scale)
Sets the y-axis to the given range using the given measurement scale.

Parameters:
min - Minimum axis value.
max - Maximum axis value.
scale - Axis measurement scale.

setTitle

public void setTitle(String title)
Sets the title of the graph and updates the display.

Parameters:
title - New title to display.

paintComponent

public void paintComponent(Graphics g)
Overrides the paint method to draw the graph components.

Parameters:
g - Graphics context to paint to.

getPreferredSize

public Dimension getPreferredSize()
Reports the preferred size of the graph

Returns:
The graph's preferred size.


Copyright Jo Wood, 1996-2004, last modified, 3rd September, 2004