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