KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > awt > DoubleBufferedCanvas


1 package JSci.awt;
2
3 import java.awt.*;
4
5 /**
6 * The DoubleBufferedCanvas class provides double buffering functionality.
7 * Painting events simply cause the offscreen buffer to be painted.
8 * It is the responsibility of sub-classes to explicitly update the offscreen buffer.
9 * The offscreen buffer can be updated in two ways.
10 * <ol>
11 * <li>Override the {@link #offscreenPaint(Graphics) offscreenPaint} method and use the {@link #redraw() redraw} method. Passive rendering.</li>
12 * <li>Draw to the graphics context returned by the {@link #getOffscreenGraphics() getOffscreenGraphics} method and use the {@link java.awt.Component#repaint() repaint} method. Active rendering.</li>
13 * </ol>
14 * The first way alone should be sufficient for most purposes.
15 * @version 1.3
16 * @author Mark Hale
17 */

18 public abstract class DoubleBufferedCanvas extends Canvas {
19         private Image buffer = null;
20         private boolean doRedraw = true;
21         /**
22         * Constructs a double buffered canvas.
23         */

24         public DoubleBufferedCanvas() {}
25         /**
26         * Paints the canvas using double buffering.
27         * @see #offscreenPaint
28         */

29         public final void paint(Graphics g) {
30                 if(doRedraw) {
31                         doRedraw = false;
32                         final int width=getSize().width;
33                         final int height=getSize().height;
34                         buffer=createImage(width,height);
35                         if(buffer == null)
36                                 return;
37                         final Graphics graphics=buffer.getGraphics();
38                         /* save original color */
39                         Color oldColor = graphics.getColor();
40                         graphics.setColor(getBackground());
41                         graphics.fillRect(0,0,width,height);
42                         /* restore original color */
43                         graphics.setColor(oldColor);
44                         offscreenPaint(graphics);
45                 }
46                 g.drawImage(buffer, 0, 0, null);
47         }
48         /**
49         * Updates the canvas.
50         */

51         public final void update(Graphics g) {
52                 paint(g);
53         }
54         /**
55         * Prints the canvas.
56         */

57         public final void print(Graphics g) {
58                 offscreenPaint(g);
59         }
60         /**
61         * Redraws the canvas.
62         * This method may safely be called from outside the event-dispatching thread.
63         */

64         public final void redraw() {
65                 doRedraw = true;
66                 repaint();
67         }
68         /**
69         * Returns the offscreen graphics context or <code>null</code> if not available.
70         */

71         protected final Graphics getOffscreenGraphics() {
72                 return (buffer != null) ? buffer.getGraphics() : null;
73         }
74         /**
75         * Paints the canvas off-screen.
76         * Override this method instead of paint(Graphics g).
77         */

78         protected abstract void offscreenPaint(Graphics g);
79 }
80
81
Popular Tags