All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JSci.awt.DoubleBufferedCanvas Maven / Gradle / Ivy

Go to download

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.awt;

import java.awt.*;

/**
* The DoubleBufferedCanvas 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.
* 
    *
  1. Override the {@link #offscreenPaint(Graphics) offscreenPaint} method and use the {@link #redraw() redraw} method. Passive rendering.
  2. *
  3. Draw to the graphics context returned by the {@link #getOffscreenGraphics() getOffscreenGraphics} method and use the {@link java.awt.Component#repaint() repaint} method. Active rendering.
  4. *
* The first way alone should be sufficient for most purposes. * @version 1.3 * @author Mark Hale */ public abstract class DoubleBufferedCanvas extends Canvas { private Image buffer = null; private boolean doRedraw = true; /** * Constructs a double buffered canvas. */ public DoubleBufferedCanvas() {} /** * Paints the canvas using double buffering. * @see #offscreenPaint */ public final void paint(Graphics g) { if(doRedraw) { doRedraw = false; final int width=getSize().width; final int height=getSize().height; 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(0,0,width,height); /* restore original color */ graphics.setColor(oldColor); offscreenPaint(graphics); } g.drawImage(buffer, 0, 0, null); } /** * Updates the canvas. */ public final void update(Graphics g) { paint(g); } /** * Prints the canvas. */ public final void print(Graphics g) { offscreenPaint(g); } /** * 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); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy