Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/applet/Applet.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.applet;
27
28
import java.awt.AWTPermission;
29
import java.awt.Dimension;
30
import java.awt.GraphicsEnvironment;
31
import java.awt.HeadlessException;
32
import java.awt.Image;
33
import java.awt.Panel;
34
import java.awt.event.ComponentEvent;
35
import java.io.IOException;
36
import java.io.ObjectInputStream;
37
import java.io.Serial;
38
import java.net.MalformedURLException;
39
import java.net.URL;
40
import java.util.Locale;
41
42
import javax.accessibility.AccessibleContext;
43
import javax.accessibility.AccessibleRole;
44
import javax.accessibility.AccessibleState;
45
import javax.accessibility.AccessibleStateSet;
46
47
import com.sun.media.sound.JavaSoundAudioClip;
48
49
/**
50
* An applet is a small program that is intended not to be run on its own, but
51
* rather to be embedded inside another application.
52
* <p>
53
* The {@code Applet} class must be the superclass of any applet that is to be
54
* embedded in a Web page or viewed by the Java Applet Viewer. The
55
* {@code Applet} class provides a standard interface between applets and their
56
* environment.
57
*
58
* @author Arthur van Hoff
59
* @author Chris Warth
60
* @since 1.0
61
* @deprecated The Applet API is deprecated, no replacement.
62
*/
63
@Deprecated(since = "9", forRemoval = true)
64
@SuppressWarnings("removal")
65
public class Applet extends Panel {
66
67
/**
68
* Constructs a new Applet.
69
* <p>
70
* Note: Many methods in {@code java.applet.Applet} may be invoked by the
71
* applet only after the applet is fully constructed; applet should avoid
72
* calling methods in {@code java.applet.Applet} in the constructor.
73
*
74
* @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
75
* returns {@code true}
76
* @see java.awt.GraphicsEnvironment#isHeadless
77
* @since 1.4
78
*/
79
public Applet() throws HeadlessException {
80
if (GraphicsEnvironment.isHeadless()) {
81
throw new HeadlessException();
82
}
83
}
84
85
/**
86
* Applets can be serialized but the following conventions MUST be followed:
87
* <p>
88
* Before Serialization: An applet must be in STOPPED state.
89
* <p>
90
* After Deserialization: The applet will be restored in STOPPED state (and
91
* most clients will likely move it into RUNNING state). The stub field will
92
* be restored by the reader.
93
*/
94
private transient AppletStub stub;
95
96
/**
97
* Use serialVersionUID from JDK 1.0 for interoperability.
98
*/
99
@Serial
100
private static final long serialVersionUID = -5836846270535785031L;
101
102
/**
103
* Read an applet from an object input stream.
104
*
105
* @param s the {@code ObjectInputStream} to read
106
* @throws ClassNotFoundException if the class of a serialized object could
107
* not be found
108
* @throws IOException if an I/O error occurs
109
* @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
110
* returns {@code true}
111
* @serial
112
* @see java.awt.GraphicsEnvironment#isHeadless
113
* @since 1.4
114
*/
115
@Serial
116
private void readObject(ObjectInputStream s)
117
throws ClassNotFoundException, IOException, HeadlessException {
118
if (GraphicsEnvironment.isHeadless()) {
119
throw new HeadlessException();
120
}
121
s.defaultReadObject();
122
}
123
124
/**
125
* Sets this applet's stub. This is done automatically by the system.
126
* <p>
127
* If there is a security manager, its {@code checkPermission} method is
128
* called with the {@code AWTPermission("setAppletStub")} permission if a
129
* stub has already been set.
130
*
131
* @param stub the new stub
132
* @throws SecurityException if the caller cannot set the stub
133
*/
134
public final void setStub(AppletStub stub) {
135
if (this.stub != null) {
136
SecurityManager s = System.getSecurityManager();
137
if (s != null) {
138
s.checkPermission(new AWTPermission("setAppletStub"));
139
}
140
}
141
this.stub = stub;
142
}
143
144
/**
145
* Determines if this applet is active. An applet is marked active just
146
* before its {@code start} method is called. It becomes inactive just
147
* before its {@code stop} method is called.
148
*
149
* @return {@code true} if the applet is active; {@code false} otherwise
150
* @see java.applet.Applet#start()
151
* @see java.applet.Applet#stop()
152
*/
153
public boolean isActive() {
154
if (stub != null) {
155
return stub.isActive();
156
} else { // If stub field not filled in, applet never active
157
return false;
158
}
159
}
160
161
/**
162
* Gets the {@code URL} of the document in which this applet is embedded.
163
* For example, suppose an applet is contained within the document:
164
* <blockquote><pre>
165
* http://www.oracle.com/technetwork/java/index.html
166
* </pre></blockquote>
167
* The document base is:
168
* <blockquote><pre>
169
* http://www.oracle.com/technetwork/java/index.html
170
* </pre></blockquote>
171
*
172
* @return the {@link java.net.URL} of the document that contains this
173
* applet
174
* @see java.applet.Applet#getCodeBase()
175
*/
176
public URL getDocumentBase() {
177
return stub.getDocumentBase();
178
}
179
180
/**
181
* Gets the base {@code URL}. This is the {@code URL} of the directory which
182
* contains this applet.
183
*
184
* @return the base {@link java.net.URL} of the directory which contains
185
* this applet
186
* @see java.applet.Applet#getDocumentBase()
187
*/
188
public URL getCodeBase() {
189
return stub.getCodeBase();
190
}
191
192
/**
193
* Returns the value of the named parameter in the HTML tag. For example, if
194
* this applet is specified as
195
* <blockquote><pre>
196
* &lt;applet code="Clock" width=50 height=50&gt;
197
* &lt;param name=Color value="blue"&gt;
198
* &lt;/applet&gt;
199
* </pre></blockquote>
200
* <p>
201
* then a call to {@code getParameter("Color")} returns the value
202
* {@code "blue"}.
203
* <p>
204
* The {@code name} argument is case insensitive.
205
*
206
* @param name a parameter name
207
* @return the value of the named parameter, or {@code null} if not set
208
*/
209
public String getParameter(String name) {
210
return stub.getParameter(name);
211
}
212
213
/**
214
* Determines this applet's context, which allows the applet to query and
215
* affect the environment in which it runs.
216
* <p>
217
* This environment of an applet represents the document that contains the
218
* applet.
219
*
220
* @return the applet's context
221
*/
222
public AppletContext getAppletContext() {
223
return stub.getAppletContext();
224
}
225
226
/**
227
* Requests that this applet be resized.
228
*
229
* @param width the new requested width for the applet
230
* @param height the new requested height for the applet
231
*/
232
@SuppressWarnings("deprecation")
233
public void resize(int width, int height) {
234
Dimension d = size();
235
if ((d.width != width) || (d.height != height)) {
236
super.resize(width, height);
237
if (stub != null) {
238
stub.appletResize(width, height);
239
}
240
}
241
}
242
243
/**
244
* Requests that this applet be resized.
245
*
246
* @param d an object giving the new width and height
247
*/
248
@SuppressWarnings("deprecation")
249
public void resize(Dimension d) {
250
resize(d.width, d.height);
251
}
252
253
/**
254
* Indicates if this container is a validate root.
255
* <p>
256
* {@code Applet} objects are the validate roots, and, therefore, they
257
* override this method to return {@code true}.
258
*
259
* @return {@code true}
260
* @see java.awt.Container#isValidateRoot
261
* @since 1.7
262
*/
263
@Override
264
public boolean isValidateRoot() {
265
return true;
266
}
267
268
/**
269
* Requests that the argument string be displayed in the "status window".
270
* Many browsers and applet viewers provide such a window, where the
271
* application can inform users of its current state.
272
*
273
* @param msg a string to display in the status window
274
*/
275
public void showStatus(String msg) {
276
getAppletContext().showStatus(msg);
277
}
278
279
/**
280
* Returns an {@code Image} object that can then be painted on the screen.
281
* The {@code url} that is passed as an argument must specify an absolute
282
* {@code URL}.
283
* <p>
284
* This method always returns immediately, whether or not the image exists.
285
* When this applet attempts to draw the image on the screen, the data will
286
* be loaded. The graphics primitives that draw the image will incrementally
287
* paint on the screen.
288
*
289
* @param url an absolute {@code URL} giving the location of the image
290
* @return the image at the specified {@code URL}
291
* @see java.awt.Image
292
*/
293
public Image getImage(URL url) {
294
return getAppletContext().getImage(url);
295
}
296
297
/**
298
* Returns an {@code Image} object that can then be painted on the screen.
299
* The {@code url} argument must specify an absolute {@code URL}. The
300
* {@code name} argument is a specifier that is relative to the {@code url}
301
* argument.
302
* <p>
303
* This method always returns immediately, whether or not the image exists.
304
* When this applet attempts to draw the image on the screen, the data will
305
* be loaded. The graphics primitives that draw the image will incrementally
306
* paint on the screen.
307
*
308
* @param url an absolute URL giving the base location of the image
309
* @param name the location of the image, relative to the {@code url}
310
* argument
311
* @return the image at the specified {@code URL}
312
* @see java.awt.Image
313
*/
314
public Image getImage(URL url, String name) {
315
try {
316
return getImage(new URL(url, name));
317
} catch (MalformedURLException e) {
318
return null;
319
}
320
}
321
322
/**
323
* Get an audio clip from the given {@code URL}.
324
*
325
* @param url points to the audio clip
326
* @return the audio clip at the specified {@code URL}
327
* @since 1.2
328
*/
329
public static final AudioClip newAudioClip(URL url) {
330
return JavaSoundAudioClip.create(url);
331
}
332
333
/**
334
* Returns the {@code AudioClip} object specified by the {@code URL}
335
* argument.
336
* <p>
337
* This method always returns immediately, whether or not the audio clip
338
* exists. When this applet attempts to play the audio clip, the data will
339
* be loaded.
340
*
341
* @param url an absolute {@code URL} giving the location of the audio clip
342
* @return the audio clip at the specified {@code URL}
343
* @see java.applet.AudioClip
344
*/
345
public AudioClip getAudioClip(URL url) {
346
return getAppletContext().getAudioClip(url);
347
}
348
349
/**
350
* Returns the {@code AudioClip} object specified by the {@code URL} and
351
* {@code name} arguments.
352
* <p>
353
* This method always returns immediately, whether or not the audio clip
354
* exists. When this applet attempts to play the audio clip, the data will
355
* be loaded.
356
*
357
* @param url an absolute {@code URL} giving the base location of the audio
358
* clip
359
* @param name the location of the audio clip, relative to the {@code url}
360
* argument
361
* @return the audio clip at the specified {@code URL}
362
* @see java.applet.AudioClip
363
*/
364
public AudioClip getAudioClip(URL url, String name) {
365
try {
366
return getAudioClip(new URL(url, name));
367
} catch (MalformedURLException e) {
368
return null;
369
}
370
}
371
372
/**
373
* Returns information about this applet. An applet should override this
374
* method to return a {@code String} containing information about the
375
* author, version, and copyright of the applet.
376
* <p>
377
* The implementation of this method provided by the {@code Applet} class
378
* returns {@code null}.
379
*
380
* @return a string containing information about the author, version, and
381
* copyright of the applet
382
*/
383
public String getAppletInfo() {
384
return null;
385
}
386
387
/**
388
* Gets the locale of the applet. It allows the applet to maintain its own
389
* locale separated from the locale of the browser or appletviewer.
390
*
391
* @return the locale of the applet; if no locale has been set, the default
392
* locale is returned
393
* @since 1.1
394
*/
395
public Locale getLocale() {
396
Locale locale = super.getLocale();
397
if (locale == null) {
398
return Locale.getDefault();
399
}
400
return locale;
401
}
402
403
/**
404
* Returns information about the parameters that are understood by this
405
* applet. An applet should override this method to return an array of
406
* strings describing these parameters.
407
* <p>
408
* Each element of the array should be a set of three strings containing the
409
* name, the type, and a description. For example:
410
* <blockquote><pre>
411
* String pinfo[][] = {
412
* {"fps", "1-10", "frames per second"},
413
* {"repeat", "boolean", "repeat image loop"},
414
* {"imgs", "url", "images directory"}
415
* };
416
* </pre></blockquote>
417
* <p>
418
* The implementation of this method provided by the {@code Applet} class
419
* returns {@code null}.
420
*
421
* @return an array describing the parameters this applet looks for
422
*/
423
public String[][] getParameterInfo() {
424
return null;
425
}
426
427
/**
428
* Plays the audio clip at the specified absolute {@code URL}. Nothing
429
* happens if the audio clip cannot be found.
430
*
431
* @param url an absolute {@code URL} giving the location of the audio clip
432
*/
433
public void play(URL url) {
434
AudioClip clip = getAudioClip(url);
435
if (clip != null) {
436
clip.play();
437
}
438
}
439
440
/**
441
* Plays the audio clip given the {@code URL} and a specifier that is
442
* relative to it. Nothing happens if the audio clip cannot be found.
443
*
444
* @param url an absolute {@code URL} giving the base location of the audio
445
* clip
446
* @param name the location of the audio clip, relative to the {@code url}
447
* argument
448
*/
449
public void play(URL url, String name) {
450
AudioClip clip = getAudioClip(url, name);
451
if (clip != null) {
452
clip.play();
453
}
454
}
455
456
/**
457
* Called by the browser or applet viewer to inform this applet that it has
458
* been loaded into the system. It is always called before the first time
459
* that the {@code start} method is called.
460
* <p>
461
* A subclass of {@code Applet} should override this method if it has
462
* initialization to perform. For example, an applet with threads would use
463
* the {@code init} method to create the threads and the {@code destroy}
464
* method to kill them.
465
* <p>
466
* The implementation of this method provided by the {@code Applet} class
467
* does nothing.
468
*
469
* @see java.applet.Applet#destroy()
470
* @see java.applet.Applet#start()
471
* @see java.applet.Applet#stop()
472
*/
473
public void init() {
474
}
475
476
/**
477
* Called by the browser or applet viewer to inform this applet that it
478
* should start its execution. It is called after the {@code init} method
479
* and each time the applet is revisited in a Web page.
480
* <p>
481
* A subclass of {@code Applet} should override this method if it has any
482
* operation that it wants to perform each time the Web page containing it
483
* is visited. For example, an applet with animation might want to use the
484
* {@code start} method to resume animation, and the {@code stop} method to
485
* suspend the animation.
486
* <p>
487
* Note: some methods, such as {@code getLocationOnScreen}, can only provide
488
* meaningful results if the applet is showing. Because {@code isShowing}
489
* returns {@code false} when the applet's {@code start} is first called,
490
* methods requiring {@code isShowing} to return {@code true} should be
491
* called from a {@code ComponentListener}.
492
* <p>
493
* The implementation of this method provided by the {@code Applet} class
494
* does nothing.
495
*
496
* @see java.applet.Applet#destroy()
497
* @see java.applet.Applet#init()
498
* @see java.applet.Applet#stop()
499
* @see java.awt.Component#isShowing()
500
* @see java.awt.event.ComponentListener#componentShown(ComponentEvent)
501
*/
502
public void start() {
503
}
504
505
/**
506
* Called by the browser or applet viewer to inform this applet that it
507
* should stop its execution. It is called when the Web page that contains
508
* this applet has been replaced by another page, and also just before the
509
* applet is to be destroyed.
510
* <p>
511
* A subclass of {@code Applet} should override this method if it has any
512
* operation that it wants to perform each time the Web page containing it
513
* is no longer visible. For example, an applet with animation might want to
514
* use the {@code start} method to resume animation, and the {@code stop}
515
* method to suspend the animation.
516
* <p>
517
* The implementation of this method provided by the {@code Applet} class
518
* does nothing.
519
*
520
* @see java.applet.Applet#destroy()
521
* @see java.applet.Applet#init()
522
*/
523
public void stop() {
524
}
525
526
/**
527
* Called by the browser or applet viewer to inform this applet that it is
528
* being reclaimed and that it should destroy any resources that it has
529
* allocated. The {@code stop} method will always be called before
530
* {@code destroy}.
531
* <p>
532
* A subclass of {@code Applet} should override this method if it has any
533
* operation that it wants to perform before it is destroyed. For example,
534
* an applet with threads would use the {@code init} method to create the
535
* threads and the {@code destroy} method to kill them.
536
* <p>
537
* The implementation of this method provided by the {@code Applet} class
538
* does nothing.
539
*
540
* @see java.applet.Applet#init()
541
* @see java.applet.Applet#start()
542
* @see java.applet.Applet#stop()
543
*/
544
public void destroy() {
545
}
546
547
//
548
// Accessibility support
549
//
550
551
/**
552
* The accessible context associated with this {@code Applet}.
553
*/
554
@SuppressWarnings("serial") // Not statically typed as Serializable
555
AccessibleContext accessibleContext = null;
556
557
/**
558
* Gets the {@code AccessibleContext} associated with this {@code Applet}.
559
* For applets, the {@code AccessibleContext} takes the form of an
560
* {@code AccessibleApplet}. A new {@code AccessibleApplet} instance is
561
* created if necessary.
562
*
563
* @return an {@code AccessibleApplet} that serves as the
564
* {@code AccessibleContext} of this {@code Applet}
565
* @since 1.3
566
*/
567
public AccessibleContext getAccessibleContext() {
568
if (accessibleContext == null) {
569
accessibleContext = new AccessibleApplet();
570
}
571
return accessibleContext;
572
}
573
574
/**
575
* This class implements accessibility support for the {@code Applet} class.
576
* It provides an implementation of the Java Accessibility API appropriate
577
* to applet user-interface elements.
578
*
579
* @since 1.3
580
*/
581
protected class AccessibleApplet extends AccessibleAWTPanel {
582
583
/**
584
* Use serialVersionUID from JDK 1.3 for interoperability.
585
*/
586
@Serial
587
private static final long serialVersionUID = 8127374778187708896L;
588
589
/**
590
* Constructs an {@code AccessibleApplet}.
591
*/
592
protected AccessibleApplet() {}
593
594
/**
595
* Get the role of this object.
596
*
597
* @return an instance of {@code AccessibleRole} describing the role of
598
* the object
599
*/
600
public AccessibleRole getAccessibleRole() {
601
return AccessibleRole.FRAME;
602
}
603
604
/**
605
* Get the state of this object.
606
*
607
* @return an instance of {@code AccessibleStateSet} containing the
608
* current state set of the object
609
* @see AccessibleState
610
*/
611
public AccessibleStateSet getAccessibleStateSet() {
612
AccessibleStateSet states = super.getAccessibleStateSet();
613
states.add(AccessibleState.ACTIVE);
614
return states;
615
}
616
}
617
}
618
619