author | anthony |
Fri, 20 Feb 2009 17:34:16 +0300 | |
changeset 2459 | 08f3416ff334 |
parent 2 | 90ce3da70b43 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
package java.awt; |
|
26 |
||
27 |
import java.awt.image.BufferStrategy; |
|
2459
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
28 |
import java.awt.peer.CanvasPeer; |
2 | 29 |
import javax.accessibility.*; |
30 |
||
31 |
/** |
|
32 |
* A <code>Canvas</code> component represents a blank rectangular |
|
33 |
* area of the screen onto which the application can draw or from |
|
34 |
* which the application can trap input events from the user. |
|
35 |
* <p> |
|
36 |
* An application must subclass the <code>Canvas</code> class in |
|
37 |
* order to get useful functionality such as creating a custom |
|
38 |
* component. The <code>paint</code> method must be overridden |
|
39 |
* in order to perform custom graphics on the canvas. |
|
40 |
* |
|
41 |
* @author Sami Shaio |
|
42 |
* @since JDK1.0 |
|
43 |
*/ |
|
44 |
public class Canvas extends Component implements Accessible { |
|
45 |
||
46 |
private static final String base = "canvas"; |
|
47 |
private static int nameCounter = 0; |
|
48 |
||
49 |
/* |
|
50 |
* JDK 1.1 serialVersionUID |
|
51 |
*/ |
|
52 |
private static final long serialVersionUID = -2284879212465893870L; |
|
53 |
||
54 |
/** |
|
55 |
* Constructs a new Canvas. |
|
56 |
*/ |
|
57 |
public Canvas() { |
|
58 |
} |
|
59 |
||
60 |
/** |
|
61 |
* Constructs a new Canvas given a GraphicsConfiguration object. |
|
62 |
* |
|
63 |
* @param config a reference to a GraphicsConfiguration object. |
|
64 |
* |
|
65 |
* @see GraphicsConfiguration |
|
66 |
*/ |
|
67 |
public Canvas(GraphicsConfiguration config) { |
|
68 |
this(); |
|
2459
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
69 |
setGraphicsConfiguration(config); |
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
70 |
} |
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
71 |
|
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
72 |
@Override |
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
73 |
void setGraphicsConfiguration(GraphicsConfiguration gc) { |
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
74 |
CanvasPeer peer = (CanvasPeer)getPeer(); |
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
75 |
if (peer != null) { |
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
76 |
gc = peer.getAppropriateGraphicsConfiguration(gc); |
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
77 |
} |
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
78 |
|
08f3416ff334
6804747: Ensure consistent graphicsConfig member across components hierarchy
anthony
parents:
2
diff
changeset
|
79 |
super.setGraphicsConfiguration(gc); |
2 | 80 |
} |
81 |
||
82 |
/** |
|
83 |
* Construct a name for this component. Called by getName() when the |
|
84 |
* name is null. |
|
85 |
*/ |
|
86 |
String constructComponentName() { |
|
87 |
synchronized (Canvas.class) { |
|
88 |
return base + nameCounter++; |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Creates the peer of the canvas. This peer allows you to change the |
|
94 |
* user interface of the canvas without changing its functionality. |
|
95 |
* @see java.awt.Toolkit#createCanvas(java.awt.Canvas) |
|
96 |
* @see java.awt.Component#getToolkit() |
|
97 |
*/ |
|
98 |
public void addNotify() { |
|
99 |
synchronized (getTreeLock()) { |
|
100 |
if (peer == null) |
|
101 |
peer = getToolkit().createCanvas(this); |
|
102 |
super.addNotify(); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
/** |
|
107 |
* Paints this canvas. |
|
108 |
* <p> |
|
109 |
* Most applications that subclass <code>Canvas</code> should |
|
110 |
* override this method in order to perform some useful operation |
|
111 |
* (typically, custom painting of the canvas). |
|
112 |
* The default operation is simply to clear the canvas. |
|
113 |
* Applications that override this method need not call |
|
114 |
* super.paint(g). |
|
115 |
* |
|
116 |
* @param g the specified Graphics context |
|
117 |
* @see #update(Graphics) |
|
118 |
* @see Component#paint(Graphics) |
|
119 |
*/ |
|
120 |
public void paint(Graphics g) { |
|
121 |
g.clearRect(0, 0, width, height); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Updates this canvas. |
|
126 |
* <p> |
|
127 |
* This method is called in response to a call to <code>repaint</code>. |
|
128 |
* The canvas is first cleared by filling it with the background |
|
129 |
* color, and then completely redrawn by calling this canvas's |
|
130 |
* <code>paint</code> method. |
|
131 |
* Note: applications that override this method should either call |
|
132 |
* super.update(g) or incorporate the functionality described |
|
133 |
* above into their own code. |
|
134 |
* |
|
135 |
* @param g the specified Graphics context |
|
136 |
* @see #paint(Graphics) |
|
137 |
* @see Component#update(Graphics) |
|
138 |
*/ |
|
139 |
public void update(Graphics g) { |
|
140 |
g.clearRect(0, 0, width, height); |
|
141 |
paint(g); |
|
142 |
} |
|
143 |
||
144 |
boolean postsOldMouseEvents() { |
|
145 |
return true; |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Creates a new strategy for multi-buffering on this component. |
|
150 |
* Multi-buffering is useful for rendering performance. This method |
|
151 |
* attempts to create the best strategy available with the number of |
|
152 |
* buffers supplied. It will always create a <code>BufferStrategy</code> |
|
153 |
* with that number of buffers. |
|
154 |
* A page-flipping strategy is attempted first, then a blitting strategy |
|
155 |
* using accelerated buffers. Finally, an unaccelerated blitting |
|
156 |
* strategy is used. |
|
157 |
* <p> |
|
158 |
* Each time this method is called, |
|
159 |
* the existing buffer strategy for this component is discarded. |
|
160 |
* @param numBuffers number of buffers to create, including the front buffer |
|
161 |
* @exception IllegalArgumentException if numBuffers is less than 1. |
|
162 |
* @exception IllegalStateException if the component is not displayable |
|
163 |
* @see #isDisplayable |
|
164 |
* @see #getBufferStrategy |
|
165 |
* @since 1.4 |
|
166 |
*/ |
|
167 |
public void createBufferStrategy(int numBuffers) { |
|
168 |
super.createBufferStrategy(numBuffers); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Creates a new strategy for multi-buffering on this component with the |
|
173 |
* required buffer capabilities. This is useful, for example, if only |
|
174 |
* accelerated memory or page flipping is desired (as specified by the |
|
175 |
* buffer capabilities). |
|
176 |
* <p> |
|
177 |
* Each time this method |
|
178 |
* is called, the existing buffer strategy for this component is discarded. |
|
179 |
* @param numBuffers number of buffers to create |
|
180 |
* @param caps the required capabilities for creating the buffer strategy; |
|
181 |
* cannot be <code>null</code> |
|
182 |
* @exception AWTException if the capabilities supplied could not be |
|
183 |
* supported or met; this may happen, for example, if there is not enough |
|
184 |
* accelerated memory currently available, or if page flipping is specified |
|
185 |
* but not possible. |
|
186 |
* @exception IllegalArgumentException if numBuffers is less than 1, or if |
|
187 |
* caps is <code>null</code> |
|
188 |
* @see #getBufferStrategy |
|
189 |
* @since 1.4 |
|
190 |
*/ |
|
191 |
public void createBufferStrategy(int numBuffers, |
|
192 |
BufferCapabilities caps) throws AWTException { |
|
193 |
super.createBufferStrategy(numBuffers, caps); |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Returns the <code>BufferStrategy</code> used by this component. This |
|
198 |
* method will return null if a <code>BufferStrategy</code> has not yet |
|
199 |
* been created or has been disposed. |
|
200 |
* |
|
201 |
* @return the buffer strategy used by this component |
|
202 |
* @see #createBufferStrategy |
|
203 |
* @since 1.4 |
|
204 |
*/ |
|
205 |
public BufferStrategy getBufferStrategy() { |
|
206 |
return super.getBufferStrategy(); |
|
207 |
} |
|
208 |
||
209 |
/* |
|
210 |
* --- Accessibility Support --- |
|
211 |
* |
|
212 |
*/ |
|
213 |
||
214 |
/** |
|
215 |
* Gets the AccessibleContext associated with this Canvas. |
|
216 |
* For canvases, the AccessibleContext takes the form of an |
|
217 |
* AccessibleAWTCanvas. |
|
218 |
* A new AccessibleAWTCanvas instance is created if necessary. |
|
219 |
* |
|
220 |
* @return an AccessibleAWTCanvas that serves as the |
|
221 |
* AccessibleContext of this Canvas |
|
222 |
* @since 1.3 |
|
223 |
*/ |
|
224 |
public AccessibleContext getAccessibleContext() { |
|
225 |
if (accessibleContext == null) { |
|
226 |
accessibleContext = new AccessibleAWTCanvas(); |
|
227 |
} |
|
228 |
return accessibleContext; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* This class implements accessibility support for the |
|
233 |
* <code>Canvas</code> class. It provides an implementation of the |
|
234 |
* Java Accessibility API appropriate to canvas user-interface elements. |
|
235 |
* @since 1.3 |
|
236 |
*/ |
|
237 |
protected class AccessibleAWTCanvas extends AccessibleAWTComponent |
|
238 |
{ |
|
239 |
private static final long serialVersionUID = -6325592262103146699L; |
|
240 |
||
241 |
/** |
|
242 |
* Get the role of this object. |
|
243 |
* |
|
244 |
* @return an instance of AccessibleRole describing the role of the |
|
245 |
* object |
|
246 |
* @see AccessibleRole |
|
247 |
*/ |
|
248 |
public AccessibleRole getAccessibleRole() { |
|
249 |
return AccessibleRole.CANVAS; |
|
250 |
} |
|
251 |
||
252 |
} // inner class AccessibleAWTCanvas |
|
253 |
} |