Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/applet/Applet.java
38829 views
/*1* Copyright (c) 1995, 2013, 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*/24package java.applet;2526import java.awt.*;27import java.awt.image.ColorModel;28import java.io.IOException;29import java.io.ObjectInputStream;30import java.net.URL;31import java.net.MalformedURLException;32import java.util.Hashtable;33import java.util.Locale;34import javax.accessibility.*;3536/**37* An applet is a small program that is intended not to be run on38* its own, but rather to be embedded inside another application.39* <p>40* The <code>Applet</code> class must be the superclass of any41* applet that is to be embedded in a Web page or viewed by the Java42* Applet Viewer. The <code>Applet</code> class provides a standard43* interface between applets and their environment.44*45* @author Arthur van Hoff46* @author Chris Warth47* @since JDK1.048*/49public class Applet extends Panel {5051/**52* Constructs a new Applet.53* <p>54* Note: Many methods in <code>java.applet.Applet</code>55* may be invoked by the applet only after the applet is56* fully constructed; applet should avoid calling methods57* in <code>java.applet.Applet</code> in the constructor.58*59* @exception HeadlessException if GraphicsEnvironment.isHeadless()60* returns true.61* @see java.awt.GraphicsEnvironment#isHeadless62* @since 1.463*/64public Applet() throws HeadlessException {65if (GraphicsEnvironment.isHeadless()) {66throw new HeadlessException();67}68}6970/**71* Applets can be serialized but the following conventions MUST be followed:72*73* Before Serialization:74* An applet must be in STOPPED state.75*76* After Deserialization:77* The applet will be restored in STOPPED state (and most clients will78* likely move it into RUNNING state).79* The stub field will be restored by the reader.80*/81transient private AppletStub stub;8283/* version ID for serialized form. */84private static final long serialVersionUID = -5836846270535785031L;8586/**87* Read an applet from an object input stream.88* @exception HeadlessException if89* <code>GraphicsEnvironment.isHeadless()</code> returns90* <code>true</code>91* @serial92* @see java.awt.GraphicsEnvironment#isHeadless93* @since 1.494*/95private void readObject(ObjectInputStream s)96throws ClassNotFoundException, IOException, HeadlessException {97if (GraphicsEnvironment.isHeadless()) {98throw new HeadlessException();99}100s.defaultReadObject();101}102103/**104* Sets this applet's stub. This is done automatically by the system.105* <p>If there is a security manager, its <code> checkPermission </code>106* method is called with the107* <code>AWTPermission("setAppletStub")</code>108* permission if a stub has already been set.109* @param stub the new stub.110* @exception SecurityException if the caller cannot set the stub111*/112public final void setStub(AppletStub stub) {113if (this.stub != null) {114SecurityManager s = System.getSecurityManager();115if (s != null) {116s.checkPermission(new AWTPermission("setAppletStub"));117}118}119this.stub = stub;120}121122/**123* Determines if this applet is active. An applet is marked active124* just before its <code>start</code> method is called. It becomes125* inactive just before its <code>stop</code> method is called.126*127* @return <code>true</code> if the applet is active;128* <code>false</code> otherwise.129* @see java.applet.Applet#start()130* @see java.applet.Applet#stop()131*/132public boolean isActive() {133if (stub != null) {134return stub.isActive();135} else { // If stub field not filled in, applet never active136return false;137}138}139140/**141* Gets the URL of the document in which this applet is embedded.142* For example, suppose an applet is contained143* within the document:144* <blockquote><pre>145* http://www.oracle.com/technetwork/java/index.html146* </pre></blockquote>147* The document base is:148* <blockquote><pre>149* http://www.oracle.com/technetwork/java/index.html150* </pre></blockquote>151*152* @return the {@link java.net.URL} of the document that contains this153* applet.154* @see java.applet.Applet#getCodeBase()155*/156public URL getDocumentBase() {157return stub.getDocumentBase();158}159160/**161* Gets the base URL. This is the URL of the directory which contains this applet.162*163* @return the base {@link java.net.URL} of164* the directory which contains this applet.165* @see java.applet.Applet#getDocumentBase()166*/167public URL getCodeBase() {168return stub.getCodeBase();169}170171/**172* Returns the value of the named parameter in the HTML tag. For173* example, if this applet is specified as174* <blockquote><pre>175* <applet code="Clock" width=50 height=50>176* <param name=Color value="blue">177* </applet>178* </pre></blockquote>179* <p>180* then a call to <code>getParameter("Color")</code> returns the181* value <code>"blue"</code>.182* <p>183* The <code>name</code> argument is case insensitive.184*185* @param name a parameter name.186* @return the value of the named parameter,187* or <code>null</code> if not set.188*/189public String getParameter(String name) {190return stub.getParameter(name);191}192193/**194* Determines this applet's context, which allows the applet to195* query and affect the environment in which it runs.196* <p>197* This environment of an applet represents the document that198* contains the applet.199*200* @return the applet's context.201*/202public AppletContext getAppletContext() {203return stub.getAppletContext();204}205206/**207* Requests that this applet be resized.208*209* @param width the new requested width for the applet.210* @param height the new requested height for the applet.211*/212@SuppressWarnings("deprecation")213public void resize(int width, int height) {214Dimension d = size();215if ((d.width != width) || (d.height != height)) {216super.resize(width, height);217if (stub != null) {218stub.appletResize(width, height);219}220}221}222223/**224* Requests that this applet be resized.225*226* @param d an object giving the new width and height.227*/228@SuppressWarnings("deprecation")229public void resize(Dimension d) {230resize(d.width, d.height);231}232233/**234* Indicates if this container is a validate root.235* <p>236* {@code Applet} objects are the validate roots, and, therefore, they237* override this method to return {@code true}.238*239* @return {@code true}240* @since 1.7241* @see java.awt.Container#isValidateRoot242*/243@Override244public boolean isValidateRoot() {245return true;246}247248/**249* Requests that the argument string be displayed in the250* "status window". Many browsers and applet viewers251* provide such a window, where the application can inform users of252* its current state.253*254* @param msg a string to display in the status window.255*/256public void showStatus(String msg) {257getAppletContext().showStatus(msg);258}259260/**261* Returns an <code>Image</code> object that can then be painted on262* the screen. The <code>url</code> that is passed as an argument263* must specify an absolute URL.264* <p>265* This method always returns immediately, whether or not the image266* exists. When this applet attempts to draw the image on the screen,267* the data will be loaded. The graphics primitives that draw the268* image will incrementally paint on the screen.269*270* @param url an absolute URL giving the location of the image.271* @return the image at the specified URL.272* @see java.awt.Image273*/274public Image getImage(URL url) {275return getAppletContext().getImage(url);276}277278/**279* Returns an <code>Image</code> object that can then be painted on280* the screen. The <code>url</code> argument must specify an absolute281* URL. The <code>name</code> argument is a specifier that is282* relative to the <code>url</code> argument.283* <p>284* This method always returns immediately, whether or not the image285* exists. When this applet attempts to draw the image on the screen,286* the data will be loaded. The graphics primitives that draw the287* image will incrementally paint on the screen.288*289* @param url an absolute URL giving the base location of the image.290* @param name the location of the image, relative to the291* <code>url</code> argument.292* @return the image at the specified URL.293* @see java.awt.Image294*/295public Image getImage(URL url, String name) {296try {297return getImage(new URL(url, name));298} catch (MalformedURLException e) {299return null;300}301}302303/**304* Get an audio clip from the given URL.305*306* @param url points to the audio clip307* @return the audio clip at the specified URL.308*309* @since 1.2310*/311public final static AudioClip newAudioClip(URL url) {312return new sun.applet.AppletAudioClip(url);313}314315/**316* Returns the <code>AudioClip</code> object specified by the317* <code>URL</code> argument.318* <p>319* This method always returns immediately, whether or not the audio320* clip exists. When this applet attempts to play the audio clip, the321* data will be loaded.322*323* @param url an absolute URL giving the location of the audio clip.324* @return the audio clip at the specified URL.325* @see java.applet.AudioClip326*/327public AudioClip getAudioClip(URL url) {328return getAppletContext().getAudioClip(url);329}330331/**332* Returns the <code>AudioClip</code> object specified by the333* <code>URL</code> and <code>name</code> arguments.334* <p>335* This method always returns immediately, whether or not the audio336* clip exists. When this applet attempts to play the audio clip, the337* data will be loaded.338*339* @param url an absolute URL giving the base location of the340* audio clip.341* @param name the location of the audio clip, relative to the342* <code>url</code> argument.343* @return the audio clip at the specified URL.344* @see java.applet.AudioClip345*/346public AudioClip getAudioClip(URL url, String name) {347try {348return getAudioClip(new URL(url, name));349} catch (MalformedURLException e) {350return null;351}352}353354/**355* Returns information about this applet. An applet should override356* this method to return a <code>String</code> containing information357* about the author, version, and copyright of the applet.358* <p>359* The implementation of this method provided by the360* <code>Applet</code> class returns <code>null</code>.361*362* @return a string containing information about the author, version, and363* copyright of the applet.364*/365public String getAppletInfo() {366return null;367}368369/**370* Gets the locale of the applet. It allows the applet371* to maintain its own locale separated from the locale372* of the browser or appletviewer.373*374* @return the locale of the applet; if no locale has375* been set, the default locale is returned.376* @since JDK1.1377*/378public Locale getLocale() {379Locale locale = super.getLocale();380if (locale == null) {381return Locale.getDefault();382}383return locale;384}385386/**387* Returns information about the parameters that are understood by388* this applet. An applet should override this method to return an389* array of <code>Strings</code> describing these parameters.390* <p>391* Each element of the array should be a set of three392* <code>Strings</code> containing the name, the type, and a393* description. For example:394* <blockquote><pre>395* String pinfo[][] = {396* {"fps", "1-10", "frames per second"},397* {"repeat", "boolean", "repeat image loop"},398* {"imgs", "url", "images directory"}399* };400* </pre></blockquote>401* <p>402* The implementation of this method provided by the403* <code>Applet</code> class returns <code>null</code>.404*405* @return an array describing the parameters this applet looks for.406*/407public String[][] getParameterInfo() {408return null;409}410411/**412* Plays the audio clip at the specified absolute URL. Nothing413* happens if the audio clip cannot be found.414*415* @param url an absolute URL giving the location of the audio clip.416*/417public void play(URL url) {418AudioClip clip = getAudioClip(url);419if (clip != null) {420clip.play();421}422}423424/**425* Plays the audio clip given the URL and a specifier that is426* relative to it. Nothing happens if the audio clip cannot be found.427*428* @param url an absolute URL giving the base location of the429* audio clip.430* @param name the location of the audio clip, relative to the431* <code>url</code> argument.432*/433public void play(URL url, String name) {434AudioClip clip = getAudioClip(url, name);435if (clip != null) {436clip.play();437}438}439440/**441* Called by the browser or applet viewer to inform442* this applet that it has been loaded into the system. It is always443* called before the first time that the <code>start</code> method is444* called.445* <p>446* A subclass of <code>Applet</code> should override this method if447* it has initialization to perform. For example, an applet with448* threads would use the <code>init</code> method to create the449* threads and the <code>destroy</code> method to kill them.450* <p>451* The implementation of this method provided by the452* <code>Applet</code> class does nothing.453*454* @see java.applet.Applet#destroy()455* @see java.applet.Applet#start()456* @see java.applet.Applet#stop()457*/458public void init() {459}460461/**462* Called by the browser or applet viewer to inform463* this applet that it should start its execution. It is called after464* the <code>init</code> method and each time the applet is revisited465* in a Web page.466* <p>467* A subclass of <code>Applet</code> should override this method if468* it has any operation that it wants to perform each time the Web469* page containing it is visited. For example, an applet with470* animation might want to use the <code>start</code> method to471* resume animation, and the <code>stop</code> method to suspend the472* animation.473* <p>474* Note: some methods, such as <code>getLocationOnScreen</code>, can only475* provide meaningful results if the applet is showing. Because476* <code>isShowing</code> returns <code>false</code> when the applet's477* <code>start</code> is first called, methods requiring478* <code>isShowing</code> to return <code>true</code> should be called from479* a <code>ComponentListener</code>.480* <p>481* The implementation of this method provided by the482* <code>Applet</code> class does nothing.483*484* @see java.applet.Applet#destroy()485* @see java.applet.Applet#init()486* @see java.applet.Applet#stop()487* @see java.awt.Component#isShowing()488* @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)489*/490public void start() {491}492493/**494* Called by the browser or applet viewer to inform495* this applet that it should stop its execution. It is called when496* the Web page that contains this applet has been replaced by497* another page, and also just before the applet is to be destroyed.498* <p>499* A subclass of <code>Applet</code> should override this method if500* it has any operation that it wants to perform each time the Web501* page containing it is no longer visible. For example, an applet502* with animation might want to use the <code>start</code> method to503* resume animation, and the <code>stop</code> method to suspend the504* animation.505* <p>506* The implementation of this method provided by the507* <code>Applet</code> class does nothing.508*509* @see java.applet.Applet#destroy()510* @see java.applet.Applet#init()511*/512public void stop() {513}514515/**516* Called by the browser or applet viewer to inform517* this applet that it is being reclaimed and that it should destroy518* any resources that it has allocated. The <code>stop</code> method519* will always be called before <code>destroy</code>.520* <p>521* A subclass of <code>Applet</code> should override this method if522* it has any operation that it wants to perform before it is523* destroyed. For example, an applet with threads would use the524* <code>init</code> method to create the threads and the525* <code>destroy</code> method to kill them.526* <p>527* The implementation of this method provided by the528* <code>Applet</code> class does nothing.529*530* @see java.applet.Applet#init()531* @see java.applet.Applet#start()532* @see java.applet.Applet#stop()533*/534public void destroy() {535}536537//538// Accessibility support539//540541AccessibleContext accessibleContext = null;542543/**544* Gets the AccessibleContext associated with this Applet.545* For applets, the AccessibleContext takes the form of an546* AccessibleApplet.547* A new AccessibleApplet instance is created if necessary.548*549* @return an AccessibleApplet that serves as the550* AccessibleContext of this Applet551* @since 1.3552*/553public AccessibleContext getAccessibleContext() {554if (accessibleContext == null) {555accessibleContext = new AccessibleApplet();556}557return accessibleContext;558}559560/**561* This class implements accessibility support for the562* <code>Applet</code> class. It provides an implementation of the563* Java Accessibility API appropriate to applet user-interface elements.564* @since 1.3565*/566protected class AccessibleApplet extends AccessibleAWTPanel {567568private static final long serialVersionUID = 8127374778187708896L;569570/**571* Get the role of this object.572*573* @return an instance of AccessibleRole describing the role of the574* object575*/576public AccessibleRole getAccessibleRole() {577return AccessibleRole.FRAME;578}579580/**581* Get the state of this object.582*583* @return an instance of AccessibleStateSet containing the current584* state set of the object585* @see AccessibleState586*/587public AccessibleStateSet getAccessibleStateSet() {588AccessibleStateSet states = super.getAccessibleStateSet();589states.add(AccessibleState.ACTIVE);590return states;591}592593}594}595596597