37 |
|
|
38 |
package java.awt; |
package java.awt; |
39 |
|
|
40 |
|
import java.awt.image.BufferStrategy; |
41 |
import java.awt.peer.ComponentPeer; |
import java.awt.peer.ComponentPeer; |
42 |
|
import java.io.Serializable; |
43 |
public class Canvas extends Component implements java.io.Serializable |
import javax.accessibility.Accessible; |
44 |
|
import javax.accessibility.AccessibleContext; |
45 |
|
import javax.accessibility.AccessibleRole; |
46 |
|
|
47 |
|
/** |
48 |
|
* The <code>Canvas</code> component provides a blank rectangular |
49 |
|
* area, which the client application can use for drawing and for |
50 |
|
* capturing events. By overriding the <code>paint()</code> method, |
51 |
|
* the canvas can be used for anything from simple line drawings to |
52 |
|
* full-scale custom components. |
53 |
|
* |
54 |
|
* @author Original author unknown |
55 |
|
* @author Tom Tromey <tromey@redhat.com> |
56 |
|
* @author Andrew John Hughes <gnu_andrew@member.fsf.org> |
57 |
|
* @since 1.0 |
58 |
|
*/ |
59 |
|
|
60 |
|
public class Canvas |
61 |
|
extends Component |
62 |
|
implements Serializable, Accessible |
63 |
{ |
{ |
64 |
|
|
65 |
|
/** |
66 |
|
* Compatible with Sun's JDK. |
67 |
|
*/ |
68 |
|
private static final long serialVersionUID = -2284879212465893870L; |
69 |
|
|
70 |
|
/** |
71 |
|
* The graphics configuration associated with the canvas. |
72 |
|
*/ |
73 |
transient GraphicsConfiguration graphicsConfiguration; |
transient GraphicsConfiguration graphicsConfiguration; |
74 |
|
|
75 |
/** |
/** |
76 |
|
* The buffer strategy associated with this canvas. |
77 |
|
*/ |
78 |
|
transient BufferStrategy bufferStrategy; |
79 |
|
|
80 |
|
/** |
81 |
* Initializes a new instance of <code>Canvas</code>. |
* Initializes a new instance of <code>Canvas</code>. |
82 |
*/ |
*/ |
83 |
public Canvas() { } |
public Canvas() |
84 |
|
{ |
85 |
|
} |
86 |
|
|
87 |
|
/** |
88 |
|
* Initializes a new instance of <code>Canvas</code> |
89 |
|
* with the supplied graphics configuration. |
90 |
|
* |
91 |
|
* @param graphicsConfiguration the graphics configuration to use |
92 |
|
* for this particular canvas. |
93 |
|
*/ |
94 |
public Canvas(GraphicsConfiguration graphicsConfiguration) |
public Canvas(GraphicsConfiguration graphicsConfiguration) |
95 |
{ |
{ |
96 |
this.graphicsConfiguration = graphicsConfiguration; |
this.graphicsConfiguration = graphicsConfiguration; |
114 |
} |
} |
115 |
|
|
116 |
/** |
/** |
117 |
* Repaints the canvas window. This method should be overriden by |
* Repaints the canvas window. This method should be overridden by |
118 |
* a subclass to do something useful, as this method simply paints |
* a subclass to do something useful, as this method simply paints |
119 |
* the window with the background color. |
* the window with the background color. |
120 |
*/ |
*/ |
129 |
gfx.fillRect(0, 0, size.width, size.height); |
gfx.fillRect(0, 0, size.width, size.height); |
130 |
} |
} |
131 |
|
|
132 |
// Serialization constant |
|
133 |
private static final long serialVersionUID = -2284879212465893870L; |
/** |
134 |
|
* This class provides accessibility support for the canvas. |
135 |
|
*/ |
136 |
|
protected class AccessibleAWTCanvas |
137 |
|
extends AccessibleAWTComponent |
138 |
|
{ |
139 |
|
|
140 |
|
/** |
141 |
|
* Constructor for the accessible canvas. |
142 |
|
*/ |
143 |
|
protected AccessibleAWTCanvas() |
144 |
|
{ |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
|
* Returns the accessible role for the canvas. |
149 |
|
* |
150 |
|
* @return an instance of <code>AccessibleRole</code>, describing |
151 |
|
* the role of the canvas. |
152 |
|
*/ |
153 |
|
public AccessibleRole getAccessibleRole() |
154 |
|
{ |
155 |
|
return AccessibleRole.CANVAS; |
156 |
|
} |
157 |
|
|
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
|
* Gets the AccessibleContext associated with this <code>Canvas</code>. |
162 |
|
* The context is created, if necessary. |
163 |
|
* |
164 |
|
* @return the associated context |
165 |
|
*/ |
166 |
|
public AccessibleContext getAccessibleContext() |
167 |
|
{ |
168 |
|
/* Create the context if this is the first request */ |
169 |
|
if (accessibleContext == null) |
170 |
|
{ |
171 |
|
/* Create the context */ |
172 |
|
accessibleContext = new AccessibleAWTCanvas(); |
173 |
|
} |
174 |
|
return accessibleContext; |
175 |
|
} |
176 |
|
|
177 |
|
/** |
178 |
|
* Returns the buffer strategy used by the canvas. |
179 |
|
* |
180 |
|
* @return the buffer strategy. |
181 |
|
* @since 1.4 |
182 |
|
*/ |
183 |
|
public BufferStrategy getBufferStrategy() |
184 |
|
{ |
185 |
|
return bufferStrategy; |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
|
* Updates the canvas in response to a request to |
190 |
|
* <code>repaint()</code> it. The canvas is cleared |
191 |
|
* with the current background colour, before <code>paint()</code> |
192 |
|
* is called to add the new contents. Subclasses |
193 |
|
* which override this method should either call this |
194 |
|
* method via <code>super.update(graphics)</code> or re-implement |
195 |
|
* this behaviour, so as to ensure that the canvas is |
196 |
|
* clear before painting takes place. |
197 |
|
* |
198 |
|
* @param graphics the graphics context. |
199 |
|
*/ |
200 |
|
public void update(Graphics graphics) |
201 |
|
{ |
202 |
|
Dimension size; |
203 |
|
|
204 |
|
/* Clear the canvas */ |
205 |
|
size = getSize(); |
206 |
|
graphics.clearRect(0, 0, size.width, size.height); |
207 |
|
/* Call the paint method */ |
208 |
|
paint(graphics); |
209 |
|
} |
210 |
|
|
211 |
} |
} |