Applet.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /* Applet.java -- Java base applet class
  2. Copyright (C) 1999, 2002, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.applet;
  32. import java.awt.Component;
  33. import java.awt.Dimension;
  34. import java.awt.GraphicsEnvironment;
  35. import java.awt.HeadlessException;
  36. import java.awt.Image;
  37. import java.awt.Panel;
  38. import java.io.IOException;
  39. import java.io.ObjectInputStream;
  40. import java.net.MalformedURLException;
  41. import java.net.URL;
  42. import java.util.Locale;
  43. import javax.accessibility.AccessibleContext;
  44. import javax.accessibility.AccessibleRole;
  45. import javax.accessibility.AccessibleState;
  46. import javax.accessibility.AccessibleStateSet;
  47. import javax.sound.sampled.AudioSystem;
  48. import javax.sound.sampled.Clip;
  49. import javax.sound.sampled.LineUnavailableException;
  50. import javax.sound.sampled.UnsupportedAudioFileException;
  51. /**
  52. * This is the base applet class. An applet is a Java program that
  53. * runs inside a web browser or other applet viewer in a restricted
  54. * environment.
  55. *
  56. * <p>To be useful, a subclass should override at least start(). Also useful
  57. * are init, stop, and destroy for control purposes, and getAppletInfo and
  58. * getParameterInfo for descriptive purposes.
  59. *
  60. * @author Aaron M. Renn (arenn@urbanophile.com)
  61. * @author Eric Blake (ebb9@email.byu.edu)
  62. * @since 1.0
  63. * @status updated to 1.4
  64. */
  65. public class Applet extends Panel
  66. {
  67. /**
  68. * Compatible with JDK 1.0+.
  69. */
  70. private static final long serialVersionUID = -5836846270535785031L;
  71. /** The applet stub for this applet. */
  72. private transient AppletStub stub;
  73. /** Some applets call setSize in their constructors. In that case,
  74. these fields are used to store width and height values until a
  75. stub is set. */
  76. private transient int width;
  77. private transient int height;
  78. /**
  79. * The accessibility context for this applet.
  80. *
  81. * @serial the accessibleContext for this
  82. * @since 1.2
  83. */
  84. private AccessibleContext accessibleContext;
  85. /**
  86. * Default constructor for subclasses.
  87. *
  88. * @throws HeadlessException if in a headless environment
  89. */
  90. public Applet()
  91. {
  92. if (GraphicsEnvironment.isHeadless())
  93. throw new HeadlessException();
  94. }
  95. /**
  96. * The browser calls this method to set the applet's stub, which is the
  97. * low level interface to the browser. Manually setting this to null is
  98. * asking for problems down the road.
  99. *
  100. * @param stub the applet stub for this applet
  101. */
  102. public final void setStub(AppletStub stub)
  103. {
  104. this.stub = stub;
  105. if (width != 0 && height != 0)
  106. stub.appletResize (width, height);
  107. }
  108. /**
  109. * Tests whether or not this applet is currently active. An applet is active
  110. * just before the browser invokes start(), and becomes inactive just
  111. * before the browser invokes stop().
  112. *
  113. * @return <code>true</code> if this applet is active
  114. */
  115. public boolean isActive()
  116. {
  117. return stub.isActive();
  118. }
  119. /**
  120. * Returns the basename URL of the document this applet is embedded in. This
  121. * is everything up to the final '/'.
  122. *
  123. * @return the URL of the document this applet is embedded in
  124. * @see #getCodeBase()
  125. */
  126. public URL getDocumentBase()
  127. {
  128. return stub.getDocumentBase();
  129. }
  130. /**
  131. * Returns the URL of the code base for this applet.
  132. *
  133. * @return the URL of the code base for this applet
  134. */
  135. public URL getCodeBase()
  136. {
  137. return stub.getCodeBase();
  138. }
  139. /**
  140. * Returns the value of the specified parameter that was specified in
  141. * the <code>&lt;APPLET&gt;</code> tag for this applet.
  142. *
  143. * @param name the parameter name
  144. * @return the parameter value, or null if the parameter does not exist
  145. * @throws NullPointerException if name is null
  146. */
  147. public String getParameter(String name)
  148. {
  149. return stub.getParameter(name);
  150. }
  151. /**
  152. * Returns the applet context for this applet.
  153. *
  154. * @return the applet context for this applet
  155. */
  156. public AppletContext getAppletContext()
  157. {
  158. return stub.getAppletContext();
  159. }
  160. /**
  161. * Requests that the applet window for this applet be resized.
  162. *
  163. * @param width the new width in pixels
  164. * @param height the new height in pixels
  165. */
  166. public void resize(int width, int height)
  167. {
  168. if (stub == null)
  169. {
  170. this.width = width;
  171. this.height = height;
  172. }
  173. else
  174. stub.appletResize(width, height);
  175. }
  176. /**
  177. * Requests that the applet window for this applet be resized.
  178. *
  179. * @param dim the requested dimensions
  180. * @throws NullPointerException if dim is null
  181. */
  182. public void resize(Dimension dim)
  183. {
  184. resize(dim.width, dim.height);
  185. }
  186. /**
  187. * Displays the specified message in the status window if that window
  188. * exists.
  189. *
  190. * @param message the status message, may be null
  191. */
  192. public void showStatus(String message)
  193. {
  194. getAppletContext().showStatus(message);
  195. }
  196. /**
  197. * Returns an image from the specified URL. Note that the image is not
  198. * actually retrieved until the applet attempts to display it, so this
  199. * method returns immediately.
  200. *
  201. * @param url the URL of the image
  202. * @return the retrieved image
  203. * @throws NullPointerException if url is null
  204. */
  205. public Image getImage(URL url)
  206. {
  207. return getAppletContext().getImage(url);
  208. }
  209. /**
  210. * Returns an image from the specified absolute URL, and relative path
  211. * from that URL. Note that the image is not actually retrieved until the
  212. * applet attempts to display it, so this method returns immediately.
  213. * This calls <code>getImage(new URL(url, name))</code>, but if building
  214. * the new URL fails, this returns null.
  215. *
  216. * @param url the base URL of the image
  217. * @param name the name of the image relative to the URL
  218. * @return the retrieved image, or null on failure
  219. * @see #getImage(URL)
  220. */
  221. public Image getImage(URL url, String name)
  222. {
  223. try
  224. {
  225. return getImage(new URL(url, name));
  226. }
  227. catch (MalformedURLException e)
  228. {
  229. return null;
  230. }
  231. }
  232. /**
  233. * Returns an audio clip from the specified URL. This clip is not tied to
  234. * any particular applet.
  235. *
  236. * @param url the URL of the audio clip
  237. * @return the retrieved audio clip
  238. * @throws NullPointerException if url is null
  239. * @see #getAudioClip(URL)
  240. * @since 1.2
  241. */
  242. public static final AudioClip newAudioClip(URL url)
  243. {
  244. return new URLAudioClip(url);
  245. }
  246. /**
  247. * Returns an audio clip from the specified URL. Note that the clip is not
  248. * actually retrieved until the applet attempts to play it, so this method
  249. * returns immediately.
  250. *
  251. * @param url the URL of the audio clip
  252. * @return the retrieved audio clip
  253. * @throws NullPointerException if url is null
  254. */
  255. public AudioClip getAudioClip(URL url)
  256. {
  257. return getAppletContext().getAudioClip(url);
  258. }
  259. /**
  260. * Returns an audio clip from the specified absolute URL, and relative path
  261. * from that URL. Note that the clip is not actually retrieved until the
  262. * applet attempts to play it, so this method returns immediately. This
  263. * calls <code>getAudioClip(new URL(url, name))</code>, but if building
  264. * the new URL fails, this returns null.
  265. *
  266. * @param url the base URL of the audio clip
  267. * @param name the name of the clip relative to the URL
  268. * @return the retrieved audio clip, or null on failure
  269. * @see #getAudioClip(URL)
  270. */
  271. public AudioClip getAudioClip(URL url, String name)
  272. {
  273. try
  274. {
  275. return getAudioClip(new URL(url, name));
  276. }
  277. catch (MalformedURLException e)
  278. {
  279. return null;
  280. }
  281. }
  282. /**
  283. * Returns a descriptive string with applet defined information. The
  284. * implementation in this class returns <code>null</code>, so subclasses
  285. * must override to return information.
  286. *
  287. * @return a string describing the author, version, and applet copyright
  288. */
  289. public String getAppletInfo()
  290. {
  291. return null;
  292. }
  293. /**
  294. * Returns the locale for this applet, if it has been set. If no applet
  295. * specific locale has been set, the default locale is returned.
  296. *
  297. * @return the locale for this applet
  298. * @see Component#setLocale(Locale)
  299. * @since 1.1
  300. */
  301. public Locale getLocale()
  302. {
  303. return super.getLocale();
  304. }
  305. /**
  306. * Returns a list of parameters this applet supports. Each element of
  307. * the outer array is an array of three strings with the name of the
  308. * parameter, the data type or valid values, and a description. This
  309. * method is optional and the default implementation returns null.
  310. *
  311. * @return the list of parameters supported by this applet
  312. */
  313. public String[][] getParameterInfo()
  314. {
  315. return null;
  316. }
  317. /**
  318. * Loads and plays the audio clip pointed to by the specified URL. This does
  319. * nothing if the URL does not point to a valid audio clip.
  320. *
  321. * @param url the URL of the audio clip
  322. * @throws NullPointerException if url is null
  323. * @see #getAudioClip(URL)
  324. */
  325. public void play(URL url)
  326. {
  327. AudioClip ac = getAudioClip(url);
  328. try
  329. {
  330. ac.play();
  331. }
  332. catch (Exception ignored)
  333. {
  334. }
  335. }
  336. /**
  337. * Loads and plays the audio clip pointed to by the specified absolute URL,
  338. * and relative path from that URL. This does nothing if the URL cannot be
  339. * constructed, or if it does not point to a valid audio clip.
  340. *
  341. * @param url the base URL of the audio clip
  342. * @param name the name of the audio clip relative to the URL
  343. * @see #getAudioClip(URL, String)
  344. * @see #play(URL)
  345. */
  346. public void play(URL url, String name)
  347. {
  348. try
  349. {
  350. getAudioClip(url, name).play();
  351. }
  352. catch (Exception ignored)
  353. {
  354. }
  355. }
  356. /**
  357. * This method is called when the applet is first loaded, before start().
  358. * The default implementation does nothing; override to do any one-time
  359. * initialization.
  360. *
  361. * @see #start()
  362. * @see #stop()
  363. * @see #destroy()
  364. */
  365. public void init()
  366. {
  367. }
  368. /**
  369. * This method is called when the applet should start running. This is
  370. * normally each time a web page containing it is loaded. The default
  371. * implemention does nothing; override for your applet to be useful.
  372. *
  373. * @see #init()
  374. * @see #stop()
  375. * @see #destroy()
  376. */
  377. public void start()
  378. {
  379. }
  380. /**
  381. * This method is called when the applet should stop running. This is
  382. * normally when the next web page is loaded. The default implementation
  383. * does nothing; override for your applet to stop using resources when
  384. * it is no longer visible, but may be restarted soon.
  385. *
  386. * @see #init()
  387. * @see #start()
  388. * @see #destroy()
  389. */
  390. public void stop()
  391. {
  392. }
  393. /**
  394. * This method is called when the applet is being unloaded. The default
  395. * implementation does nothing; override for your applet to clean up
  396. * resources on exit.
  397. *
  398. * @see #init()
  399. * @see #start()
  400. * @see #stop()
  401. */
  402. public void destroy()
  403. {
  404. }
  405. /**
  406. * Gets the AccessibleContext associated with this applet, creating one if
  407. * necessary. This always returns an instance of {@link AccessibleApplet}.
  408. *
  409. * @return the accessibility context of this applet
  410. * @since 1.3
  411. */
  412. public AccessibleContext getAccessibleContext()
  413. {
  414. if (accessibleContext == null)
  415. accessibleContext = new AccessibleApplet();
  416. return accessibleContext;
  417. }
  418. /**
  419. * Read an applet from an object stream. This checks for a headless
  420. * environment, then does the normal read.
  421. *
  422. * @param s the stream to read from
  423. * @throws ClassNotFoundException if a class is not found
  424. * @throws IOException if deserialization fails
  425. * @throws HeadlessException if this is a headless environment
  426. * @see GraphicsEnvironment#isHeadless()
  427. * @since 1.4
  428. */
  429. private void readObject(ObjectInputStream s)
  430. throws ClassNotFoundException, IOException
  431. {
  432. if (GraphicsEnvironment.isHeadless())
  433. throw new HeadlessException();
  434. s.defaultReadObject();
  435. }
  436. /**
  437. * This class provides accessibility support for Applets, and is the
  438. * runtime type returned by {@link #getAccessibleContext()}.
  439. *
  440. * @author Eric Blake (ebb9@email.byu.edu)
  441. * @since 1.3
  442. */
  443. protected class AccessibleApplet extends AccessibleAWTPanel
  444. {
  445. /**
  446. * Compatible with JDK 1.4+.
  447. */
  448. private static final long serialVersionUID = 8127374778187708896L;
  449. /**
  450. * The default constructor.
  451. */
  452. protected AccessibleApplet()
  453. {
  454. }
  455. /**
  456. * Get the role of this accessible object, a frame.
  457. *
  458. * @return the role of the object
  459. * @see AccessibleRole#FRAME
  460. */
  461. public AccessibleRole getAccessibleRole()
  462. {
  463. return AccessibleRole.FRAME;
  464. }
  465. /**
  466. * Get the state set of this accessible object. In addition to the default
  467. * states of a Component, the applet can also be active.
  468. *
  469. * @return the role of the object
  470. * @see AccessibleState
  471. */
  472. public AccessibleStateSet getAccessibleStateSet()
  473. {
  474. AccessibleStateSet s = super.getAccessibleStateSet();
  475. if (isActive())
  476. s.add(AccessibleState.ACTIVE);
  477. return s;
  478. }
  479. } // class AccessibleApplet
  480. private static class URLAudioClip implements AudioClip
  481. {
  482. // The URL we will try to play.
  483. // This is null if we have already tried to create an
  484. // audio input stream from this URL.
  485. private URL url;
  486. // The real audio clip. This is null before the URL is read,
  487. // and might be null afterward if we were unable to read the URL
  488. // for some reason.
  489. private Clip clip;
  490. public URLAudioClip(URL url)
  491. {
  492. this.url = url;
  493. }
  494. private synchronized Clip getClip()
  495. {
  496. if (url == null)
  497. return clip;
  498. try
  499. {
  500. clip = AudioSystem.getClip();
  501. clip.open(AudioSystem.getAudioInputStream(url));
  502. }
  503. catch (LineUnavailableException _)
  504. {
  505. // Ignore.
  506. }
  507. catch (IOException _)
  508. {
  509. // Ignore.
  510. }
  511. catch (UnsupportedAudioFileException _)
  512. {
  513. // Ignore.
  514. }
  515. url = null;
  516. return clip;
  517. }
  518. public void loop()
  519. {
  520. Clip myclip = getClip();
  521. if (myclip != null)
  522. myclip.loop(Clip.LOOP_CONTINUOUSLY);
  523. }
  524. public void play()
  525. {
  526. Clip myclip = getClip();
  527. if (myclip != null)
  528. myclip.start();
  529. }
  530. public void stop()
  531. {
  532. Clip myclip = getClip();
  533. if (myclip != null)
  534. {
  535. myclip.stop();
  536. myclip.setFramePosition(0);
  537. }
  538. }
  539. }
  540. } // class Applet