< prev index next >

src/java.desktop/share/classes/java/applet/Applet.java

Print this page




  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.io.IOException;
  35 import java.io.ObjectInputStream;
  36 import java.net.MalformedURLException;
  37 import java.net.URL;
  38 import java.util.Locale;
  39 
  40 import javax.accessibility.AccessibleContext;
  41 import javax.accessibility.AccessibleRole;
  42 import javax.accessibility.AccessibleState;
  43 import javax.accessibility.AccessibleStateSet;
  44 
  45 import com.sun.media.sound.JavaSoundAudioClip;
  46 
  47 /**
  48  * An applet is a small program that is intended not to be run on
  49  * its own, but rather to be embedded inside another application.
  50  * <p>
  51  * The {@code Applet} class must be the superclass of any
  52  * applet that is to be embedded in a Web page or viewed by the Java
  53  * Applet Viewer. The {@code Applet} class provides a standard
  54  * interface between applets and their environment.
  55  *
  56  * @author      Arthur van Hoff
  57  * @author      Chris Warth
  58  * @since       1.0
  59  *
  60  * @deprecated The Applet API is deprecated, no replacement.
  61  */
  62 @Deprecated(since = "9")
  63 public class Applet extends Panel {
  64 
  65     /**
  66      * Constructs a new Applet.
  67      * <p>
  68      * Note: Many methods in {@code java.applet.Applet}
  69      * may be invoked by the applet only after the applet is
  70      * fully constructed; applet should avoid calling methods
  71      * in {@code java.applet.Applet} in the constructor.
  72      *
  73      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  74      * returns true.
  75      * @see java.awt.GraphicsEnvironment#isHeadless
  76      * @since 1.4
  77      */
  78     public Applet() throws HeadlessException {
  79         if (GraphicsEnvironment.isHeadless()) {
  80             throw new HeadlessException();
  81         }
  82     }
  83 
  84     /**
  85      * Applets can be serialized but the following conventions MUST be followed:
  86      *
  87      * Before Serialization:
  88      * An applet must be in STOPPED state.
  89      *
  90      * After Deserialization:
  91      * The applet will be restored in STOPPED state (and most clients will
  92      * likely move it into RUNNING state).
  93      * The stub field will be restored by the reader.
  94      */
  95     private transient AppletStub stub;
  96 
  97     /* version ID for serialized form. */


  98     private static final long serialVersionUID = -5836846270535785031L;
  99 
 100     /**
 101      * Read an applet from an object input stream.
 102      * @param  s  an object input stream.
 103      * @exception HeadlessException if
 104      * {@code GraphicsEnvironment.isHeadless()} returns
 105      * {@code true}
 106      * @serial
 107      * @see java.awt.GraphicsEnvironment#isHeadless
 108      * @since 1.4
 109      */
 110     private void readObject(ObjectInputStream s)
 111         throws ClassNotFoundException, IOException, HeadlessException {
 112         if (GraphicsEnvironment.isHeadless()) {
 113             throw new HeadlessException();
 114         }
 115         s.defaultReadObject();
 116     }
 117 
 118     /**
 119      * Sets this applet's stub. This is done automatically by the system.
 120      * <p>If there is a security manager, its {@code checkPermission}
 121      * method is called with the
 122      * {@code AWTPermission("setAppletStub")}
 123      * permission if a stub has already been set.
 124      * @param   stub   the new stub.
 125      * @exception SecurityException if the caller cannot set the stub

 126      */
 127     public final void setStub(AppletStub stub) {
 128         if (this.stub != null) {
 129             SecurityManager s = System.getSecurityManager();
 130             if (s != null) {
 131                 s.checkPermission(new AWTPermission("setAppletStub"));
 132             }
 133         }
 134         this.stub = stub;
 135     }
 136 
 137     /**
 138      * Determines if this applet is active. An applet is marked active
 139      * just before its {@code start} method is called. It becomes
 140      * inactive just before its {@code stop} method is called.
 141      *
 142      * @return  {@code true} if the applet is active;
 143      *          {@code false} otherwise.
 144      * @see     java.applet.Applet#start()
 145      * @see     java.applet.Applet#stop()
 146      */
 147     public boolean isActive() {
 148         if (stub != null) {
 149             return stub.isActive();
 150         } else {        // If stub field not filled in, applet never active
 151             return false;
 152         }
 153     }
 154 
 155     /**
 156      * Gets the URL of the document in which this applet is embedded.
 157      * For example, suppose an applet is contained
 158      * within the document:
 159      * <blockquote><pre>
 160      *    http://www.oracle.com/technetwork/java/index.html
 161      * </pre></blockquote>
 162      * The document base is:
 163      * <blockquote><pre>
 164      *    http://www.oracle.com/technetwork/java/index.html
 165      * </pre></blockquote>
 166      *
 167      * @return  the {@link java.net.URL} of the document that contains this
 168      *          applet.
 169      * @see     java.applet.Applet#getCodeBase()
 170      */
 171     public URL getDocumentBase() {
 172         return stub.getDocumentBase();
 173     }
 174 
 175     /**
 176      * Gets the base URL. This is the URL of the directory which contains this applet.

 177      *
 178      * @return  the base {@link java.net.URL} of
 179      *          the directory which contains this applet.
 180      * @see     java.applet.Applet#getDocumentBase()
 181      */
 182     public URL getCodeBase() {
 183         return stub.getCodeBase();
 184     }
 185 
 186     /**
 187      * Returns the value of the named parameter in the HTML tag. For
 188      * example, if this applet is specified as
 189      * <blockquote><pre>
 190      * &lt;applet code="Clock" width=50 height=50&gt;
 191      * &lt;param name=Color value="blue"&gt;
 192      * &lt;/applet&gt;
 193      * </pre></blockquote>
 194      * <p>
 195      * then a call to {@code getParameter("Color")} returns the
 196      * value {@code "blue"}.
 197      * <p>
 198      * The {@code name} argument is case insensitive.
 199      *
 200      * @param   name   a parameter name.
 201      * @return  the value of the named parameter,
 202      *          or {@code null} if not set.
 203      */
 204      public String getParameter(String name) {
 205          return stub.getParameter(name);
 206      }
 207 
 208     /**
 209      * Determines this applet's context, which allows the applet to
 210      * query and affect the environment in which it runs.
 211      * <p>
 212      * This environment of an applet represents the document that
 213      * contains the applet.
 214      *
 215      * @return  the applet's context.
 216      */
 217     public AppletContext getAppletContext() {
 218         return stub.getAppletContext();
 219     }
 220 
 221     /**
 222      * Requests that this applet be resized.
 223      *
 224      * @param   width    the new requested width for the applet.
 225      * @param   height   the new requested height for the applet.
 226      */
 227     @SuppressWarnings("deprecation")
 228     public void resize(int width, int height) {
 229         Dimension d = size();
 230         if ((d.width != width) || (d.height != height)) {
 231             super.resize(width, height);
 232             if (stub != null) {
 233                 stub.appletResize(width, height);
 234             }
 235         }
 236     }
 237 
 238     /**
 239      * Requests that this applet be resized.
 240      *
 241      * @param   d   an object giving the new width and height.
 242      */
 243     @SuppressWarnings("deprecation")
 244     public void resize(Dimension d) {
 245         resize(d.width, d.height);
 246     }
 247 
 248     /**
 249      * Indicates if this container is a validate root.
 250      * <p>
 251      * {@code Applet} objects are the validate roots, and, therefore, they
 252      * override this method to return {@code true}.
 253      *
 254      * @return {@code true}
 255      * @since 1.7
 256      * @see java.awt.Container#isValidateRoot

 257      */
 258     @Override
 259     public boolean isValidateRoot() {
 260         return true;
 261     }
 262 
 263     /**
 264      * Requests that the argument string be displayed in the
 265      * "status window". Many browsers and applet viewers
 266      * provide such a window, where the application can inform users of
 267      * its current state.
 268      *
 269      * @param   msg   a string to display in the status window.
 270      */
 271     public void showStatus(String msg) {
 272         getAppletContext().showStatus(msg);
 273     }
 274 
 275     /**
 276      * Returns an {@code Image} object that can then be painted on
 277      * the screen. The {@code url} that is passed as an argument
 278      * must specify an absolute URL.
 279      * <p>
 280      * This method always returns immediately, whether or not the image
 281      * exists. When this applet attempts to draw the image on the screen,
 282      * the data will be loaded. The graphics primitives that draw the
 283      * image will incrementally paint on the screen.
 284      *
 285      * @param   url   an absolute URL giving the location of the image.
 286      * @return  the image at the specified URL.
 287      * @see     java.awt.Image
 288      */
 289     public Image getImage(URL url) {
 290         return getAppletContext().getImage(url);
 291     }
 292 
 293     /**
 294      * Returns an {@code Image} object that can then be painted on
 295      * the screen. The {@code url} argument must specify an absolute
 296      * URL. The {@code name} argument is a specifier that is
 297      * relative to the {@code url} argument.
 298      * <p>
 299      * This method always returns immediately, whether or not the image
 300      * exists. When this applet attempts to draw the image on the screen,
 301      * the data will be loaded. The graphics primitives that draw the
 302      * image will incrementally paint on the screen.
 303      *
 304      * @param   url    an absolute URL giving the base location of the image.
 305      * @param   name   the location of the image, relative to the
 306      *                 {@code url} argument.
 307      * @return  the image at the specified URL.
 308      * @see     java.awt.Image
 309      */
 310     public Image getImage(URL url, String name) {
 311         try {
 312             return getImage(new URL(url, name));
 313         } catch (MalformedURLException e) {
 314             return null;
 315         }
 316     }
 317 
 318     /**
 319      * Get an audio clip from the given URL.
 320      *
 321      * @param url points to the audio clip
 322      * @return the audio clip at the specified URL.
 323      *
 324      * @since       1.2
 325      */
 326     public static final AudioClip newAudioClip(URL url) {
 327         return JavaSoundAudioClip.create(url);
 328     }
 329 
 330     /**
 331      * Returns the {@code AudioClip} object specified by the
 332      * {@code URL} argument.
 333      * <p>
 334      * This method always returns immediately, whether or not the audio
 335      * clip exists. When this applet attempts to play the audio clip, the
 336      * data will be loaded.
 337      *
 338      * @param   url  an absolute URL giving the location of the audio clip.
 339      * @return  the audio clip at the specified URL.
 340      * @see     java.applet.AudioClip
 341      */
 342     public AudioClip getAudioClip(URL url) {
 343         return getAppletContext().getAudioClip(url);
 344     }
 345 
 346     /**
 347      * Returns the {@code AudioClip} object specified by the
 348      * {@code URL} and {@code name} arguments.
 349      * <p>
 350      * This method always returns immediately, whether or not the audio
 351      * clip exists. When this applet attempts to play the audio clip, the
 352      * data will be loaded.
 353      *
 354      * @param   url    an absolute URL giving the base location of the
 355      *                 audio clip.
 356      * @param   name   the location of the audio clip, relative to the
 357      *                 {@code url} argument.
 358      * @return  the audio clip at the specified URL.
 359      * @see     java.applet.AudioClip
 360      */
 361     public AudioClip getAudioClip(URL url, String name) {
 362         try {
 363             return getAudioClip(new URL(url, name));
 364         } catch (MalformedURLException e) {
 365             return null;
 366         }
 367     }
 368 
 369     /**
 370      * Returns information about this applet. An applet should override
 371      * this method to return a {@code String} containing information
 372      * about the author, version, and copyright of the applet.
 373      * <p>
 374      * The implementation of this method provided by the
 375      * {@code Applet} class returns {@code null}.
 376      *
 377      * @return  a string containing information about the author, version, and
 378      *          copyright of the applet.
 379      */
 380     public String getAppletInfo() {
 381         return null;
 382     }
 383 
 384     /**
 385      * Gets the locale of the applet. It allows the applet
 386      * to maintain its own locale separated from the locale
 387      * of the browser or appletviewer.
 388      *
 389      * @return  the locale of the applet; if no locale has
 390      *          been set, the default locale is returned.
 391      * @since   1.1
 392      */
 393     public Locale getLocale() {
 394       Locale locale = super.getLocale();
 395       if (locale == null) {
 396         return Locale.getDefault();
 397       }
 398       return locale;
 399     }
 400 
 401     /**
 402      * Returns information about the parameters that are understood by
 403      * this applet. An applet should override this method to return an
 404      * array of {@code Strings} describing these parameters.
 405      * <p>
 406      * Each element of the array should be a set of three
 407      * {@code Strings} containing the name, the type, and a
 408      * 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
 418      * {@code Applet} class returns {@code null}.
 419      *
 420      * @return  an array describing the parameters this applet looks for.
 421      */
 422     public String[][] getParameterInfo() {
 423         return null;
 424     }
 425 
 426     /**
 427      * Plays the audio clip at the specified absolute URL. Nothing
 428      * happens if the audio clip cannot be found.
 429      *
 430      * @param   url   an absolute URL giving the location of the audio clip.
 431      */
 432     public void play(URL url) {
 433         AudioClip clip = getAudioClip(url);
 434         if (clip != null) {
 435             clip.play();
 436         }
 437     }
 438 
 439     /**
 440      * Plays the audio clip given the URL and a specifier that is
 441      * relative to it. Nothing happens if the audio clip cannot be found.
 442      *
 443      * @param   url    an absolute URL giving the base location of the
 444      *                 audio clip.
 445      * @param   name   the location of the audio clip, relative to the
 446      *                 {@code url} argument.
 447      */
 448     public void play(URL url, String name) {
 449         AudioClip clip = getAudioClip(url, name);
 450         if (clip != null) {
 451             clip.play();
 452         }
 453     }
 454 
 455     /**
 456      * Called by the browser or applet viewer to inform
 457      * this applet that it has been loaded into the system. It is always
 458      * called before the first time that the {@code start} method is
 459      * called.
 460      * <p>
 461      * A subclass of {@code Applet} should override this method if
 462      * it has initialization to perform. For example, an applet with
 463      * threads would use the {@code init} method to create the
 464      * threads and the {@code destroy} method to kill them.
 465      * <p>
 466      * The implementation of this method provided by the
 467      * {@code Applet} class 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
 478      * this applet that it should start its execution. It is called after
 479      * the {@code init} method and each time the applet is revisited
 480      * in a Web page.
 481      * <p>
 482      * A subclass of {@code Applet} should override this method if
 483      * it has any operation that it wants to perform each time the Web
 484      * page containing it is visited. For example, an applet with
 485      * animation might want to use the {@code start} method to
 486      * resume animation, and the {@code stop} method to suspend the
 487      * animation.
 488      * <p>
 489      * Note: some methods, such as {@code getLocationOnScreen}, can only
 490      * provide meaningful results if the applet is showing.  Because
 491      * {@code isShowing} returns {@code false} when the applet's
 492      * {@code start} is first called, methods requiring
 493      * {@code isShowing} to return {@code true} should be called from
 494      * a {@code ComponentListener}.
 495      * <p>
 496      * The implementation of this method provided by the
 497      * {@code Applet} class does nothing.
 498      *
 499      * @see     java.applet.Applet#destroy()
 500      * @see     java.applet.Applet#init()
 501      * @see     java.applet.Applet#stop()
 502      * @see     java.awt.Component#isShowing()
 503      * @see     java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
 504      */
 505     public void start() {
 506     }
 507 
 508     /**
 509      * Called by the browser or applet viewer to inform
 510      * this applet that it should stop its execution. It is called when
 511      * the Web page that contains this applet has been replaced by
 512      * another page, and also just before the applet is to be destroyed.
 513      * <p>
 514      * A subclass of {@code Applet} should override this method if
 515      * it has any operation that it wants to perform each time the Web
 516      * page containing it is no longer visible. For example, an applet
 517      * with animation might want to use the {@code start} method to
 518      * resume animation, and the {@code stop} method to suspend the
 519      * animation.
 520      * <p>
 521      * The implementation of this method provided by the
 522      * {@code Applet} class does nothing.
 523      *
 524      * @see     java.applet.Applet#destroy()
 525      * @see     java.applet.Applet#init()
 526      */
 527     public void stop() {
 528     }
 529 
 530     /**
 531      * Called by the browser or applet viewer to inform
 532      * this applet that it is being reclaimed and that it should destroy
 533      * any resources that it has allocated. The {@code stop} method
 534      * will always be called before {@code destroy}.
 535      * <p>
 536      * A subclass of {@code Applet} should override this method if
 537      * it has any operation that it wants to perform before it is
 538      * destroyed. For example, an applet with threads would use the
 539      * {@code init} method to create the threads and the
 540      * {@code destroy} method to kill them.
 541      * <p>
 542      * The implementation of this method provided by the
 543      * {@code Applet} class does nothing.
 544      *
 545      * @see     java.applet.Applet#init()
 546      * @see     java.applet.Applet#start()
 547      * @see     java.applet.Applet#stop()
 548      */
 549     public void destroy() {
 550     }
 551 
 552     //
 553     // Accessibility support
 554     //
 555 



 556     AccessibleContext accessibleContext = null;
 557 
 558     /**
 559      * Gets the AccessibleContext associated with this Applet.
 560      * For applets, the AccessibleContext takes the form of an
 561      * AccessibleApplet.
 562      * A new AccessibleApplet instance is created if necessary.
 563      *
 564      * @return an AccessibleApplet that serves as the
 565      *         AccessibleContext of this Applet
 566      * @since 1.3
 567      */
 568     public AccessibleContext getAccessibleContext() {
 569         if (accessibleContext == null) {
 570             accessibleContext = new AccessibleApplet();
 571         }
 572         return accessibleContext;
 573     }
 574 
 575     /**
 576      * This class implements accessibility support for the
 577      * {@code Applet} class.  It provides an implementation of the
 578      * Java Accessibility API appropriate to applet user-interface elements.

 579      * @since 1.3
 580      */
 581     protected class AccessibleApplet extends AccessibleAWTPanel {
 582 



 583         private static final long serialVersionUID = 8127374778187708896L;
 584 
 585         /**
 586          * Get the role of this object.
 587          *
 588          * @return an instance of AccessibleRole describing the role of the
 589          * object
 590          */
 591         public AccessibleRole getAccessibleRole() {
 592             return AccessibleRole.FRAME;
 593         }
 594 
 595         /**
 596          * Get the state of this object.
 597          *
 598          * @return an instance of AccessibleStateSet containing the current
 599          * state set of the object
 600          * @see AccessibleState
 601          */
 602         public AccessibleStateSet getAccessibleStateSet() {
 603             AccessibleStateSet states = super.getAccessibleStateSet();
 604             states.add(AccessibleState.ACTIVE);
 605             return states;
 606         }
 607 
 608     }
 609 }


  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.net.MalformedURLException;
  38 import java.net.URL;
  39 import java.util.Locale;
  40 
  41 import javax.accessibility.AccessibleContext;
  42 import javax.accessibility.AccessibleRole;
  43 import javax.accessibility.AccessibleState;
  44 import javax.accessibility.AccessibleStateSet;
  45 
  46 import com.sun.media.sound.JavaSoundAudioClip;
  47 
  48 /**
  49  * An applet is a small program that is intended not to be run on its own, but
  50  * rather to be embedded inside another application.
  51  * <p>
  52  * The {@code Applet} class must be the superclass of any applet that is to be
  53  * embedded in a Web page or viewed by the Java Applet Viewer. The
  54  * {@code Applet} class provides a standard interface between applets and their
  55  * environment.
  56  *
  57  * @author Arthur van Hoff
  58  * @author Chris Warth
  59  * @since 1.0

  60  * @deprecated The Applet API is deprecated, no replacement.
  61  */
  62 @Deprecated(since = "9")
  63 public class Applet extends Panel {
  64 
  65     /**
  66      * Constructs a new Applet.
  67      * <p>
  68      * Note: Many methods in {@code java.applet.Applet} may be invoked by the
  69      * applet only after the applet is fully constructed; applet should avoid
  70      * calling methods in {@code java.applet.Applet} in the constructor.

  71      *
  72      * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
  73      *         returns {@code true}
  74      * @see java.awt.GraphicsEnvironment#isHeadless
  75      * @since 1.4
  76      */
  77     public Applet() throws HeadlessException {
  78         if (GraphicsEnvironment.isHeadless()) {
  79             throw new HeadlessException();
  80         }
  81     }
  82 
  83     /**
  84      * Applets can be serialized but the following conventions MUST be followed:
  85      * <p>
  86      * Before Serialization: An applet must be in STOPPED state.
  87      * <p>
  88      * After Deserialization: The applet will be restored in STOPPED state (and
  89      * most clients will likely move it into RUNNING state). The stub field will
  90      * be restored by the reader.


  91      */
  92     private transient AppletStub stub;
  93 
  94     /**
  95      * Use serialVersionUID from JDK 1.0 for interoperability.
  96      */
  97     private static final long serialVersionUID = -5836846270535785031L;
  98 
  99     /**
 100      * Read an applet from an object input stream.
 101      *
 102      * @param  s an object input stream
 103      * @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}
 104      *         returns {@code true}
 105      * @serial
 106      * @see java.awt.GraphicsEnvironment#isHeadless
 107      * @since 1.4
 108      */
 109     private void readObject(ObjectInputStream s)
 110         throws ClassNotFoundException, IOException, HeadlessException {
 111         if (GraphicsEnvironment.isHeadless()) {
 112             throw new HeadlessException();
 113         }
 114         s.defaultReadObject();
 115     }
 116 
 117     /**
 118      * Sets this applet's stub. This is done automatically by the system.
 119      * <p>
 120      * If there is a security manager, its {@code checkPermission} method is
 121      * called with the {@code AWTPermission("setAppletStub")} permission if a
 122      * stub has already been set.
 123      *
 124      * @param  stub the new stub
 125      * @throws SecurityException if the caller cannot set the stub
 126      */
 127     public final void setStub(AppletStub stub) {
 128         if (this.stub != null) {
 129             SecurityManager s = System.getSecurityManager();
 130             if (s != null) {
 131                 s.checkPermission(new AWTPermission("setAppletStub"));
 132             }
 133         }
 134         this.stub = stub;
 135     }
 136 
 137     /**
 138      * Determines if this applet is active. An applet is marked active just
 139      * before its {@code start} method is called. It becomes inactive just
 140      * before its {@code stop} method is called.
 141      *
 142      * @return {@code true} if the applet is active; {@code false} otherwise

 143      * @see java.applet.Applet#start()
 144      * @see java.applet.Applet#stop()
 145      */
 146     public boolean isActive() {
 147         if (stub != null) {
 148             return stub.isActive();
 149         } else {        // If stub field not filled in, applet never active
 150             return false;
 151         }
 152     }
 153 
 154     /**
 155      * Gets the {@code URL} of the document in which this applet is embedded.
 156      * For example, suppose an applet is contained within the document:

 157      * <blockquote><pre>
 158      *    http://www.oracle.com/technetwork/java/index.html
 159      * </pre></blockquote>
 160      * The document base is:
 161      * <blockquote><pre>
 162      *    http://www.oracle.com/technetwork/java/index.html
 163      * </pre></blockquote>
 164      *
 165      * @return the {@link java.net.URL} of the document that contains this
 166      *         applet
 167      * @see java.applet.Applet#getCodeBase()
 168      */
 169     public URL getDocumentBase() {
 170         return stub.getDocumentBase();
 171     }
 172 
 173     /**
 174      * Gets the base {@code URL}. This is the {@code URL} of the directory which
 175      * contains this applet.
 176      *
 177      * @return the base {@link java.net.URL} of the directory which contains
 178      *         this applet
 179      * @see java.applet.Applet#getDocumentBase()
 180      */
 181     public URL getCodeBase() {
 182         return stub.getCodeBase();
 183     }
 184 
 185     /**
 186      * Returns the value of the named parameter in the HTML tag. For example, if
 187      * this applet is specified as
 188      * <blockquote><pre>
 189      * &lt;applet code="Clock" width=50 height=50&gt;
 190      * &lt;param name=Color value="blue"&gt;
 191      * &lt;/applet&gt;
 192      * </pre></blockquote>
 193      * <p>
 194      * then a call to {@code getParameter("Color")} returns the value
 195      * {@code "blue"}.
 196      * <p>
 197      * The {@code name} argument is case insensitive.
 198      *
 199      * @param  name a parameter name
 200      * @return the value of the named parameter, or {@code null} if not set

 201      */
 202     public String getParameter(String name) {
 203         return stub.getParameter(name);
 204     }
 205 
 206     /**
 207      * Determines this applet's context, which allows the applet to query and
 208      * affect the environment in which it runs.
 209      * <p>
 210      * This environment of an applet represents the document that contains the
 211      * applet.
 212      *
 213      * @return the applet's context
 214      */
 215     public AppletContext getAppletContext() {
 216         return stub.getAppletContext();
 217     }
 218 
 219     /**
 220      * Requests that this applet be resized.
 221      *
 222      * @param  width the new requested width for the applet
 223      * @param  height the new requested height for the applet
 224      */
 225     @SuppressWarnings("deprecation")
 226     public void resize(int width, int height) {
 227         Dimension d = size();
 228         if ((d.width != width) || (d.height != height)) {
 229             super.resize(width, height);
 230             if (stub != null) {
 231                 stub.appletResize(width, height);
 232             }
 233         }
 234     }
 235 
 236     /**
 237      * Requests that this applet be resized.
 238      *
 239      * @param  d an object giving the new width and height
 240      */
 241     @SuppressWarnings("deprecation")
 242     public void resize(Dimension d) {
 243         resize(d.width, d.height);
 244     }
 245 
 246     /**
 247      * Indicates if this container is a validate root.
 248      * <p>
 249      * {@code Applet} objects are the validate roots, and, therefore, they
 250      * override this method to return {@code true}.
 251      *
 252      * @return {@code true}

 253      * @see java.awt.Container#isValidateRoot
 254      * @since 1.7
 255      */
 256     @Override
 257     public boolean isValidateRoot() {
 258         return true;
 259     }
 260 
 261     /**
 262      * Requests that the argument string be displayed in the "status window".
 263      * Many browsers and applet viewers provide such a window, where the
 264      * application can inform users of its current state.

 265      *
 266      * @param  msg a string to display in the status window
 267      */
 268     public void showStatus(String msg) {
 269         getAppletContext().showStatus(msg);
 270     }
 271 
 272     /**
 273      * Returns an {@code Image} object that can then be painted on the screen.
 274      * The {@code url} that is passed as an argument must specify an absolute
 275      * {@code URL}.
 276      * <p>
 277      * This method always returns immediately, whether or not the image exists.
 278      * When this applet attempts to draw the image on the screen, the data will
 279      * be loaded. The graphics primitives that draw the image will incrementally
 280      * paint on the screen.
 281      *
 282      * @param  url an absolute {@code URL} giving the location of the image
 283      * @return the image at the specified {@code URL}
 284      * @see java.awt.Image
 285      */
 286     public Image getImage(URL url) {
 287         return getAppletContext().getImage(url);
 288     }
 289 
 290     /**
 291      * Returns an {@code Image} object that can then be painted on the screen.
 292      * The {@code url} argument must specify an absolute {@code URL}. The
 293      * {@code name} argument is a specifier that is relative to the {@code url}
 294      * argument.
 295      * <p>
 296      * This method always returns immediately, whether or not the image exists.
 297      * When this applet attempts to draw the image on the screen, the data will
 298      * be loaded. The graphics primitives that draw the image will incrementally
 299      * paint on the screen.
 300      *
 301      * @param  url an absolute URL giving the base location of the image
 302      * @param  name the location of the image, relative to the {@code url}
 303      *         argument
 304      * @return the image at the specified {@code URL}
 305      * @see java.awt.Image
 306      */
 307     public Image getImage(URL url, String name) {
 308         try {
 309             return getImage(new URL(url, name));
 310         } catch (MalformedURLException e) {
 311             return null;
 312         }
 313     }
 314 
 315     /**
 316      * Get an audio clip from the given {@code URL}.
 317      *
 318      * @param  url points to the audio clip
 319      * @return the audio clip at the specified {@code URL}

 320      * @since 1.2
 321      */
 322     public static final AudioClip newAudioClip(URL url) {
 323         return JavaSoundAudioClip.create(url);
 324     }
 325 
 326     /**
 327      * Returns the {@code AudioClip} object specified by the {@code URL}
 328      * argument.
 329      * <p>
 330      * This method always returns immediately, whether or not the audio clip
 331      * exists. When this applet attempts to play the audio clip, the data will
 332      * be loaded.
 333      *
 334      * @param  url an absolute {@code URL} giving the location of the audio clip
 335      * @return the audio clip at the specified {@code URL}
 336      * @see java.applet.AudioClip
 337      */
 338     public AudioClip getAudioClip(URL url) {
 339         return getAppletContext().getAudioClip(url);
 340     }
 341 
 342     /**
 343      * Returns the {@code AudioClip} object specified by the {@code URL} and
 344      * {@code name} arguments.
 345      * <p>
 346      * This method always returns immediately, whether or not the audio clip
 347      * exists. When this applet attempts to play the audio clip, the data will
 348      * be loaded.
 349      *
 350      * @param  url an absolute {@code URL} giving the base location of the audio
 351      *         clip
 352      * @param  name the location of the audio clip, relative to the {@code url}
 353      *         argument
 354      * @return the audio clip at the specified {@code URL}
 355      * @see java.applet.AudioClip
 356      */
 357     public AudioClip getAudioClip(URL url, String name) {
 358         try {
 359             return getAudioClip(new URL(url, name));
 360         } catch (MalformedURLException e) {
 361             return null;
 362         }
 363     }
 364 
 365     /**
 366      * Returns information about this applet. An applet should override this
 367      * method to return a {@code String} containing information about the
 368      * author, version, and copyright of the applet.
 369      * <p>
 370      * The implementation of this method provided by the {@code Applet} class
 371      * returns {@code null}.
 372      *
 373      * @return a string containing information about the author, version, and
 374      *         copyright of the applet
 375      */
 376     public String getAppletInfo() {
 377         return null;
 378     }
 379 
 380     /**
 381      * Gets the locale of the applet. It allows the applet to maintain its own
 382      * locale separated from the locale of the browser or appletviewer.

 383      *
 384      * @return the locale of the applet; if no locale has been set, the default
 385      *         locale is returned
 386      * @since 1.1
 387      */
 388     public Locale getLocale() {
 389         Locale locale = super.getLocale();
 390         if (locale == null) {
 391             return Locale.getDefault();
 392         }
 393         return locale;
 394     }
 395 
 396     /**
 397      * Returns information about the parameters that are understood by this
 398      * applet. An applet should override this method to return an array of
 399      * {@code Strings} describing these parameters.
 400      * <p>
 401      * Each element of the array should be a set of three {@code Strings}
 402      * containing the name, the type, and a description. For example:

 403      * <blockquote><pre>
 404      * String pinfo[][] = {
 405      *   {"fps",    "1-10",    "frames per second"},
 406      *   {"repeat", "boolean", "repeat image loop"},
 407      *   {"imgs",   "url",     "images directory"}
 408      * };
 409      * </pre></blockquote>
 410      * <p>
 411      * The implementation of this method provided by the {@code Applet} class
 412      * returns {@code null}.
 413      *
 414      * @return an array describing the parameters this applet looks for
 415      */
 416     public String[][] getParameterInfo() {
 417         return null;
 418     }
 419 
 420     /**
 421      * Plays the audio clip at the specified absolute {@code URL}. Nothing
 422      * happens if the audio clip cannot be found.
 423      *
 424      * @param  url an absolute {@code URL} giving the location of the audio clip
 425      */
 426     public void play(URL url) {
 427         AudioClip clip = getAudioClip(url);
 428         if (clip != null) {
 429             clip.play();
 430         }
 431     }
 432 
 433     /**
 434      * Plays the audio clip given the URL and a specifier that is relative to
 435      * it. Nothing happens if the audio clip cannot be found.
 436      *
 437      * @param  url an absolute {@code URL} giving the base location of the audio
 438      *         clip
 439      * @param  name the location of the audio clip, relative to the {@code url}
 440      *         argument
 441      */
 442     public void play(URL url, String name) {
 443         AudioClip clip = getAudioClip(url, name);
 444         if (clip != null) {
 445             clip.play();
 446         }
 447     }
 448 
 449     /**
 450      * Called by the browser or applet viewer to inform this applet that it has
 451      * been loaded into the system. It is always called before the first time
 452      * that the {@code start} method is called.
 453      * <p>
 454      * A subclass of {@code Applet} should override this method if it has
 455      * initialization to perform. For example, an applet with threads would use
 456      * the {@code init} method to create the threads and the {@code destroy}
 457      * method to kill them.

 458      * <p>
 459      * The implementation of this method provided by the {@code Applet} class
 460      * does nothing.
 461      *
 462      * @see java.applet.Applet#destroy()
 463      * @see java.applet.Applet#start()
 464      * @see java.applet.Applet#stop()
 465      */
 466     public void init() {
 467     }
 468 
 469     /**
 470      * Called by the browser or applet viewer to inform this applet that it
 471      * should start its execution. It is called after the {@code init} method
 472      * and each time the applet is revisited in a Web page.
 473      * <p>
 474      * A subclass of {@code Applet} should override this method if it has any
 475      * operation that it wants to perform each time the Web page containing it
 476      * is visited. For example, an applet with animation might want to use the
 477      * {@code start} method to resume animation, and the {@code stop} method to
 478      * suspend the animation.
 479      * <p>
 480      * Note: some methods, such as {@code getLocationOnScreen}, can only provide
 481      * meaningful results if the applet is showing. Because {@code isShowing}
 482      * returns {@code false} when the applet's {@code start} is first called,
 483      * methods requiring {@code isShowing} to return {@code true} should be
 484      * called from a {@code ComponentListener}.



 485      * <p>
 486      * The implementation of this method provided by the {@code Applet} class
 487      * does nothing.
 488      *
 489      * @see java.applet.Applet#destroy()
 490      * @see java.applet.Applet#init()
 491      * @see java.applet.Applet#stop()
 492      * @see java.awt.Component#isShowing()
 493      * @see java.awt.event.ComponentListener#componentShown(ComponentEvent)
 494      */
 495     public void start() {
 496     }
 497 
 498     /**
 499      * Called by the browser or applet viewer to inform this applet that it
 500      * should stop its execution. It is called when the Web page that contains
 501      * this applet has been replaced by another page, and also just before the
 502      * applet is to be destroyed.
 503      * <p>
 504      * A subclass of {@code Applet} should override this method if it has any
 505      * operation that it wants to perform each time the Web page containing it
 506      * is no longer visible. For example, an applet with animation might want to
 507      * use the {@code start} method to resume animation, and the {@code stop}
 508      * method to suspend the animation.

 509      * <p>
 510      * The implementation of this method provided by the {@code Applet} class
 511      * does nothing.
 512      *
 513      * @see java.applet.Applet#destroy()
 514      * @see java.applet.Applet#init()
 515      */
 516     public void stop() {
 517     }
 518 
 519     /**
 520      * Called by the browser or applet viewer to inform this applet that it is
 521      * being reclaimed and that it should destroy any resources that it has
 522      * allocated. The {@code stop} method will always be called before
 523      * {@code destroy}.
 524      * <p>
 525      * A subclass of {@code Applet} should override this method if it has any
 526      * operation that it wants to perform before it is destroyed. For example,
 527      * an applet with threads would use the {@code init} method to create the
 528      * threads and the {@code destroy} method to kill them.

 529      * <p>
 530      * The implementation of this method provided by the {@code Applet} class
 531      * does nothing.
 532      *
 533      * @see java.applet.Applet#init()
 534      * @see java.applet.Applet#start()
 535      * @see java.applet.Applet#stop()
 536      */
 537     public void destroy() {
 538     }
 539 
 540     //
 541     // Accessibility support
 542     //
 543 
 544     /**
 545      * The accessible context associated with this {@code Applet}.
 546      */
 547     AccessibleContext accessibleContext = null;
 548 
 549     /**
 550      * Gets the {@code AccessibleContext} associated with this {@code Applet}.
 551      * For applets, the {@code AccessibleContext} takes the form of an
 552      * {@code AccessibleApplet}. A new {@code AccessibleApplet} instance is
 553      * created if necessary.
 554      *
 555      * @return an {@code AccessibleApplet} that serves as the
 556      *         {@code AccessibleContext} of this {@code Applet}
 557      * @since 1.3
 558      */
 559     public AccessibleContext getAccessibleContext() {
 560         if (accessibleContext == null) {
 561             accessibleContext = new AccessibleApplet();
 562         }
 563         return accessibleContext;
 564     }
 565 
 566     /**
 567      * This class implements accessibility support for the {@code Applet} class.
 568      * It provides an implementation of the Java Accessibility API appropriate
 569      * to applet user-interface elements.
 570      *
 571      * @since 1.3
 572      */
 573     protected class AccessibleApplet extends AccessibleAWTPanel {
 574 
 575         /**
 576          * Use serialVersionUID from JDK 1.3 for interoperability.
 577          */
 578         private static final long serialVersionUID = 8127374778187708896L;
 579 
 580         /**
 581          * Get the role of this object.
 582          *
 583          * @return an instance of {@code AccessibleRole} describing the role of
 584          *         the object
 585          */
 586         public AccessibleRole getAccessibleRole() {
 587             return AccessibleRole.FRAME;
 588         }
 589 
 590         /**
 591          * Get the state of this object.
 592          *
 593          * @return an instance of {@code AccessibleStateSet} containing the
 594          *         current state set of the object
 595          * @see AccessibleState
 596          */
 597         public AccessibleStateSet getAccessibleStateSet() {
 598             AccessibleStateSet states = super.getAccessibleStateSet();
 599             states.add(AccessibleState.ACTIVE);
 600             return states;
 601         }

 602     }
 603 }
< prev index next >