1 /* Applet.java -- Java base applet class
2 Copyright (C) 1999, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.Component
;
42 import java
.awt
.Dimension
;
43 import java
.awt
.GraphicsEnvironment
;
44 import java
.awt
.HeadlessException
;
45 import java
.awt
.Image
;
46 import java
.awt
.Panel
;
47 import java
.io
.IOException
;
48 import java
.io
.ObjectInputStream
;
49 import java
.net
.MalformedURLException
;
51 import java
.util
.Locale
;
53 import javax
.accessibility
.AccessibleContext
;
54 import javax
.accessibility
.AccessibleRole
;
55 import javax
.accessibility
.AccessibleState
;
56 import javax
.accessibility
.AccessibleStateSet
;
57 import javax
.sound
.sampled
.AudioSystem
;
58 import javax
.sound
.sampled
.Clip
;
59 import javax
.sound
.sampled
.LineUnavailableException
;
60 import javax
.sound
.sampled
.UnsupportedAudioFileException
;
63 * This is the base applet class. An applet is a Java program that
64 * runs inside a web browser or other applet viewer in a restricted
67 * <p>To be useful, a subclass should override at least start(). Also useful
68 * are init, stop, and destroy for control purposes, and getAppletInfo and
69 * getParameterInfo for descriptive purposes.
71 * @author Aaron M. Renn (arenn@urbanophile.com)
72 * @author Eric Blake (ebb9@email.byu.edu)
74 * @status updated to 1.4
76 public class Applet
extends Panel
79 * Compatible with JDK 1.0+.
81 private static final long serialVersionUID
= -5836846270535785031L;
83 /** The applet stub for this applet. */
84 private transient AppletStub stub
;
86 /** Some applets call setSize in their constructors. In that case,
87 these fields are used to store width and height values until a
89 private transient int width
;
90 private transient int height
;
93 * The accessibility context for this applet.
95 * @serial the accessibleContext for this
98 private AccessibleContext accessibleContext
;
101 * Default constructor for subclasses.
103 * @throws HeadlessException if in a headless environment
107 if (GraphicsEnvironment
.isHeadless())
108 throw new HeadlessException();
112 * The browser calls this method to set the applet's stub, which is the
113 * low level interface to the browser. Manually setting this to null is
114 * asking for problems down the road.
116 * @param stub the applet stub for this applet
118 public final void setStub(AppletStub stub
)
122 if (width
!= 0 && height
!= 0)
123 stub
.appletResize (width
, height
);
127 * Tests whether or not this applet is currently active. An applet is active
128 * just before the browser invokes start(), and becomes inactive just
129 * before the browser invokes stop().
131 * @return <code>true</code> if this applet is active
133 public boolean isActive()
135 return stub
.isActive();
139 * Returns the basename URL of the document this applet is embedded in. This
140 * is everything up to the final '/'.
142 * @return the URL of the document this applet is embedded in
143 * @see #getCodeBase()
145 public URL
getDocumentBase()
147 return stub
.getDocumentBase();
151 * Returns the URL of the code base for this applet.
153 * @return the URL of the code base for this applet
155 public URL
getCodeBase()
157 return stub
.getCodeBase();
161 * Returns the value of the specified parameter that was specified in
162 * the <code><APPLET></code> tag for this applet.
164 * @param name the parameter name
165 * @return the parameter value, or null if the parameter does not exist
166 * @throws NullPointerException if name is null
168 public String
getParameter(String name
)
170 return stub
.getParameter(name
);
174 * Returns the applet context for this applet.
176 * @return the applet context for this applet
178 public AppletContext
getAppletContext()
180 return stub
.getAppletContext();
184 * Requests that the applet window for this applet be resized.
186 * @param width the new width in pixels
187 * @param height the new height in pixels
189 public void resize(int width
, int height
)
194 this.height
= height
;
197 stub
.appletResize(width
, height
);
201 * Requests that the applet window for this applet be resized.
203 * @param dim the requested dimensions
204 * @throws NullPointerException if dim is null
206 public void resize(Dimension dim
)
208 resize(dim
.width
, dim
.height
);
212 * Displays the specified message in the status window if that window
215 * @param message the status message, may be null
217 public void showStatus(String message
)
219 getAppletContext().showStatus(message
);
223 * Returns an image from the specified URL. Note that the image is not
224 * actually retrieved until the applet attempts to display it, so this
225 * method returns immediately.
227 * @param url the URL of the image
228 * @return the retrieved image
229 * @throws NullPointerException if url is null
231 public Image
getImage(URL url
)
233 return getAppletContext().getImage(url
);
237 * Returns an image from the specified absolute URL, and relative path
238 * from that URL. Note that the image is not actually retrieved until the
239 * applet attempts to display it, so this method returns immediately.
240 * This calls <code>getImage(new URL(url, name))</code>, but if building
241 * the new URL fails, this returns null.
243 * @param url the base URL of the image
244 * @param name the name of the image relative to the URL
245 * @return the retrieved image, or null on failure
246 * @see #getImage(URL)
248 public Image
getImage(URL url
, String name
)
252 return getImage(new URL(url
, name
));
254 catch (MalformedURLException e
)
261 * Returns an audio clip from the specified URL. This clip is not tied to
262 * any particular applet.
264 * @param url the URL of the audio clip
265 * @return the retrieved audio clip
266 * @throws NullPointerException if url is null
267 * @see #getAudioClip(URL)
270 public static final AudioClip
newAudioClip(URL url
)
272 return new URLAudioClip(url
);
276 * Returns an audio clip from the specified URL. Note that the clip is not
277 * actually retrieved until the applet attempts to play it, so this method
278 * returns immediately.
280 * @param url the URL of the audio clip
281 * @return the retrieved audio clip
282 * @throws NullPointerException if url is null
284 public AudioClip
getAudioClip(URL url
)
286 return getAppletContext().getAudioClip(url
);
290 * Returns an audio clip from the specified absolute URL, and relative path
291 * from that URL. Note that the clip is not actually retrieved until the
292 * applet attempts to play it, so this method returns immediately. This
293 * calls <code>getAudioClip(new URL(url, name))</code>, but if building
294 * the new URL fails, this returns null.
296 * @param url the base URL of the audio clip
297 * @param name the name of the clip relative to the URL
298 * @return the retrieved audio clip, or null on failure
299 * @see #getAudioClip(URL)
301 public AudioClip
getAudioClip(URL url
, String name
)
305 return getAudioClip(new URL(url
, name
));
307 catch (MalformedURLException e
)
314 * Returns a descriptive string with applet defined information. The
315 * implementation in this class returns <code>null</code>, so subclasses
316 * must override to return information.
318 * @return a string describing the author, version, and applet copyright
320 public String
getAppletInfo()
326 * Returns the locale for this applet, if it has been set. If no applet
327 * specific locale has been set, the default locale is returned.
329 * @return the locale for this applet
330 * @see Component#setLocale(Locale)
333 public Locale
getLocale()
335 return super.getLocale();
339 * Returns a list of parameters this applet supports. Each element of
340 * the outer array is an array of three strings with the name of the
341 * parameter, the data type or valid values, and a description. This
342 * method is optional and the default implementation returns null.
344 * @return the list of parameters supported by this applet
346 public String
[][] getParameterInfo()
352 * Loads and plays the audio clip pointed to by the specified URL. This does
353 * nothing if the URL does not point to a valid audio clip.
355 * @param url the URL of the audio clip
356 * @throws NullPointerException if url is null
357 * @see #getAudioClip(URL)
359 public void play(URL url
)
361 AudioClip ac
= getAudioClip(url
);
366 catch (Exception ignored
)
372 * Loads and plays the audio clip pointed to by the specified absolute URL,
373 * and relative path from that URL. This does nothing if the URL cannot be
374 * constructed, or if it does not point to a valid audio clip.
376 * @param url the base URL of the audio clip
377 * @param name the name of the audio clip relative to the URL
378 * @see #getAudioClip(URL, String)
381 public void play(URL url
, String name
)
385 getAudioClip(url
, name
).play();
387 catch (Exception ignored
)
393 * This method is called when the applet is first loaded, before start().
394 * The default implementation does nothing; override to do any one-time
406 * This method is called when the applet should start running. This is
407 * normally each time a web page containing it is loaded. The default
408 * implemention does nothing; override for your applet to be useful.
419 * This method is called when the applet should stop running. This is
420 * normally when the next web page is loaded. The default implementation
421 * does nothing; override for your applet to stop using resources when
422 * it is no longer visible, but may be restarted soon.
433 * This method is called when the applet is being unloaded. The default
434 * implementation does nothing; override for your applet to clean up
441 public void destroy()
446 * Gets the AccessibleContext associated with this applet, creating one if
447 * necessary. This always returns an instance of {@link AccessibleApplet}.
449 * @return the accessibility context of this applet
452 public AccessibleContext
getAccessibleContext()
454 if (accessibleContext
== null)
455 accessibleContext
= new AccessibleApplet();
456 return accessibleContext
;
460 * Read an applet from an object stream. This checks for a headless
461 * environment, then does the normal read.
463 * @param s the stream to read from
464 * @throws ClassNotFoundException if a class is not found
465 * @throws IOException if deserialization fails
466 * @throws HeadlessException if this is a headless environment
467 * @see GraphicsEnvironment#isHeadless()
470 private void readObject(ObjectInputStream s
)
471 throws ClassNotFoundException
, IOException
473 if (GraphicsEnvironment
.isHeadless())
474 throw new HeadlessException();
475 s
.defaultReadObject();
479 * This class provides accessibility support for Applets, and is the
480 * runtime type returned by {@link #getAccessibleContext()}.
482 * @author Eric Blake (ebb9@email.byu.edu)
485 protected class AccessibleApplet
extends AccessibleAWTPanel
488 * Compatible with JDK 1.4+.
490 private static final long serialVersionUID
= 8127374778187708896L;
493 * The default constructor.
495 protected AccessibleApplet()
500 * Get the role of this accessible object, a frame.
502 * @return the role of the object
503 * @see AccessibleRole#FRAME
505 public AccessibleRole
getAccessibleRole()
507 return AccessibleRole
.FRAME
;
511 * Get the state set of this accessible object. In addition to the default
512 * states of a Component, the applet can also be active.
514 * @return the role of the object
515 * @see AccessibleState
517 public AccessibleStateSet
getAccessibleStateSet()
519 AccessibleStateSet s
= super.getAccessibleStateSet();
521 s
.add(AccessibleState
.ACTIVE
);
524 } // class AccessibleApplet
526 private static class URLAudioClip
implements AudioClip
528 // The URL we will try to play.
529 // This is null if we have already tried to create an
530 // audio input stream from this URL.
533 // The real audio clip. This is null before the URL is read,
534 // and might be null afterward if we were unable to read the URL
538 public URLAudioClip(URL url
)
543 private synchronized Clip
getClip()
549 clip
= AudioSystem
.getClip();
550 clip
.open(AudioSystem
.getAudioInputStream(url
));
552 catch (LineUnavailableException _
)
556 catch (IOException _
)
560 catch (UnsupportedAudioFileException _
)
570 Clip myclip
= getClip();
572 myclip
.loop(Clip
.LOOP_CONTINUOUSLY
);
577 Clip myclip
= getClip();
584 Clip myclip
= getClip();
588 myclip
.setFramePosition(0);