Path: blob/master/src/java.desktop/share/classes/java/applet/Applet.java
41152 views
/*1* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.applet;2627import java.awt.AWTPermission;28import java.awt.Dimension;29import java.awt.GraphicsEnvironment;30import java.awt.HeadlessException;31import java.awt.Image;32import java.awt.Panel;33import java.awt.event.ComponentEvent;34import java.io.IOException;35import java.io.ObjectInputStream;36import java.io.Serial;37import java.net.MalformedURLException;38import java.net.URL;39import java.util.Locale;4041import javax.accessibility.AccessibleContext;42import javax.accessibility.AccessibleRole;43import javax.accessibility.AccessibleState;44import javax.accessibility.AccessibleStateSet;4546import com.sun.media.sound.JavaSoundAudioClip;4748/**49* An applet is a small program that is intended not to be run on its own, but50* rather to be embedded inside another application.51* <p>52* The {@code Applet} class must be the superclass of any applet that is to be53* embedded in a Web page or viewed by the Java Applet Viewer. The54* {@code Applet} class provides a standard interface between applets and their55* environment.56*57* @author Arthur van Hoff58* @author Chris Warth59* @since 1.060* @deprecated The Applet API is deprecated, no replacement.61*/62@Deprecated(since = "9", forRemoval = true)63@SuppressWarnings("removal")64public class Applet extends Panel {6566/**67* Constructs a new Applet.68* <p>69* Note: Many methods in {@code java.applet.Applet} may be invoked by the70* applet only after the applet is fully constructed; applet should avoid71* calling methods in {@code java.applet.Applet} in the constructor.72*73* @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}74* returns {@code true}75* @see java.awt.GraphicsEnvironment#isHeadless76* @since 1.477*/78public Applet() throws HeadlessException {79if (GraphicsEnvironment.isHeadless()) {80throw new HeadlessException();81}82}8384/**85* Applets can be serialized but the following conventions MUST be followed:86* <p>87* Before Serialization: An applet must be in STOPPED state.88* <p>89* After Deserialization: The applet will be restored in STOPPED state (and90* most clients will likely move it into RUNNING state). The stub field will91* be restored by the reader.92*/93private transient AppletStub stub;9495/**96* Use serialVersionUID from JDK 1.0 for interoperability.97*/98@Serial99private static final long serialVersionUID = -5836846270535785031L;100101/**102* Read an applet from an object input stream.103*104* @param s the {@code ObjectInputStream} to read105* @throws ClassNotFoundException if the class of a serialized object could106* not be found107* @throws IOException if an I/O error occurs108* @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}109* returns {@code true}110* @serial111* @see java.awt.GraphicsEnvironment#isHeadless112* @since 1.4113*/114@Serial115private void readObject(ObjectInputStream s)116throws ClassNotFoundException, IOException, HeadlessException {117if (GraphicsEnvironment.isHeadless()) {118throw new HeadlessException();119}120s.defaultReadObject();121}122123/**124* Sets this applet's stub. This is done automatically by the system.125* <p>126* If there is a security manager, its {@code checkPermission} method is127* called with the {@code AWTPermission("setAppletStub")} permission if a128* stub has already been set.129*130* @param stub the new stub131* @throws SecurityException if the caller cannot set the stub132*/133public final void setStub(AppletStub stub) {134if (this.stub != null) {135SecurityManager s = System.getSecurityManager();136if (s != null) {137s.checkPermission(new AWTPermission("setAppletStub"));138}139}140this.stub = stub;141}142143/**144* Determines if this applet is active. An applet is marked active just145* before its {@code start} method is called. It becomes inactive just146* before its {@code stop} method is called.147*148* @return {@code true} if the applet is active; {@code false} otherwise149* @see java.applet.Applet#start()150* @see java.applet.Applet#stop()151*/152public boolean isActive() {153if (stub != null) {154return stub.isActive();155} else { // If stub field not filled in, applet never active156return false;157}158}159160/**161* Gets the {@code URL} of the document in which this applet is embedded.162* For example, suppose an applet is contained within the document:163* <blockquote><pre>164* http://www.oracle.com/technetwork/java/index.html165* </pre></blockquote>166* The document base is:167* <blockquote><pre>168* http://www.oracle.com/technetwork/java/index.html169* </pre></blockquote>170*171* @return the {@link java.net.URL} of the document that contains this172* applet173* @see java.applet.Applet#getCodeBase()174*/175public URL getDocumentBase() {176return stub.getDocumentBase();177}178179/**180* Gets the base {@code URL}. This is the {@code URL} of the directory which181* contains this applet.182*183* @return the base {@link java.net.URL} of the directory which contains184* this applet185* @see java.applet.Applet#getDocumentBase()186*/187public URL getCodeBase() {188return stub.getCodeBase();189}190191/**192* Returns the value of the named parameter in the HTML tag. For example, if193* this applet is specified as194* <blockquote><pre>195* <applet code="Clock" width=50 height=50>196* <param name=Color value="blue">197* </applet>198* </pre></blockquote>199* <p>200* then a call to {@code getParameter("Color")} returns the value201* {@code "blue"}.202* <p>203* The {@code name} argument is case insensitive.204*205* @param name a parameter name206* @return the value of the named parameter, or {@code null} if not set207*/208public String getParameter(String name) {209return stub.getParameter(name);210}211212/**213* Determines this applet's context, which allows the applet to query and214* affect the environment in which it runs.215* <p>216* This environment of an applet represents the document that contains the217* applet.218*219* @return the applet's context220*/221public AppletContext getAppletContext() {222return stub.getAppletContext();223}224225/**226* Requests that this applet be resized.227*228* @param width the new requested width for the applet229* @param height the new requested height for the applet230*/231@SuppressWarnings("deprecation")232public void resize(int width, int height) {233Dimension d = size();234if ((d.width != width) || (d.height != height)) {235super.resize(width, height);236if (stub != null) {237stub.appletResize(width, height);238}239}240}241242/**243* Requests that this applet be resized.244*245* @param d an object giving the new width and height246*/247@SuppressWarnings("deprecation")248public void resize(Dimension d) {249resize(d.width, d.height);250}251252/**253* Indicates if this container is a validate root.254* <p>255* {@code Applet} objects are the validate roots, and, therefore, they256* override this method to return {@code true}.257*258* @return {@code true}259* @see java.awt.Container#isValidateRoot260* @since 1.7261*/262@Override263public boolean isValidateRoot() {264return true;265}266267/**268* Requests that the argument string be displayed in the "status window".269* Many browsers and applet viewers provide such a window, where the270* application can inform users of its current state.271*272* @param msg a string to display in the status window273*/274public void showStatus(String msg) {275getAppletContext().showStatus(msg);276}277278/**279* Returns an {@code Image} object that can then be painted on the screen.280* The {@code url} that is passed as an argument must specify an absolute281* {@code URL}.282* <p>283* This method always returns immediately, whether or not the image exists.284* When this applet attempts to draw the image on the screen, the data will285* be loaded. The graphics primitives that draw the image will incrementally286* paint on the screen.287*288* @param url an absolute {@code URL} giving the location of the image289* @return the image at the specified {@code URL}290* @see java.awt.Image291*/292public Image getImage(URL url) {293return getAppletContext().getImage(url);294}295296/**297* Returns an {@code Image} object that can then be painted on the screen.298* The {@code url} argument must specify an absolute {@code URL}. The299* {@code name} argument is a specifier that is relative to the {@code url}300* argument.301* <p>302* This method always returns immediately, whether or not the image exists.303* When this applet attempts to draw the image on the screen, the data will304* be loaded. The graphics primitives that draw the image will incrementally305* paint on the screen.306*307* @param url an absolute URL giving the base location of the image308* @param name the location of the image, relative to the {@code url}309* argument310* @return the image at the specified {@code URL}311* @see java.awt.Image312*/313public Image getImage(URL url, String name) {314try {315return getImage(new URL(url, name));316} catch (MalformedURLException e) {317return null;318}319}320321/**322* Get an audio clip from the given {@code URL}.323*324* @param url points to the audio clip325* @return the audio clip at the specified {@code URL}326* @since 1.2327*/328public static final AudioClip newAudioClip(URL url) {329return JavaSoundAudioClip.create(url);330}331332/**333* Returns the {@code AudioClip} object specified by the {@code URL}334* argument.335* <p>336* This method always returns immediately, whether or not the audio clip337* exists. When this applet attempts to play the audio clip, the data will338* be loaded.339*340* @param url an absolute {@code URL} giving the location of the audio clip341* @return the audio clip at the specified {@code URL}342* @see java.applet.AudioClip343*/344public AudioClip getAudioClip(URL url) {345return getAppletContext().getAudioClip(url);346}347348/**349* Returns the {@code AudioClip} object specified by the {@code URL} and350* {@code name} arguments.351* <p>352* This method always returns immediately, whether or not the audio clip353* exists. When this applet attempts to play the audio clip, the data will354* be loaded.355*356* @param url an absolute {@code URL} giving the base location of the audio357* clip358* @param name the location of the audio clip, relative to the {@code url}359* argument360* @return the audio clip at the specified {@code URL}361* @see java.applet.AudioClip362*/363public AudioClip getAudioClip(URL url, String name) {364try {365return getAudioClip(new URL(url, name));366} catch (MalformedURLException e) {367return null;368}369}370371/**372* Returns information about this applet. An applet should override this373* method to return a {@code String} containing information about the374* author, version, and copyright of the applet.375* <p>376* The implementation of this method provided by the {@code Applet} class377* returns {@code null}.378*379* @return a string containing information about the author, version, and380* copyright of the applet381*/382public String getAppletInfo() {383return null;384}385386/**387* Gets the locale of the applet. It allows the applet to maintain its own388* locale separated from the locale of the browser or appletviewer.389*390* @return the locale of the applet; if no locale has been set, the default391* locale is returned392* @since 1.1393*/394public Locale getLocale() {395Locale locale = super.getLocale();396if (locale == null) {397return Locale.getDefault();398}399return locale;400}401402/**403* Returns information about the parameters that are understood by this404* applet. An applet should override this method to return an array of405* strings describing these parameters.406* <p>407* Each element of the array should be a set of three strings containing the408* name, the type, and a description. For example:409* <blockquote><pre>410* String pinfo[][] = {411* {"fps", "1-10", "frames per second"},412* {"repeat", "boolean", "repeat image loop"},413* {"imgs", "url", "images directory"}414* };415* </pre></blockquote>416* <p>417* The implementation of this method provided by the {@code Applet} class418* returns {@code null}.419*420* @return an array describing the parameters this applet looks for421*/422public String[][] getParameterInfo() {423return null;424}425426/**427* Plays the audio clip at the specified absolute {@code URL}. Nothing428* happens if the audio clip cannot be found.429*430* @param url an absolute {@code URL} giving the location of the audio clip431*/432public void play(URL url) {433AudioClip clip = getAudioClip(url);434if (clip != null) {435clip.play();436}437}438439/**440* Plays the audio clip given the {@code URL} and a specifier that is441* relative to it. Nothing happens if the audio clip cannot be found.442*443* @param url an absolute {@code URL} giving the base location of the audio444* clip445* @param name the location of the audio clip, relative to the {@code url}446* argument447*/448public void play(URL url, String name) {449AudioClip clip = getAudioClip(url, name);450if (clip != null) {451clip.play();452}453}454455/**456* Called by the browser or applet viewer to inform this applet that it has457* been loaded into the system. It is always called before the first time458* that the {@code start} method is called.459* <p>460* A subclass of {@code Applet} should override this method if it has461* initialization to perform. For example, an applet with threads would use462* the {@code init} method to create the threads and the {@code destroy}463* method to kill them.464* <p>465* The implementation of this method provided by the {@code Applet} class466* does nothing.467*468* @see java.applet.Applet#destroy()469* @see java.applet.Applet#start()470* @see java.applet.Applet#stop()471*/472public void init() {473}474475/**476* Called by the browser or applet viewer to inform this applet that it477* should start its execution. It is called after the {@code init} method478* and each time the applet is revisited in a Web page.479* <p>480* A subclass of {@code Applet} should override this method if it has any481* operation that it wants to perform each time the Web page containing it482* is visited. For example, an applet with animation might want to use the483* {@code start} method to resume animation, and the {@code stop} method to484* suspend the animation.485* <p>486* Note: some methods, such as {@code getLocationOnScreen}, can only provide487* meaningful results if the applet is showing. Because {@code isShowing}488* returns {@code false} when the applet's {@code start} is first called,489* methods requiring {@code isShowing} to return {@code true} should be490* called from a {@code ComponentListener}.491* <p>492* The implementation of this method provided by the {@code Applet} class493* does nothing.494*495* @see java.applet.Applet#destroy()496* @see java.applet.Applet#init()497* @see java.applet.Applet#stop()498* @see java.awt.Component#isShowing()499* @see java.awt.event.ComponentListener#componentShown(ComponentEvent)500*/501public void start() {502}503504/**505* Called by the browser or applet viewer to inform this applet that it506* should stop its execution. It is called when the Web page that contains507* this applet has been replaced by another page, and also just before the508* applet is to be destroyed.509* <p>510* A subclass of {@code Applet} should override this method if it has any511* operation that it wants to perform each time the Web page containing it512* is no longer visible. For example, an applet with animation might want to513* use the {@code start} method to resume animation, and the {@code stop}514* method to suspend the animation.515* <p>516* The implementation of this method provided by the {@code Applet} class517* does nothing.518*519* @see java.applet.Applet#destroy()520* @see java.applet.Applet#init()521*/522public void stop() {523}524525/**526* Called by the browser or applet viewer to inform this applet that it is527* being reclaimed and that it should destroy any resources that it has528* allocated. The {@code stop} method will always be called before529* {@code destroy}.530* <p>531* A subclass of {@code Applet} should override this method if it has any532* operation that it wants to perform before it is destroyed. For example,533* an applet with threads would use the {@code init} method to create the534* threads and the {@code destroy} method to kill them.535* <p>536* The implementation of this method provided by the {@code Applet} class537* does nothing.538*539* @see java.applet.Applet#init()540* @see java.applet.Applet#start()541* @see java.applet.Applet#stop()542*/543public void destroy() {544}545546//547// Accessibility support548//549550/**551* The accessible context associated with this {@code Applet}.552*/553@SuppressWarnings("serial") // Not statically typed as Serializable554AccessibleContext accessibleContext = null;555556/**557* Gets the {@code AccessibleContext} associated with this {@code Applet}.558* For applets, the {@code AccessibleContext} takes the form of an559* {@code AccessibleApplet}. A new {@code AccessibleApplet} instance is560* created if necessary.561*562* @return an {@code AccessibleApplet} that serves as the563* {@code AccessibleContext} of this {@code Applet}564* @since 1.3565*/566public AccessibleContext getAccessibleContext() {567if (accessibleContext == null) {568accessibleContext = new AccessibleApplet();569}570return accessibleContext;571}572573/**574* This class implements accessibility support for the {@code Applet} class.575* It provides an implementation of the Java Accessibility API appropriate576* to applet user-interface elements.577*578* @since 1.3579*/580protected class AccessibleApplet extends AccessibleAWTPanel {581582/**583* Use serialVersionUID from JDK 1.3 for interoperability.584*/585@Serial586private static final long serialVersionUID = 8127374778187708896L;587588/**589* Constructs an {@code AccessibleApplet}.590*/591protected AccessibleApplet() {}592593/**594* Get the role of this object.595*596* @return an instance of {@code AccessibleRole} describing the role of597* the object598*/599public AccessibleRole getAccessibleRole() {600return AccessibleRole.FRAME;601}602603/**604* Get the state of this object.605*606* @return an instance of {@code AccessibleStateSet} containing the607* current state set of the object608* @see AccessibleState609*/610public AccessibleStateSet getAccessibleStateSet() {611AccessibleStateSet states = super.getAccessibleStateSet();612states.add(AccessibleState.ACTIVE);613return states;614}615}616}617618619