JSci.swing.JDoubleBufferedComponent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.swing;
import java.awt.*;
import javax.swing.*;
/**
* The JDoubleBufferedComponent class provides double buffering functionality.
* Painting events simply cause the offscreen buffer to be painted.
* It is the responsibility of sub-classes to explicitly update the offscreen buffer.
* The offscreen buffer can be updated in two ways.
*
* - Override the {@link #offscreenPaint(Graphics) offscreenPaint} method and use the {@link #redraw() redraw} method. Passive rendering.
* - Draw to the graphics context returned by the {@link #getOffscreenGraphics() getOffscreenGraphics} method and use the {@link java.awt.Component#repaint() repaint} method. Active rendering.
*
* The first way alone should be sufficient for most purposes.
* @version 1.3
* @author Mark Hale
*/
public abstract class JDoubleBufferedComponent extends JComponent {
private Image buffer = null;
private boolean doRedraw = true;
/**
* Constructs a double buffered canvas.
*/
public JDoubleBufferedComponent() {
super.setDoubleBuffered(false);
}
/**
* Paints the canvas using double buffering.
* @see #offscreenPaint
*/
public final void paintComponent(Graphics g) {
Insets insets = getInsets();
if(doRedraw) {
doRedraw = false;
final int width = getWidth()-insets.left-insets.right;
final int height = getHeight()-insets.top-insets.bottom;
buffer = createImage(width, height);
if(buffer == null)
return;
final Graphics graphics=buffer.getGraphics();
/* save original color */
Color oldColor = graphics.getColor();
graphics.setColor(getBackground());
graphics.fillRect(insets.left, insets.top, width, height);
/* restore original color */
graphics.setColor(oldColor);
offscreenPaint(graphics);
}
g.drawImage(buffer, insets.left, insets.top, null);
}
/**
* Updates the canvas.
*/
public final void update(Graphics g) {
paint(g);
}
/**
* Prints the canvas.
*/
public final void printComponent(Graphics g) {
offscreenPaint(g);
}
/**
* Double buffering cannot be controlled for this component.
* This method always throws an exception.
*/
public final void setDoubleBuffered(boolean flag) {
throw new IllegalArgumentException();
}
/**
* Double buffering is always enabled.
* @return true.
*/
public final boolean isDoubleBuffered() {
return true;
}
/**
* Redraws the canvas.
* This method may safely be called from outside the event-dispatching thread.
*/
public final void redraw() {
doRedraw = true;
repaint();
}
/**
* Returns the offscreen graphics context or null
if not available.
*/
protected final Graphics getOffscreenGraphics() {
return (buffer != null) ? buffer.getGraphics() : null;
}
/**
* Paints the canvas off-screen.
* Override this method instead of paint(Graphics g).
*/
protected abstract void offscreenPaint(Graphics g);
}