com.approximatrix.charting
Class BufferedChart

java.lang.Object
  extended by com.approximatrix.charting.render.AbstractRenderer
      extended by com.approximatrix.charting.AbstractChart
          extended by com.approximatrix.charting.BufferedChart
All Implemented Interfaces:
Chart, ChartDataModelListener, RenderChangeListener, Renderer, java.util.EventListener

public class BufferedChart
extends AbstractChart
implements RenderChangeListener, ChartDataModelListener

BufferedChart creates a chart that renders only once to a buffered image, then simply displays the image rather than performing a full chart rendering. This technique improves performance by only re-rendering the chart when absolutely necessary.

Version:
1.0
Author:
armstrong

Nested Class Summary
private  class BufferedChart.RenderThread
          Private class for handling drawing the chart on a separate thread.
 
Field Summary
private  java.awt.image.BufferedImage buffer
          The image buffer holding the current rendering of the chart
private  java.awt.image.BufferedImage drawbuffer
          The second buffer where the separate thread will draw
private  boolean enableBuffering
          Flag indicating buffering is enabled
protected  java.awt.Font font
          The font to display the render message
static int LINEAR_X_LINEAR_Y
           
static int NO_COORDINATE_SYSTEM
           
private  boolean redraw
          Flag indicating a redraw may be necessary
protected  java.lang.String render_message
          The String displayed during rendering work
private  BufferedChart.RenderThread rt
          The rendering thread to perform all drawing
 
Fields inherited from class com.approximatrix.charting.AbstractChart
bounds, coord, legend, model, rcModel, renderer, title
 
Fields inherited from class com.approximatrix.charting.render.AbstractRenderer
listener
 
Constructor Summary
protected BufferedChart()
          Creates new empty DefaultChart.
  BufferedChart(ChartDataModel model, java.lang.String title)
          Creates a new BufferedChart with the given model and title string and no coordinate system.
  BufferedChart(ChartDataModel model, java.lang.String title, int coord)
          Creates a new BufferedChart with the given model and title string and a coordinate system.
  BufferedChart(ChartDataModel model, java.lang.String title, int coord, java.lang.String xaxis, java.lang.String yaxis)
          Creates a new BufferedChart with the given model and title string and a coordinate system.
 
Method Summary
 void addChartRenderer(AbstractChartRenderer render, int z)
          Adds a ChartRenderer with a specific z-coordinate.
private  void assignListeners()
          Assigns this chart as the listener for all RenderChange events
 void chartDataChanged(ChartDataModelEvent evt)
          This method is called, whenever an event is created.
private  java.awt.image.BufferedImage createBuffer()
          Returns a new BufferedImage that is sized to be the same as this component's bounds.
private  void drawBufferImage(java.awt.Graphics2D g)
          Draws the current image buffer onto the specified Graphics2D object
 void drawChart(java.awt.Graphics2D g)
          Does the layout of the title, legend and coordinate system and calls the render method of all those including the ChartRenderers.
private  void drawNotification()
          Draws a notification that rendering is in process on the buffer image
 java.awt.Font getFont()
          Returns the font used to display the rendering message.
 java.awt.Dimension getPreferredSize()
          Should compute the preferred size of the Chart
 java.lang.String getRenderMessage()
          Returns the current message stating that rendering is occuring
private  void initBuffer()
          Initializes the image buffer based on current bounds
private  void initialize()
          Central calling method for initializing the specifics of this bufferd chart.
private  boolean invalidBuffer()
          Determines if the current buffer is the wrong size
 boolean isBuffering()
          Returns whether buffering is currently enabled
 void notifyDisplayReady()
          Called by the render thread to notify its parent that rendering of the chart is complete and available on the drawBuffer
 void render(java.awt.Graphics2D g)
          The central rendering routine that handles all drawing operations.
 void renderUpdateRequested(RenderChangeEvent evt)
          This method is called, whenever an event is created.
 void setBuffering(boolean value)
          Sets whether rendering should be buffered for speed
 void setChartDataModel(ChartDataModel model)
          Stores the ChartDataModel for this Chart.
 void setChartRenderer(java.util.Map render)
          Sets the Map with all ChartRenderers.
 void setCoordSystem(CoordSystem c)
          Sets the coordinate system for this chart, which can be null if the ChartRenderer doesn't need a coordinate system, e.g.
 void setFont(java.awt.Font newfont)
          Sets the font which messages are rendered int
 void setLegend(Legend l)
          Sets the legend for this chart.
 void setRenderMessage(java.lang.String value)
          Sets the current rendering message to be displayed while the render thread is running
 void setRowColorModel(RowColorModel rcm)
          Sets the RowColorModel for this chart.
 void setTitle(Title t)
          Sets the title for this chart.
 
Methods inherited from class com.approximatrix.charting.AbstractChart
getBounds, getChartDataModel, getChartRenderer, getChartRenderer, getCoordSystem, getLegend, getRowColorModel, getTitle, paintDefault, setBounds
 
Methods inherited from class com.approximatrix.charting.render.AbstractRenderer
addRenderChangeListener, clearRenderChangeListeners, fireRenderChangeEvent, getDirectRender, isDifferentBounds, removeRenderChangeListener, setDirectRender
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINEAR_X_LINEAR_Y

public static int LINEAR_X_LINEAR_Y

NO_COORDINATE_SYSTEM

public static int NO_COORDINATE_SYSTEM

render_message

protected java.lang.String render_message
The String displayed during rendering work


font

protected java.awt.Font font
The font to display the render message


rt

private BufferedChart.RenderThread rt
The rendering thread to perform all drawing


buffer

private java.awt.image.BufferedImage buffer
The image buffer holding the current rendering of the chart


drawbuffer

private java.awt.image.BufferedImage drawbuffer
The second buffer where the separate thread will draw


redraw

private boolean redraw
Flag indicating a redraw may be necessary


enableBuffering

private boolean enableBuffering
Flag indicating buffering is enabled

Constructor Detail

BufferedChart

protected BufferedChart()
Creates new empty DefaultChart.


BufferedChart

public BufferedChart(ChartDataModel model,
                     java.lang.String title)
Creates a new BufferedChart with the given model and title string and no coordinate system.

Parameters:
model - the ChartDataModel
title - the title String

BufferedChart

public BufferedChart(ChartDataModel model,
                     java.lang.String title,
                     int coord)
Creates a new BufferedChart with the given model and title string and a coordinate system.

Parameters:
model - the ChartDataModel
title - the title String
coord - the id of the coordinate system configuration

BufferedChart

public BufferedChart(ChartDataModel model,
                     java.lang.String title,
                     int coord,
                     java.lang.String xaxis,
                     java.lang.String yaxis)
Creates a new BufferedChart with the given model and title string and a coordinate system.

Parameters:
model - the ChartDataModel
title - the title String
coord - the id of the coordinate system configuration
xaxis - the x-axis' unit
yaxis - the y-axis' unit
Method Detail

initialize

private void initialize()
Central calling method for initializing the specifics of this bufferd chart.


assignListeners

private void assignListeners()
Assigns this chart as the listener for all RenderChange events


getPreferredSize

public java.awt.Dimension getPreferredSize()
Should compute the preferred size of the Chart

Specified by:
getPreferredSize in interface Renderer
Returns:
null

initBuffer

private void initBuffer()
Initializes the image buffer based on current bounds


createBuffer

private java.awt.image.BufferedImage createBuffer()
Returns a new BufferedImage that is sized to be the same as this component's bounds.

Returns:
a new, blank BufferedImage of type TYPE_INT_RGB

invalidBuffer

private boolean invalidBuffer()
Determines if the current buffer is the wrong size

Returns:
true if the buffer needs to be reinitialized, false otherwise

setBuffering

public void setBuffering(boolean value)
Sets whether rendering should be buffered for speed

Parameters:
value - true to enable buffering, false otherwise

isBuffering

public boolean isBuffering()
Returns whether buffering is currently enabled

Returns:
true is buffering is on, false otherwise

getFont

public java.awt.Font getFont()
Returns the font used to display the rendering message.

Returns:
the current font all messages are displayed in

setFont

public void setFont(java.awt.Font newfont)
Sets the font which messages are rendered int

Parameters:
newfont - the new font to use for rendering messages

getRenderMessage

public java.lang.String getRenderMessage()
Returns the current message stating that rendering is occuring

Returns:
a String to be displayed while the render thread is busy

setRenderMessage

public void setRenderMessage(java.lang.String value)
Sets the current rendering message to be displayed while the render thread is running

Parameters:
value - a valid (non-null) String to be displayed during rendering

notifyDisplayReady

public void notifyDisplayReady()
Called by the render thread to notify its parent that rendering of the chart is complete and available on the drawBuffer


render

public void render(java.awt.Graphics2D g)
The central rendering routine that handles all drawing operations. The routine will launch the render thread if buffering is enabled and an update is needed (this.redraw = true). Otherwise, it just plots either the buffered image (if buffeering is enabled) or draws directly to the passed graphics object.

Specified by:
render in interface Chart
Specified by:
render in interface Renderer
Overrides:
render in class AbstractChart
Parameters:
g - the actual display Graphics2D object on which to be painted

drawNotification

private void drawNotification()
Draws a notification that rendering is in process on the buffer image


drawBufferImage

private void drawBufferImage(java.awt.Graphics2D g)
Draws the current image buffer onto the specified Graphics2D object

Parameters:
g - the target Graphics2D object

drawChart

public void drawChart(java.awt.Graphics2D g)
Does the layout of the title, legend and coordinate system and calls the render method of all those including the ChartRenderers.

Parameters:
g - the Graphics2D object to paint in.

renderUpdateRequested

public void renderUpdateRequested(RenderChangeEvent evt)
This method is called, whenever an event is created. The routine simply flips the redraw flag to true. The actual redraw is within the render method.

Specified by:
renderUpdateRequested in interface RenderChangeListener
Parameters:
evt - the event object

chartDataChanged

public void chartDataChanged(ChartDataModelEvent evt)
This method is called, whenever an event is created.

Specified by:
chartDataChanged in interface ChartDataModelListener
Parameters:
evt - the event object

addChartRenderer

public void addChartRenderer(AbstractChartRenderer render,
                             int z)
Adds a ChartRenderer with a specific z-coordinate.

Specified by:
addChartRenderer in interface Chart
Overrides:
addChartRenderer in class AbstractChart
Parameters:
render - the ChartRenderer
z - the z-coordinate, the highest coordinate is in front.

setChartRenderer

public void setChartRenderer(java.util.Map render)
Sets the Map with all ChartRenderers. The keys have to be the z-coordinates of the ChartRenderers.

Specified by:
setChartRenderer in interface Chart
Overrides:
setChartRenderer in class AbstractChart
Parameters:
render - a java.util.Map with all ChartRenderers.

setCoordSystem

public void setCoordSystem(CoordSystem c)
Sets the coordinate system for this chart, which can be null if the ChartRenderer doesn't need a coordinate system, e.g. if it's a PieChart.

Specified by:
setCoordSystem in interface Chart
Overrides:
setCoordSystem in class AbstractChart
Parameters:
c - the CoordSystem object

setLegend

public void setLegend(Legend l)
Sets the legend for this chart.

Specified by:
setLegend in interface Chart
Overrides:
setLegend in class AbstractChart
Parameters:
l - the Legend

setTitle

public void setTitle(Title t)
Sets the title for this chart.

Specified by:
setTitle in interface Chart
Overrides:
setTitle in class AbstractChart
Parameters:
t - the Title object

setRowColorModel

public void setRowColorModel(RowColorModel rcm)
Sets the RowColorModel for this chart.

Overrides:
setRowColorModel in class AbstractChart
Parameters:
rcm - the new RowColorModel

setChartDataModel

public void setChartDataModel(ChartDataModel model)
Stores the ChartDataModel for this Chart.

Specified by:
setChartDataModel in interface Chart
Overrides:
setChartDataModel in class AbstractChart
Parameters:
model - the ChartDataModel