Class HeatMap

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JPanel
                  extended by HeatMap
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class HeatMap
extends javax.swing.JPanel

Title: HeatMap

Description: HeatMap is a JPanel that displays a 2-dimensional array of data using a selected color gradient scheme.

For specifying data, the first index into the double[][] array is the x- coordinate, and the second index is the y-coordinate. In the constructor and updateData method, the 'useGraphicsYAxis' parameter is used to control whether the row y=0 is displayed at the top or bottom. Since the usual graphics coordinate system has y=0 at the top, setting this parameter to true will draw the y=0 row at the top, and setting the parameter to false will draw the y=0 row at the bottom, like in a regular, mathematical coordinate system. This parameter was added as a solution to the problem of "Which coordinate system should we use? Graphics, or mathematical?", and allows the user to choose either coordinate system. Because the HeatMap will be plotting the data in a graphical manner, using the Java Swing framework that uses the standard computer graphics coordinate system, the user's data is stored internally with the y=0 row at the top.

There are a number of defined gradient types (look at the static fields), but you can create any gradient you like by using either of the following functions in the Gradient class:

You can then assign an arbitrary Color[] object to the HeatMap as follows:
myHeatMap.updateGradient(Gradient.createMultiGradient(new Color[] {Color.red, Color.white, Color.blue}, 256));

By default, the graph title, axis titles, and axis tick marks are not displayed. Be sure to set the appropriate title before enabling them.


Copyright: Copyright (c) 2007, 2008

HeatMap is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

HeatMap is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with HeatMap; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
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
 
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
HeatMap(double[][] data, boolean useGraphicsYAxis, java.awt.Color[] colors)
           
 
Method Summary
static double[][] generatePyramidData(int dimension)
          This function generates an appropriate data array for display.
static double[][] generateRampTestData()
          This function generates data that is not vertically-symmetric, which makes it very useful for testing which type of vertical axis is being used to plot the data.
static double[][] generateSinCosData(int dimension)
          This function generates an appropriate data array for display.
 void paintComponent(java.awt.Graphics g)
          The overridden painting method, now optimized to simply draw the data plot to the screen, letting the drawImage method do the resizing.
 void setColorBackground(java.awt.Color bg)
          Updates the background color.
 void setColorForeground(java.awt.Color fg)
          Updates the foreground color.
 void setCoordinateBounds(double xMin, double xMax, double yMin, double yMax)
          Specify the coordinate bounds for the map.
 void setDrawLegend(boolean drawLegend)
          Updates the state of the legend.
 void setDrawTitle(boolean drawTitle)
          Updates the state of the title.
 void setDrawXAxisTitle(boolean drawXAxisTitle)
          Updates the state of the X-Axis Title.
 void setDrawXTicks(boolean drawXTicks)
          Updates the state of the X-Axis ticks.
 void setDrawYAxisTitle(boolean drawYAxisTitle)
          Updates the state of the Y-Axis Title.
 void setDrawYTicks(boolean drawYTicks)
          Updates the state of the Y-Axis ticks.
 void setTitle(java.lang.String title)
          Updates the title.
 void setXAxisTitle(java.lang.String xAxisTitle)
          Updates the X-Axis title.
 void setXCoordinateBounds(double xMin, double xMax)
          Specify the coordinate bounds for the X-range.
 void setXMaxCoordinateBounds(double xMax)
          Specify the coordinate bounds for the X Max.
 void setXMinCoordinateBounds(double xMin)
          Specify the coordinate bounds for the X Min.
 void setYAxisTitle(java.lang.String yAxisTitle)
          Updates the Y-Axis title.
 void setYCoordinateBounds(double yMin, double yMax)
          Specify the coordinate bounds for the Y-range.
 void setYMaxCoordinateBounds(double yMax)
          Specify the coordinate bounds for the Y Max.
 void setYMinCoordinateBounds(double yMin)
          Specify the coordinate bounds for the Y Min.
 void updateData(double[][] data, boolean useGraphicsYAxis)
          Updates the data display, calls drawData() to do the expensive re-drawing of the data plot, and then calls repaint().
 void updateGradient(java.awt.Color[] colors)
          Updates the gradient used to display the data.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, 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, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, 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, setFont, setForeground, setInheritsPopupMenu, 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, 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
 

Constructor Detail

HeatMap

public HeatMap(double[][] data,
               boolean useGraphicsYAxis,
               java.awt.Color[] colors)
Parameters:
data - The data to display, must be a complete array (non-ragged)
useGraphicsYAxis - If true, the data will be displayed with the y=0 row at the top of the screen. If false, the data will be displayed with they=0 row at the bottom of the screen.
colors - A variable of the type Color[]. See also #createMultiGradient and #createGradient.
Method Detail

setCoordinateBounds

public void setCoordinateBounds(double xMin,
                                double xMax,
                                double yMin,
                                double yMax)
Specify the coordinate bounds for the map. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.

Parameters:
xMin - The lower bound of x-values, used for axis labels
xMax - The upper bound of x-values, used for axis labels

setXCoordinateBounds

public void setXCoordinateBounds(double xMin,
                                 double xMax)
Specify the coordinate bounds for the X-range. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.

Parameters:
xMin - The lower bound of x-values, used for axis labels
xMax - The upper bound of x-values, used for axis labels

setXMinCoordinateBounds

public void setXMinCoordinateBounds(double xMin)
Specify the coordinate bounds for the X Min. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.

Parameters:
xMin - The lower bound of x-values, used for axis labels

setXMaxCoordinateBounds

public void setXMaxCoordinateBounds(double xMax)
Specify the coordinate bounds for the X Max. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.

Parameters:
xMax - The upper bound of x-values, used for axis labels

setYCoordinateBounds

public void setYCoordinateBounds(double yMin,
                                 double yMax)
Specify the coordinate bounds for the Y-range. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.

Parameters:
yMin - The lower bound of y-values, used for axis labels
yMax - The upper bound of y-values, used for axis labels

setYMinCoordinateBounds

public void setYMinCoordinateBounds(double yMin)
Specify the coordinate bounds for the Y Min. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.

Parameters:
yMin - The lower bound of Y-values, used for axis labels

setYMaxCoordinateBounds

public void setYMaxCoordinateBounds(double yMax)
Specify the coordinate bounds for the Y Max. Only used for the axis labels, which must be enabled seperately. Calls repaint() when finished.

Parameters:
yMax - The upper bound of y-values, used for axis labels

setTitle

public void setTitle(java.lang.String title)
Updates the title. Calls repaint() when finished.

Parameters:
title - The new title

setDrawTitle

public void setDrawTitle(boolean drawTitle)
Updates the state of the title. Calls repaint() when finished.

Parameters:
drawTitle - Specifies if the title should be drawn

setXAxisTitle

public void setXAxisTitle(java.lang.String xAxisTitle)
Updates the X-Axis title. Calls repaint() when finished.

Parameters:
xAxisTitle - The new X-Axis title

setDrawXAxisTitle

public void setDrawXAxisTitle(boolean drawXAxisTitle)
Updates the state of the X-Axis Title. Calls repaint() when finished.

Parameters:
drawXAxisTitle - Specifies if the X-Axis title should be drawn

setYAxisTitle

public void setYAxisTitle(java.lang.String yAxisTitle)
Updates the Y-Axis title. Calls repaint() when finished.

Parameters:
yAxisTitle - The new Y-Axis title

setDrawYAxisTitle

public void setDrawYAxisTitle(boolean drawYAxisTitle)
Updates the state of the Y-Axis Title. Calls repaint() when finished.

Parameters:
drawYAxisTitle - Specifies if the Y-Axis title should be drawn

setDrawLegend

public void setDrawLegend(boolean drawLegend)
Updates the state of the legend. Calls repaint() when finished.

Parameters:
drawLegend - Specifies if the legend should be drawn

setDrawXTicks

public void setDrawXTicks(boolean drawXTicks)
Updates the state of the X-Axis ticks. Calls repaint() when finished.

Parameters:
drawXTicks - Specifies if the X-Axis ticks should be drawn

setDrawYTicks

public void setDrawYTicks(boolean drawYTicks)
Updates the state of the Y-Axis ticks. Calls repaint() when finished.

Parameters:
drawYTicks - Specifies if the Y-Axis ticks should be drawn

setColorForeground

public void setColorForeground(java.awt.Color fg)
Updates the foreground color. Calls repaint() when finished.

Parameters:
fg - Specifies the desired foreground color

setColorBackground

public void setColorBackground(java.awt.Color bg)
Updates the background color. Calls repaint() when finished.

Parameters:
bg - Specifies the desired background color

updateGradient

public void updateGradient(java.awt.Color[] colors)
Updates the gradient used to display the data. Calls drawData() and repaint() when finished.

Parameters:
colors - A variable of type Color[]

generateRampTestData

public static double[][] generateRampTestData()
This function generates data that is not vertically-symmetric, which makes it very useful for testing which type of vertical axis is being used to plot the data. If the graphics Y-axis is used, then the lowest values should be displayed at the top of the frame. If the non-graphics (mathematical coordinate-system) Y-axis is used, then the lowest values should be displayed at the bottom of the frame.

Returns:
double[][] data values of a simple vertical ramp

generateSinCosData

public static double[][] generateSinCosData(int dimension)
This function generates an appropriate data array for display. It uses the function: z = sin(x)*cos(y). The parameter specifies the number of data points in each direction, producing a square matrix.

Parameters:
dimension - Size of each side of the returned array
Returns:
double[][] calculated values of z = sin(x)*cos(y)

generatePyramidData

public static double[][] generatePyramidData(int dimension)
This function generates an appropriate data array for display. It uses the function: z = Math.cos(Math.abs(sX) + Math.abs(sY)). The parameter specifies the number of data points in each direction, producing a square matrix.

Parameters:
dimension - Size of each side of the returned array
Returns:
double[][] calculated values of z = Math.cos(Math.abs(sX) + Math.abs(sY));

updateData

public void updateData(double[][] data,
                       boolean useGraphicsYAxis)
Updates the data display, calls drawData() to do the expensive re-drawing of the data plot, and then calls repaint().

Parameters:
data - The data to display, must be a complete array (non-ragged)
useGraphicsYAxis - If true, the data will be displayed with the y=0 row at the top of the screen. If false, the data will be displayed with the y=0 row at the bottom of the screen.

paintComponent

public void paintComponent(java.awt.Graphics g)
The overridden painting method, now optimized to simply draw the data plot to the screen, letting the drawImage method do the resizing. This saves an extreme amount of time.

Overrides:
paintComponent in class javax.swing.JComponent