KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > VVCanvas


1 /*
2  * VVCanvas.java
3  *
4  * Created on 24 May 2005, 23:17
5  */

6
7 package com.thoughtriver.open.vectorvisuals;
8
9 import java.awt.*;
10 import java.awt.image.*;
11
12 import com.thoughtriver.open.vectorvisuals.task.*;
13
14 /**
15  * This is a <CODE>Canvas</CODE>-based area upon which the graphics used by
16  * the Vector Visuals system can be painted. An instance of this (or any other
17  * <CODE>VVViewPane</CODE> implementation) must be provided to the <CODE>VVDisplay</CODE>
18  * that is being used to control the vector world.
19  *
20  * @author Brandon Franklin
21  * @version $Date: 2006/11/25 09:18:36 $
22  */

23 public class VVCanvas extends Canvas implements VVViewPane {
24
25     private static final long serialVersionUID = 3904965243971908914L;
26
27     /**
28      * The visual buffering strategy employed by this canvas to allow smooth
29      * animations
30      */

31     private BufferStrategy bufferStrategy = null;
32
33     /** The <CODE>VVDisplay</CODE> that is controlling the vector world */
34     private VVDisplay vvDisplay = null;
35
36     /**
37      * Creates a new instance of <CODE>VVCanvas</CODE>.
38      */

39     public VVCanvas() {
40     }
41
42     /**
43      * {@inheritDoc}
44      */

45     public void setVVDisplay(final VVDisplay vvDisplay) {
46         this.vvDisplay = vvDisplay;
47     }
48
49     /**
50      * Returns the appropriate <CODE>Graphics</CODE> context to paint on,
51      * taking into consideration the <CODE>Component</CODE>'s buffering
52      * strategy.
53      *
54      * @return the appropriate <CODE>Graphics</CODE> context to paint on
55      */

56     private Graphics getBufferedGraphics() {
57
58         // This ensures that this method only creates the BufferStrategy once
59
if (bufferStrategy != null) {
60             return bufferStrategy.getDrawGraphics();
61         }
62
63         try {
64             createBufferStrategy(2);
65             bufferStrategy = getBufferStrategy();
66         }
67         catch (IllegalStateException JavaDoc ise) {
68             bufferStrategy = null;
69             return null;
70         }
71
72         return getBufferedGraphics();
73     }
74
75     /**
76      * Causes the <CODE>VVCanvas</CODE> to render itself.
77      *
78      * @param g the <CODE>Graphics</CODE> context of the panel
79      */

80     @Override JavaDoc
81     public synchronized void paint(final Graphics g) {
82         update(g);
83     }
84
85     /**
86      * Causes the <CODE>Component</CODE> to render itself. In the Vector
87      * Visuals system, this method is called quite frequently in order to
88      * facilitate animations.
89      *
90      * @param g the <CODE>Graphics</CODE> context of the panel
91      */

92     @Override JavaDoc
93     public synchronized void update(final Graphics g) {
94         Graphics g2 = getBufferedGraphics();
95         Graphics graphics = (g2 == null ? g : g2);
96
97         if (vvDisplay != null) {
98             vvDisplay.paint(graphics);
99         }
100         else {
101             super.paint(graphics);
102         }
103
104         if (g2 != null) {
105             bufferStrategy.show();
106         }
107
108     }
109
110     /**
111      * Causes the <CODE>Component</CODE> to render itself. In the Vector
112      * Visuals system, this method is called quite frequently in order to
113      * facilitate animations.
114      */

115     @Override JavaDoc
116     public synchronized void repaint() {
117         if (vvDisplay.getTaskManager().getExecutionState() == ExecutionState.running) {
118             Graphics g = getBufferedGraphics();
119             if (g != null) {
120                 update(g);
121                 bufferStrategy.show();
122                 return;
123             }
124         }
125
126         super.repaint();
127     }
128
129 }
130
Popular Tags