1. /*
  2. * @(#)Applet.java 1.72 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.applet;
  8. import java.awt.*;
  9. import java.awt.image.ColorModel;
  10. import java.io.IOException;
  11. import java.io.ObjectInputStream;
  12. import java.net.URL;
  13. import java.net.MalformedURLException;
  14. import java.util.Hashtable;
  15. import java.util.Locale;
  16. import javax.accessibility.*;
  17. /**
  18. * An applet is a small program that is intended not to be run on
  19. * its own, but rather to be embedded inside another application.
  20. * <p>
  21. * The <code>Applet</code> class must be the superclass of any
  22. * applet that is to be embedded in a Web page or viewed by the Java
  23. * Applet Viewer. The <code>Applet</code> class provides a standard
  24. * interface between applets and their environment.
  25. *
  26. * @author Arthur van Hoff
  27. * @author Chris Warth
  28. * @version 1.72, 01/23/03
  29. * @since JDK1.0
  30. */
  31. public class Applet extends Panel {
  32. /**
  33. * Creates a new Applet object
  34. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  35. * returns true.
  36. * @see java.awt.GraphicsEnvironment#isHeadless
  37. * @since 1.4
  38. */
  39. public Applet() throws HeadlessException {
  40. if (GraphicsEnvironment.isHeadless()) {
  41. throw new HeadlessException();
  42. }
  43. }
  44. /**
  45. * Applets can be serialized but the following conventions MUST be followed:
  46. *
  47. * Before Serialization:
  48. * An applet must be in STOPPED state.
  49. *
  50. * After Deserialization:
  51. * The applet will be restored in STOPPED state (and most clients will
  52. * likely move it into RUNNING state).
  53. * The stub field will be restored by the reader.
  54. */
  55. transient private AppletStub stub;
  56. /* version ID for serialized form. */
  57. private static final long serialVersionUID = -5836846270535785031L;
  58. /**
  59. * Read an applet from an object input stream.
  60. * @exception HeadlessException if
  61. * <code>GraphicsEnvironment.isHeadless()</code> returns
  62. * <code>true</code>
  63. * @serial
  64. * @see java.awt.GraphicsEnvironment#isHeadless
  65. * @since 1.4
  66. */
  67. private void readObject(ObjectInputStream s)
  68. throws ClassNotFoundException, IOException, HeadlessException {
  69. if (GraphicsEnvironment.isHeadless()) {
  70. throw new HeadlessException();
  71. }
  72. s.defaultReadObject();
  73. }
  74. /**
  75. * Sets this applet's stub. This is done automatically by the system.
  76. *
  77. * @param stub the new stub.
  78. */
  79. public final void setStub(AppletStub stub) {
  80. this.stub = (AppletStub)stub;
  81. }
  82. /**
  83. * Determines if this applet is active. An applet is marked active
  84. * just before its <code>start</code> method is called. It becomes
  85. * inactive just before its <code>stop</code> method is called.
  86. *
  87. * @return <code>true</code> if the applet is active;
  88. * <code>false</code> otherwise.
  89. * @see java.applet.Applet#start()
  90. * @see java.applet.Applet#stop()
  91. */
  92. public boolean isActive() {
  93. if (stub != null) {
  94. return stub.isActive();
  95. } else { // If stub field not filled in, applet never active
  96. return false;
  97. }
  98. }
  99. /**
  100. * Gets the URL of the document in which this applet is embedded.
  101. * For example, suppose an applet is contained
  102. * within the document:
  103. * <blockquote><pre>
  104. * http://java.sun.com/products/jdk/1.2/index.html
  105. * </pre></blockquote>
  106. * The document base is:
  107. * <blockquote><pre>
  108. * http://java.sun.com/products/jdk/1.2/index.html
  109. * </pre></blockquote>
  110. *
  111. * @return the {@link java.net.URL} of the document that contains this
  112. * applet.
  113. * @see java.applet.Applet#getCodeBase()
  114. */
  115. public URL getDocumentBase() {
  116. return stub.getDocumentBase();
  117. }
  118. /**
  119. * Gets the base URL. This is the URL of the directory which contains this applet.
  120. *
  121. * @return the base {@link java.net.URL} of
  122. * the directory which contains this applet.
  123. * @see java.applet.Applet#getDocumentBase()
  124. */
  125. public URL getCodeBase() {
  126. return stub.getCodeBase();
  127. }
  128. /**
  129. * Returns the value of the named parameter in the HTML tag. For
  130. * example, if this applet is specified as
  131. * <blockquote><pre>
  132. * <applet code="Clock" width=50 height=50>
  133. * <param name=Color value="blue">
  134. * </applet>
  135. * </pre></blockquote>
  136. * <p>
  137. * then a call to <code>getParameter("Color")</code> returns the
  138. * value <code>"blue"</code>.
  139. * <p>
  140. * The <code>name</code> argument is case insensitive.
  141. *
  142. * @param name a parameter name.
  143. * @return the value of the named parameter,
  144. * or <code>null</code> if not set.
  145. */
  146. public String getParameter(String name) {
  147. return stub.getParameter(name);
  148. }
  149. /**
  150. * Determines this applet's context, which allows the applet to
  151. * query and affect the environment in which it runs.
  152. * <p>
  153. * This environment of an applet represents the document that
  154. * contains the applet.
  155. *
  156. * @return the applet's context.
  157. */
  158. public AppletContext getAppletContext() {
  159. return stub.getAppletContext();
  160. }
  161. /**
  162. * Requests that this applet be resized.
  163. *
  164. * @param width the new requested width for the applet.
  165. * @param height the new requested height for the applet.
  166. */
  167. public void resize(int width, int height) {
  168. Dimension d = size();
  169. if ((d.width != width) || (d.height != height)) {
  170. super.resize(width, height);
  171. if (stub != null) {
  172. stub.appletResize(width, height);
  173. }
  174. }
  175. }
  176. /**
  177. * Requests that this applet be resized.
  178. *
  179. * @param d an object giving the new width and height.
  180. */
  181. public void resize(Dimension d) {
  182. resize(d.width, d.height);
  183. }
  184. /**
  185. * Requests that the argument string be displayed in the
  186. * "status window". Many browsers and applet viewers
  187. * provide such a window, where the application can inform users of
  188. * its current state.
  189. *
  190. * @param msg a string to display in the status window.
  191. */
  192. public void showStatus(String msg) {
  193. getAppletContext().showStatus(msg);
  194. }
  195. /**
  196. * Returns an <code>Image</code> object that can then be painted on
  197. * the screen. The <code>url</code> that is passed as an argument
  198. * must specify an absolute URL.
  199. * <p>
  200. * This method always returns immediately, whether or not the image
  201. * exists. When this applet attempts to draw the image on the screen,
  202. * the data will be loaded. The graphics primitives that draw the
  203. * image will incrementally paint on the screen.
  204. *
  205. * @param url an absolute URL giving the location of the image.
  206. * @return the image at the specified URL.
  207. * @see java.awt.Image
  208. */
  209. public Image getImage(URL url) {
  210. return getAppletContext().getImage(url);
  211. }
  212. /**
  213. * Returns an <code>Image</code> object that can then be painted on
  214. * the screen. The <code>url</code> argument must specify an absolute
  215. * URL. The <code>name</code> argument is a specifier that is
  216. * relative to the <code>url</code> argument.
  217. * <p>
  218. * This method always returns immediately, whether or not the image
  219. * exists. When this applet attempts to draw the image on the screen,
  220. * the data will be loaded. The graphics primitives that draw the
  221. * image will incrementally paint on the screen.
  222. *
  223. * @param url an absolute URL giving the base location of the image.
  224. * @param name the location of the image, relative to the
  225. * <code>url</code> argument.
  226. * @return the image at the specified URL.
  227. * @see java.awt.Image
  228. */
  229. public Image getImage(URL url, String name) {
  230. try {
  231. return getImage(new URL(url, name));
  232. } catch (MalformedURLException e) {
  233. return null;
  234. }
  235. }
  236. /**
  237. * Get an audio clip from the given URL.
  238. *
  239. * @param url points to the audio clip
  240. * @return the audio clip at the specified URL.
  241. *
  242. * @since 1.2
  243. */
  244. public final static AudioClip newAudioClip(URL url) {
  245. return new sun.applet.AppletAudioClip(url);
  246. }
  247. /**
  248. * Returns the <code>AudioClip</code> object specified by the
  249. * <code>URL</code> argument.
  250. * <p>
  251. * This method always returns immediately, whether or not the audio
  252. * clip exists. When this applet attempts to play the audio clip, the
  253. * data will be loaded.
  254. *
  255. * @param url an absolute URL giving the location of the audio clip.
  256. * @return the audio clip at the specified URL.
  257. * @see java.applet.AudioClip
  258. */
  259. public AudioClip getAudioClip(URL url) {
  260. return getAppletContext().getAudioClip(url);
  261. }
  262. /**
  263. * Returns the <code>AudioClip</code> object specified by the
  264. * <code>URL</code> and <code>name</code> arguments.
  265. * <p>
  266. * This method always returns immediately, whether or not the audio
  267. * clip exists. When this applet attempts to play the audio clip, the
  268. * data will be loaded.
  269. *
  270. * @param url an absolute URL giving the base location of the
  271. * audio clip.
  272. * @param name the location of the audio clip, relative to the
  273. * <code>url</code> argument.
  274. * @return the audio clip at the specified URL.
  275. * @see java.applet.AudioClip
  276. */
  277. public AudioClip getAudioClip(URL url, String name) {
  278. try {
  279. return getAudioClip(new URL(url, name));
  280. } catch (MalformedURLException e) {
  281. return null;
  282. }
  283. }
  284. /**
  285. * Returns information about this applet. An applet should override
  286. * this method to return a <code>String</code> containing information
  287. * about the author, version, and copyright of the applet.
  288. * <p>
  289. * The implementation of this method provided by the
  290. * <code>Applet</code> class returns <code>null</code>.
  291. *
  292. * @return a string containing information about the author, version, and
  293. * copyright of the applet.
  294. */
  295. public String getAppletInfo() {
  296. return null;
  297. }
  298. /**
  299. * Gets the Locale for the applet, if it has been set.
  300. * If no Locale has been set, then the default Locale
  301. * is returned.
  302. *
  303. * @return the Locale for the applet
  304. * @since JDK1.1
  305. */
  306. public Locale getLocale() {
  307. Locale locale = super.getLocale();
  308. if (locale == null) {
  309. return Locale.getDefault();
  310. }
  311. return locale;
  312. }
  313. /**
  314. * Returns information about the parameters that are understood by
  315. * this applet. An applet should override this method to return an
  316. * array of <code>Strings</code> describing these parameters.
  317. * <p>
  318. * Each element of the array should be a set of three
  319. * <code>Strings</code> containing the name, the type, and a
  320. * description. For example:
  321. * <p><blockquote><pre>
  322. * String pinfo[][] = {
  323. * {"fps", "1-10", "frames per second"},
  324. * {"repeat", "boolean", "repeat image loop"},
  325. * {"imgs", "url", "images directory"}
  326. * };
  327. * </pre></blockquote>
  328. * <p>
  329. * The implementation of this method provided by the
  330. * <code>Applet</code> class returns <code>null</code>.
  331. *
  332. * @return an array describing the parameters this applet looks for.
  333. */
  334. public String[][] getParameterInfo() {
  335. return null;
  336. }
  337. /**
  338. * Plays the audio clip at the specified absolute URL. Nothing
  339. * happens if the audio clip cannot be found.
  340. *
  341. * @param url an absolute URL giving the location of the audio clip.
  342. */
  343. public void play(URL url) {
  344. AudioClip clip = getAudioClip(url);
  345. if (clip != null) {
  346. clip.play();
  347. }
  348. }
  349. /**
  350. * Plays the audio clip given the URL and a specifier that is
  351. * relative to it. Nothing happens if the audio clip cannot be found.
  352. *
  353. * @param url an absolute URL giving the base location of the
  354. * audio clip.
  355. * @param name the location of the audio clip, relative to the
  356. * <code>url</code> argument.
  357. */
  358. public void play(URL url, String name) {
  359. AudioClip clip = getAudioClip(url, name);
  360. if (clip != null) {
  361. clip.play();
  362. }
  363. }
  364. /**
  365. * Called by the browser or applet viewer to inform
  366. * this applet that it has been loaded into the system. It is always
  367. * called before the first time that the <code>start</code> method is
  368. * called.
  369. * <p>
  370. * A subclass of <code>Applet</code> should override this method if
  371. * it has initialization to perform. For example, an applet with
  372. * threads would use the <code>init</code> method to create the
  373. * threads and the <code>destroy</code> method to kill them.
  374. * <p>
  375. * The implementation of this method provided by the
  376. * <code>Applet</code> class does nothing.
  377. *
  378. * @see java.applet.Applet#destroy()
  379. * @see java.applet.Applet#start()
  380. * @see java.applet.Applet#stop()
  381. */
  382. public void init() {
  383. }
  384. /**
  385. * Called by the browser or applet viewer to inform
  386. * this applet that it should start its execution. It is called after
  387. * the <code>init</code> method and each time the applet is revisited
  388. * in a Web page.
  389. * <p>
  390. * A subclass of <code>Applet</code> should override this method if
  391. * it has any operation that it wants to perform each time the Web
  392. * page containing it is visited. For example, an applet with
  393. * animation might want to use the <code>start</code> method to
  394. * resume animation, and the <code>stop</code> method to suspend the
  395. * animation.
  396. * <p>
  397. * The implementation of this method provided by the
  398. * <code>Applet</code> class does nothing.
  399. *
  400. * @see java.applet.Applet#destroy()
  401. * @see java.applet.Applet#init()
  402. * @see java.applet.Applet#stop()
  403. */
  404. public void start() {
  405. }
  406. /**
  407. * Called by the browser or applet viewer to inform
  408. * this applet that it should stop its execution. It is called when
  409. * the Web page that contains this applet has been replaced by
  410. * another page, and also just before the applet is to be destroyed.
  411. * <p>
  412. * A subclass of <code>Applet</code> should override this method if
  413. * it has any operation that it wants to perform each time the Web
  414. * page containing it is no longer visible. For example, an applet
  415. * with animation might want to use the <code>start</code> method to
  416. * resume animation, and the <code>stop</code> method to suspend the
  417. * animation.
  418. * <p>
  419. * The implementation of this method provided by the
  420. * <code>Applet</code> class does nothing.
  421. *
  422. * @see java.applet.Applet#destroy()
  423. * @see java.applet.Applet#init()
  424. */
  425. public void stop() {
  426. }
  427. /**
  428. * Called by the browser or applet viewer to inform
  429. * this applet that it is being reclaimed and that it should destroy
  430. * any resources that it has allocated. The <code>stop</code> method
  431. * will always be called before <code>destroy</code>.
  432. * <p>
  433. * A subclass of <code>Applet</code> should override this method if
  434. * it has any operation that it wants to perform before it is
  435. * destroyed. For example, an applet with threads would use the
  436. * <code>init</code> method to create the threads and the
  437. * <code>destroy</code> method to kill them.
  438. * <p>
  439. * The implementation of this method provided by the
  440. * <code>Applet</code> class does nothing.
  441. *
  442. * @see java.applet.Applet#init()
  443. * @see java.applet.Applet#start()
  444. * @see java.applet.Applet#stop()
  445. */
  446. public void destroy() {
  447. }
  448. //
  449. // Accessibility support
  450. //
  451. AccessibleContext accessibleContext = null;
  452. /**
  453. * Gets the AccessibleContext associated with this Applet.
  454. * For applets, the AccessibleContext takes the form of an
  455. * AccessibleApplet.
  456. * A new AccessibleApplet instance is created if necessary.
  457. *
  458. * @return an AccessibleApplet that serves as the
  459. * AccessibleContext of this Applet
  460. */
  461. public AccessibleContext getAccessibleContext() {
  462. if (accessibleContext == null) {
  463. accessibleContext = new AccessibleApplet();
  464. }
  465. return accessibleContext;
  466. }
  467. /**
  468. * This class implements accessibility support for the
  469. * <code>Applet</code> class. It provides an implementation of the
  470. * Java Accessibility API appropriate to applet user-interface elements.
  471. */
  472. protected class AccessibleApplet extends AccessibleAWTPanel {
  473. /**
  474. * Get the role of this object.
  475. *
  476. * @return an instance of AccessibleRole describing the role of the
  477. * object
  478. */
  479. public AccessibleRole getAccessibleRole() {
  480. return AccessibleRole.FRAME;
  481. }
  482. /**
  483. * Get the state of this object.
  484. *
  485. * @return an instance of AccessibleStateSet containing the current
  486. * state set of the object
  487. * @see AccessibleState
  488. */
  489. public AccessibleStateSet getAccessibleStateSet() {
  490. AccessibleStateSet states = super.getAccessibleStateSet();
  491. states.add(AccessibleState.ACTIVE);
  492. return states;
  493. }
  494. }
  495. }