KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > swing > JDoubleBufferedComponent


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

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

25         public JDoubleBufferedComponent() {
26                 super.setDoubleBuffered(false);
27         }
28         /**
29         * Paints the canvas using double buffering.
30         * @see #offscreenPaint
31         */

32         public final void paintComponent(Graphics g) {
33                 Insets insets = getInsets();
34                 if(doRedraw) {
35                         doRedraw = false;
36                         final int width = getWidth()-insets.left-insets.right;
37                         final int height = getHeight()-insets.top-insets.bottom;
38                         buffer = createImage(width, height);
39                         if(buffer == null)
40                                 return;
41                         final Graphics graphics=buffer.getGraphics();
42                         /* save original color */
43                         Color oldColor = graphics.getColor();
44                         graphics.setColor(getBackground());
45                         graphics.fillRect(insets.left, insets.top, width, height);
46                         /* restore original color */
47                         graphics.setColor(oldColor);
48                         offscreenPaint(graphics);
49                 }
50                 g.drawImage(buffer, insets.left, insets.top, null);
51         }
52         /**
53         * Updates the canvas.
54         */

55         public final void update(Graphics g) {
56                 paint(g);
57         }
58         /**
59         * Prints the canvas.
60         */

61         public final void printComponent(Graphics g) {
62                 offscreenPaint(g);
63         }
64         /**
65         * Double buffering cannot be controlled for this component.
66         * This method always throws an exception.
67         */

68         public final void setDoubleBuffered(boolean flag) {
69                 throw new IllegalArgumentException JavaDoc();
70         }
71         /**
72         * Double buffering is always enabled.
73         * @return true.
74         */

75         public final boolean isDoubleBuffered() {
76                 return true;
77         }
78         /**
79         * Redraws the canvas.
80         * This method may safely be called from outside the event-dispatching thread.
81         */

82         public final void redraw() {
83                 doRedraw = true;
84                 repaint();
85         }
86         /**
87         * Returns the offscreen graphics context or <code>null</code> if not available.
88         */

89         protected final Graphics getOffscreenGraphics() {
90                 return (buffer != null) ? buffer.getGraphics() : null;
91         }
92         /**
93         * Paints the canvas off-screen.
94         * Override this method instead of paint(Graphics g).
95         */

96         protected abstract void offscreenPaint(Graphics g);
97 }
98
99
Popular Tags