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;
|
|
28 |
import javax.accessibility.*;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* A <code>Canvas</code> component represents a blank rectangular
|
|
32 |
* area of the screen onto which the application can draw or from
|
|
33 |
* which the application can trap input events from the user.
|
|
34 |
* <p>
|
|
35 |
* An application must subclass the <code>Canvas</code> class in
|
|
36 |
* order to get useful functionality such as creating a custom
|
|
37 |
* component. The <code>paint</code> method must be overridden
|
|
38 |
* in order to perform custom graphics on the canvas.
|
|
39 |
*
|
|
40 |
* @author Sami Shaio
|
|
41 |
* @since JDK1.0
|
|
42 |
*/
|
|
43 |
public class Canvas extends Component implements Accessible {
|
|
44 |
|
|
45 |
private static final String base = "canvas";
|
|
46 |
private static int nameCounter = 0;
|
|
47 |
|
|
48 |
/*
|
|
49 |
* JDK 1.1 serialVersionUID
|
|
50 |
*/
|
|
51 |
private static final long serialVersionUID = -2284879212465893870L;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* Constructs a new Canvas.
|
|
55 |
*/
|
|
56 |
public Canvas() {
|
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Constructs a new Canvas given a GraphicsConfiguration object.
|
|
61 |
*
|
|
62 |
* @param config a reference to a GraphicsConfiguration object.
|
|
63 |
*
|
|
64 |
* @see GraphicsConfiguration
|
|
65 |
*/
|
|
66 |
public Canvas(GraphicsConfiguration config) {
|
|
67 |
this();
|
|
68 |
graphicsConfig = config;
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Construct a name for this component. Called by getName() when the
|
|
73 |
* name is null.
|
|
74 |
*/
|
|
75 |
String constructComponentName() {
|
|
76 |
synchronized (Canvas.class) {
|
|
77 |
return base + nameCounter++;
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Creates the peer of the canvas. This peer allows you to change the
|
|
83 |
* user interface of the canvas without changing its functionality.
|
|
84 |
* @see java.awt.Toolkit#createCanvas(java.awt.Canvas)
|
|
85 |
* @see java.awt.Component#getToolkit()
|
|
86 |
*/
|
|
87 |
public void addNotify() {
|
|
88 |
synchronized (getTreeLock()) {
|
|
89 |
if (peer == null)
|
|
90 |
peer = getToolkit().createCanvas(this);
|
|
91 |
super.addNotify();
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Paints this canvas.
|
|
97 |
* <p>
|
|
98 |
* Most applications that subclass <code>Canvas</code> should
|
|
99 |
* override this method in order to perform some useful operation
|
|
100 |
* (typically, custom painting of the canvas).
|
|
101 |
* The default operation is simply to clear the canvas.
|
|
102 |
* Applications that override this method need not call
|
|
103 |
* super.paint(g).
|
|
104 |
*
|
|
105 |
* @param g the specified Graphics context
|
|
106 |
* @see #update(Graphics)
|
|
107 |
* @see Component#paint(Graphics)
|
|
108 |
*/
|
|
109 |
public void paint(Graphics g) {
|
|
110 |
g.clearRect(0, 0, width, height);
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* Updates this canvas.
|
|
115 |
* <p>
|
|
116 |
* This method is called in response to a call to <code>repaint</code>.
|
|
117 |
* The canvas is first cleared by filling it with the background
|
|
118 |
* color, and then completely redrawn by calling this canvas's
|
|
119 |
* <code>paint</code> method.
|
|
120 |
* Note: applications that override this method should either call
|
|
121 |
* super.update(g) or incorporate the functionality described
|
|
122 |
* above into their own code.
|
|
123 |
*
|
|
124 |
* @param g the specified Graphics context
|
|
125 |
* @see #paint(Graphics)
|
|
126 |
* @see Component#update(Graphics)
|
|
127 |
*/
|
|
128 |
public void update(Graphics g) {
|
|
129 |
g.clearRect(0, 0, width, height);
|
|
130 |
paint(g);
|
|
131 |
}
|
|
132 |
|
|
133 |
boolean postsOldMouseEvents() {
|
|
134 |
return true;
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Creates a new strategy for multi-buffering on this component.
|
|
139 |
* Multi-buffering is useful for rendering performance. This method
|
|
140 |
* attempts to create the best strategy available with the number of
|
|
141 |
* buffers supplied. It will always create a <code>BufferStrategy</code>
|
|
142 |
* with that number of buffers.
|
|
143 |
* A page-flipping strategy is attempted first, then a blitting strategy
|
|
144 |
* using accelerated buffers. Finally, an unaccelerated blitting
|
|
145 |
* strategy is used.
|
|
146 |
* <p>
|
|
147 |
* Each time this method is called,
|
|
148 |
* the existing buffer strategy for this component is discarded.
|
|
149 |
* @param numBuffers number of buffers to create, including the front buffer
|
|
150 |
* @exception IllegalArgumentException if numBuffers is less than 1.
|
|
151 |
* @exception IllegalStateException if the component is not displayable
|
|
152 |
* @see #isDisplayable
|
|
153 |
* @see #getBufferStrategy
|
|
154 |
* @since 1.4
|
|
155 |
*/
|
|
156 |
public void createBufferStrategy(int numBuffers) {
|
|
157 |
super.createBufferStrategy(numBuffers);
|
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Creates a new strategy for multi-buffering on this component with the
|
|
162 |
* required buffer capabilities. This is useful, for example, if only
|
|
163 |
* accelerated memory or page flipping is desired (as specified by the
|
|
164 |
* buffer capabilities).
|
|
165 |
* <p>
|
|
166 |
* Each time this method
|
|
167 |
* is called, the existing buffer strategy for this component is discarded.
|
|
168 |
* @param numBuffers number of buffers to create
|
|
169 |
* @param caps the required capabilities for creating the buffer strategy;
|
|
170 |
* cannot be <code>null</code>
|
|
171 |
* @exception AWTException if the capabilities supplied could not be
|
|
172 |
* supported or met; this may happen, for example, if there is not enough
|
|
173 |
* accelerated memory currently available, or if page flipping is specified
|
|
174 |
* but not possible.
|
|
175 |
* @exception IllegalArgumentException if numBuffers is less than 1, or if
|
|
176 |
* caps is <code>null</code>
|
|
177 |
* @see #getBufferStrategy
|
|
178 |
* @since 1.4
|
|
179 |
*/
|
|
180 |
public void createBufferStrategy(int numBuffers,
|
|
181 |
BufferCapabilities caps) throws AWTException {
|
|
182 |
super.createBufferStrategy(numBuffers, caps);
|
|
183 |
}
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Returns the <code>BufferStrategy</code> used by this component. This
|
|
187 |
* method will return null if a <code>BufferStrategy</code> has not yet
|
|
188 |
* been created or has been disposed.
|
|
189 |
*
|
|
190 |
* @return the buffer strategy used by this component
|
|
191 |
* @see #createBufferStrategy
|
|
192 |
* @since 1.4
|
|
193 |
*/
|
|
194 |
public BufferStrategy getBufferStrategy() {
|
|
195 |
return super.getBufferStrategy();
|
|
196 |
}
|
|
197 |
|
|
198 |
/*
|
|
199 |
* --- Accessibility Support ---
|
|
200 |
*
|
|
201 |
*/
|
|
202 |
|
|
203 |
/**
|
|
204 |
* Gets the AccessibleContext associated with this Canvas.
|
|
205 |
* For canvases, the AccessibleContext takes the form of an
|
|
206 |
* AccessibleAWTCanvas.
|
|
207 |
* A new AccessibleAWTCanvas instance is created if necessary.
|
|
208 |
*
|
|
209 |
* @return an AccessibleAWTCanvas that serves as the
|
|
210 |
* AccessibleContext of this Canvas
|
|
211 |
* @since 1.3
|
|
212 |
*/
|
|
213 |
public AccessibleContext getAccessibleContext() {
|
|
214 |
if (accessibleContext == null) {
|
|
215 |
accessibleContext = new AccessibleAWTCanvas();
|
|
216 |
}
|
|
217 |
return accessibleContext;
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* This class implements accessibility support for the
|
|
222 |
* <code>Canvas</code> class. It provides an implementation of the
|
|
223 |
* Java Accessibility API appropriate to canvas user-interface elements.
|
|
224 |
* @since 1.3
|
|
225 |
*/
|
|
226 |
protected class AccessibleAWTCanvas extends AccessibleAWTComponent
|
|
227 |
{
|
|
228 |
private static final long serialVersionUID = -6325592262103146699L;
|
|
229 |
|
|
230 |
/**
|
|
231 |
* Get the role of this object.
|
|
232 |
*
|
|
233 |
* @return an instance of AccessibleRole describing the role of the
|
|
234 |
* object
|
|
235 |
* @see AccessibleRole
|
|
236 |
*/
|
|
237 |
public AccessibleRole getAccessibleRole() {
|
|
238 |
return AccessibleRole.CANVAS;
|
|
239 |
}
|
|
240 |
|
|
241 |
} // inner class AccessibleAWTCanvas
|
|
242 |
}
|