home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / applet / applet.jav next >
Encoding:
Text File  |  1995-10-30  |  6.0 KB  |  241 lines

  1. /*
  2.  * @(#)Applet.java    1.25 95/10/24 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.applet;
  20.  
  21. import java.awt.*;
  22. import java.awt.image.ColorModel;
  23. import java.net.URL;
  24. import java.net.MalformedURLException;
  25.  
  26. /**
  27.  * Base applet class. 
  28.  *
  29.  * @version     1.25, 10/24/95
  30.  *
  31.  * @author Chris Warth
  32.  * @author Arthur van Hoff
  33.  */
  34. public class Applet extends Panel {
  35.     private AppletStub stub;
  36.  
  37.     /**
  38.      * Set the applet stub. This is done by automatically by the system.
  39.      */
  40.     public final void setStub(AppletStub stub) {
  41.     this.stub = (AppletStub)stub;
  42.     }
  43.  
  44.     /**
  45.      * Returns true if the applet is active. An applet is marked active
  46.      * just before the start method is called.
  47.      * @see #start
  48.      */
  49.     public boolean isActive() {
  50.     return stub.isActive();
  51.     }
  52.     
  53.     /**
  54.      * Gets the document URL. This is the URL of the document in which
  55.      * the applet is embedded.
  56.      * @see #getCodeBase
  57.      */
  58.     public URL getDocumentBase() {
  59.     return stub.getDocumentBase();
  60.     }
  61.  
  62.     /**
  63.      * Gets the base URL. This is the URL of the applet itself. 
  64.      * @see #getDocumentBase
  65.      */
  66.     public URL getCodeBase() {
  67.     return stub.getCodeBase();
  68.     }
  69.  
  70.     /**
  71.      * Gets a parameter of the applet.
  72.      */
  73.      public String getParameter(String name) {
  74.      return stub.getParameter(name);
  75.      }
  76.  
  77.     /**
  78.      * Gets a handle to the applet context. The applet context
  79.      * lets an applet control the applet's environment which is
  80.      * usually the browser or the applet viewer.
  81.      */
  82.     public AppletContext getAppletContext() {
  83.     return stub.getAppletContext();
  84.     }
  85.  
  86.     /**
  87.      * Request for the applet to be resized.
  88.      */
  89.     public void resize(int width, int height) {
  90.     if (stub == null) {
  91.         super.resize(width, height);
  92.         return;
  93.     }
  94.  
  95.     Dimension d = size();
  96.     if ((d.width != width) || (d.height != height)) {
  97.         stub.appletResize(width, height);
  98.     }
  99.     }
  100.  
  101.     /**
  102.      * Show a status message in the Applet's context.
  103.      */
  104.     public void showStatus(String msg) {
  105.     getAppletContext().showStatus(msg);
  106.     }
  107.  
  108.     /**
  109.      * Gets an image given a URL. Note that this method
  110.      * always returns an image object immediatly, even if 
  111.      * the image does not exist. The actual image datais 
  112.      * loaded when it is first needed.
  113.      */
  114.     public Image getImage(URL url) {
  115.     return getAppletContext().getImage(url);
  116.     }
  117.  
  118.     /**
  119.      * Gets an image relative to a URL. This methods returns
  120.      * immediatly, even if the image does not exist. The actual
  121.      * image data is loaded when it is first needed.
  122.      * 
  123.      * @see #getImage
  124.      */
  125.     public Image getImage(URL url, String name) {
  126.     try {
  127.         return getImage(new URL(url, name));
  128.     } catch (MalformedURLException e) {
  129.         return null;
  130.     }
  131.     }
  132.  
  133.     /**
  134.      * Gets an audio clip. 
  135.      */
  136.     public AudioClip getAudioClip(URL url) {
  137.     return getAppletContext().getAudioClip(url);
  138.     }
  139.  
  140.     /**
  141.      * Gets an audio clip. 
  142.      * @see #getAudioClip
  143.      */
  144.     public AudioClip getAudioClip(URL url, String name) {
  145.     try {
  146.         return getAudioClip(new URL(url, name));
  147.     } catch (MalformedURLException e) {
  148.         return null;
  149.     }
  150.     }
  151.  
  152.     /**
  153.      * Return a string containing information about
  154.      * the author, version and copyright of the applet.
  155.      */
  156.     public String getAppletInfo() {
  157.     return null;
  158.     }
  159.  
  160.     /**
  161.      * Returns an array of strings describing the
  162.      * parameters that are understoond by this
  163.      * applet. The array consists of sets of 3 strings
  164.      * name/type/description. For example:
  165.      * <pre>
  166.      *     String pinfo[][] = {
  167.      *      {"fps",    "1-10",    "frames per second"},
  168.      *      {"repeat", "boolean", "repeat image loop"},
  169.      *      {"imgs",   "url",     "directory in which the images live"}
  170.      *    };
  171.      * </pre>
  172.      */
  173.     public String[][] getParameterInfo() {
  174.     return null;
  175.     }
  176.  
  177.     /**
  178.      * Play an audio clip. Nothing happens if the audio clip could
  179.      * not be found.
  180.      */
  181.     public void play(URL url) {
  182.     AudioClip clip = getAudioClip(url);
  183.     if (clip != null) {
  184.         clip.play();
  185.     }
  186.     }
  187.  
  188.     /**
  189.      * Play an audio clip. Nothing happens if the audio clip could
  190.      * not be found.
  191.      */
  192.     public void play(URL url, String name) {
  193.     AudioClip clip = getAudioClip(url, name);
  194.     if (clip != null) {
  195.         clip.play();
  196.     }
  197.     }
  198.  
  199.     /**
  200.      * Initializes the applet.
  201.      * You never need to call this directly, it is called automatically
  202.      * by the system once the applet is created.
  203.      * @see #start
  204.      * @see #stop
  205.      * @see #destroy
  206.      */
  207.     public void init() {
  208.     }
  209.  
  210.     /**
  211.      * Called to start the applet. You never need to call this method
  212.      * directly, it is called when the applet's document is visited.
  213.      * @see #init
  214.      * @see #stop
  215.      * @see #destroy
  216.      */
  217.     public void start() {
  218.     }
  219.  
  220.     /**
  221.      * Called to stop the applet. It is called when the applet's document is
  222.      * no longer on the screen. It is guaranteed to be called before destroy()
  223.      * is called. You never need to call this method directly.
  224.      * @see #init
  225.      * @see #start
  226.      * @see #destroy
  227.      */
  228.     public void stop() {
  229.     }
  230.  
  231.     /**
  232.      * Cleans up whatever resources are being held. If the applet is active
  233.      * it is first stopped.
  234.      * @see #init
  235.      * @see #start
  236.      * @see #stop
  237.      */
  238.     public void destroy() {
  239.     }
  240. }
  241.