1. /*
  2. * @(#)Applet.java 1.77 04/06/22
  3. *
  4. * Copyright 2004 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.77, 06/22/04
  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. * <p>If there is a security manager, its <code> checkPermission </code>
  77. * method is called with the
  78. * <code>AWTPermission("setAppletStub")</code>
  79. * permission if a stub has already been set.
  80. * @param stub the new stub.
  81. * @exception SecurityException if the caller cannot set the stub
  82. */
  83. public final void setStub(AppletStub stub) {
  84. if (this.stub != null) {
  85. SecurityManager s = System.getSecurityManager();
  86. if (s != null) {
  87. s.checkPermission(new AWTPermission("setAppletStub"));
  88. }
  89. }
  90. this.stub = (AppletStub)stub;
  91. }
  92. /**
  93. * Determines if this applet is active. An applet is marked active
  94. * just before its <code>start</code> method is called. It becomes
  95. * inactive just before its <code>stop</code> method is called.
  96. *
  97. * @return <code>true</code> if the applet is active;
  98. * <code>false</code> otherwise.
  99. * @see java.applet.Applet#start()
  100. * @see java.applet.Applet#stop()
  101. */
  102. public boolean isActive() {
  103. if (stub != null) {
  104. return stub.isActive();
  105. } else { // If stub field not filled in, applet never active
  106. return false;
  107. }
  108. }
  109. /**
  110. * Gets the URL of the document in which this applet is embedded.
  111. * For example, suppose an applet is contained
  112. * within the document:
  113. * <blockquote><pre>
  114. * http://java.sun.com/products/jdk/1.2/index.html
  115. * </pre></blockquote>
  116. * The document base is:
  117. * <blockquote><pre>
  118. * http://java.sun.com/products/jdk/1.2/index.html
  119. * </pre></blockquote>
  120. *
  121. * @return the {@link java.net.URL} of the document that contains this
  122. * applet.
  123. * @see java.applet.Applet#getCodeBase()
  124. */
  125. public URL getDocumentBase() {
  126. return stub.getDocumentBase();
  127. }
  128. /**
  129. * Gets the base URL. This is the URL of the directory which contains this applet.
  130. *
  131. * @return the base {@link java.net.URL} of
  132. * the directory which contains this applet.
  133. * @see java.applet.Applet#getDocumentBase()
  134. */
  135. public URL getCodeBase() {
  136. return stub.getCodeBase();
  137. }
  138. /**
  139. * Returns the value of the named parameter in the HTML tag. For
  140. * example, if this applet is specified as
  141. * <blockquote><pre>
  142. * <applet code="Clock" width=50 height=50>
  143. * <param name=Color value="blue">
  144. * </applet>
  145. * </pre></blockquote>
  146. * <p>
  147. * then a call to <code>getParameter("Color")</code> returns the
  148. * value <code>"blue"</code>.
  149. * <p>
  150. * The <code>name</code> argument is case insensitive.
  151. *
  152. * @param name a parameter name.
  153. * @return the value of the named parameter,
  154. * or <code>null</code> if not set.
  155. */
  156. public String getParameter(String name) {
  157. return stub.getParameter(name);
  158. }
  159. /**
  160. * Determines this applet's context, which allows the applet to
  161. * query and affect the environment in which it runs.
  162. * <p>
  163. * This environment of an applet represents the document that
  164. * contains the applet.
  165. *
  166. * @return the applet's context.
  167. */
  168. public AppletContext getAppletContext() {
  169. return stub.getAppletContext();
  170. }
  171. /**
  172. * Requests that this applet be resized.
  173. *
  174. * @param width the new requested width for the applet.
  175. * @param height the new requested height for the applet.
  176. */
  177. public void resize(int width, int height) {
  178. Dimension d = size();
  179. if ((d.width != width) || (d.height != height)) {
  180. super.resize(width, height);
  181. if (stub != null) {
  182. stub.appletResize(width, height);
  183. }
  184. }
  185. }
  186. /**
  187. * Requests that this applet be resized.
  188. *
  189. * @param d an object giving the new width and height.
  190. */
  191. public void resize(Dimension d) {
  192. resize(d.width, d.height);
  193. }
  194. /**
  195. * Requests that the argument string be displayed in the
  196. * "status window". Many browsers and applet viewers
  197. * provide such a window, where the application can inform users of
  198. * its current state.
  199. *
  200. * @param msg a string to display in the status window.
  201. */
  202. public void showStatus(String msg) {
  203. getAppletContext().showStatus(msg);
  204. }
  205. /**
  206. * Returns an <code>Image</code> object that can then be painted on
  207. * the screen. The <code>url</code> that is passed as an argument
  208. * must specify an absolute URL.
  209. * <p>
  210. * This method always returns immediately, whether or not the image
  211. * exists. When this applet attempts to draw the image on the screen,
  212. * the data will be loaded. The graphics primitives that draw the
  213. * image will incrementally paint on the screen.
  214. *
  215. * @param url an absolute URL giving the location of the image.
  216. * @return the image at the specified URL.
  217. * @see java.awt.Image
  218. */
  219. public Image getImage(URL url) {
  220. return getAppletContext().getImage(url);
  221. }
  222. /**
  223. * Returns an <code>Image</code> object that can then be painted on
  224. * the screen. The <code>url</code> argument must specify an absolute
  225. * URL. The <code>name</code> argument is a specifier that is
  226. * relative to the <code>url</code> argument.
  227. * <p>
  228. * This method always returns immediately, whether or not the image
  229. * exists. When this applet attempts to draw the image on the screen,
  230. * the data will be loaded. The graphics primitives that draw the
  231. * image will incrementally paint on the screen.
  232. *
  233. * @param url an absolute URL giving the base location of the image.
  234. * @param name the location of the image, relative to the
  235. * <code>url</code> argument.
  236. * @return the image at the specified URL.
  237. * @see java.awt.Image
  238. */
  239. public Image getImage(URL url, String name) {
  240. try {
  241. return getImage(new URL(url, name));
  242. } catch (MalformedURLException e) {
  243. return null;
  244. }
  245. }
  246. /**
  247. * Get an audio clip from the given URL.
  248. *
  249. * @param url points to the audio clip
  250. * @return the audio clip at the specified URL.
  251. *
  252. * @since 1.2
  253. */
  254. public final static AudioClip newAudioClip(URL url) {
  255. return new sun.applet.AppletAudioClip(url);
  256. }
  257. /**
  258. * Returns the <code>AudioClip</code> object specified by the
  259. * <code>URL</code> argument.
  260. * <p>
  261. * This method always returns immediately, whether or not the audio
  262. * clip exists. When this applet attempts to play the audio clip, the
  263. * data will be loaded.
  264. *
  265. * @param url an absolute URL giving the location of the audio clip.
  266. * @return the audio clip at the specified URL.
  267. * @see java.applet.AudioClip
  268. */
  269. public AudioClip getAudioClip(URL url) {
  270. return getAppletContext().getAudioClip(url);
  271. }
  272. /**
  273. * Returns the <code>AudioClip</code> object specified by the
  274. * <code>URL</code> and <code>name</code> arguments.
  275. * <p>
  276. * This method always returns immediately, whether or not the audio
  277. * clip exists. When this applet attempts to play the audio clip, the
  278. * data will be loaded.
  279. *
  280. * @param url an absolute URL giving the base location of the
  281. * audio clip.
  282. * @param name the location of the audio clip, relative to the
  283. * <code>url</code> argument.
  284. * @return the audio clip at the specified URL.
  285. * @see java.applet.AudioClip
  286. */
  287. public AudioClip getAudioClip(URL url, String name) {
  288. try {
  289. return getAudioClip(new URL(url, name));
  290. } catch (MalformedURLException e) {
  291. return null;
  292. }
  293. }
  294. /**
  295. * Returns information about this applet. An applet should override
  296. * this method to return a <code>String</code> containing information
  297. * about the author, version, and copyright of the applet.
  298. * <p>
  299. * The implementation of this method provided by the
  300. * <code>Applet</code> class returns <code>null</code>.
  301. *
  302. * @return a string containing information about the author, version, and
  303. * copyright of the applet.
  304. */
  305. public String getAppletInfo() {
  306. return null;
  307. }
  308. /**
  309. * Gets the Locale for the applet, if it has been set.
  310. * If no Locale has been set, then the default Locale
  311. * is returned.
  312. *
  313. * @return the Locale for the applet
  314. * @since JDK1.1
  315. */
  316. public Locale getLocale() {
  317. Locale locale = super.getLocale();
  318. if (locale == null) {
  319. return Locale.getDefault();
  320. }
  321. return locale;
  322. }
  323. /**
  324. * Returns information about the parameters that are understood by
  325. * this applet. An applet should override this method to return an
  326. * array of <code>Strings</code> describing these parameters.
  327. * <p>
  328. * Each element of the array should be a set of three
  329. * <code>Strings</code> containing the name, the type, and a
  330. * description. For example:
  331. * <p><blockquote><pre>
  332. * String pinfo[][] = {
  333. * {"fps", "1-10", "frames per second"},
  334. * {"repeat", "boolean", "repeat image loop"},
  335. * {"imgs", "url", "images directory"}
  336. * };
  337. * </pre></blockquote>
  338. * <p>
  339. * The implementation of this method provided by the
  340. * <code>Applet</code> class returns <code>null</code>.
  341. *
  342. * @return an array describing the parameters this applet looks for.
  343. */
  344. public String[][] getParameterInfo() {
  345. return null;
  346. }
  347. /**
  348. * Plays the audio clip at the specified absolute URL. Nothing
  349. * happens if the audio clip cannot be found.
  350. *
  351. * @param url an absolute URL giving the location of the audio clip.
  352. */
  353. public void play(URL url) {
  354. AudioClip clip = getAudioClip(url);
  355. if (clip != null) {
  356. clip.play();
  357. }
  358. }
  359. /**
  360. * Plays the audio clip given the URL and a specifier that is
  361. * relative to it. Nothing happens if the audio clip cannot be found.
  362. *
  363. * @param url an absolute URL giving the base location of the
  364. * audio clip.
  365. * @param name the location of the audio clip, relative to the
  366. * <code>url</code> argument.
  367. */
  368. public void play(URL url, String name) {
  369. AudioClip clip = getAudioClip(url, name);
  370. if (clip != null) {
  371. clip.play();
  372. }
  373. }
  374. /**
  375. * Called by the browser or applet viewer to inform
  376. * this applet that it has been loaded into the system. It is always
  377. * called before the first time that the <code>start</code> method is
  378. * called.
  379. * <p>
  380. * A subclass of <code>Applet</code> should override this method if
  381. * it has initialization to perform. For example, an applet with
  382. * threads would use the <code>init</code> method to create the
  383. * threads and the <code>destroy</code> method to kill them.
  384. * <p>
  385. * The implementation of this method provided by the
  386. * <code>Applet</code> class does nothing.
  387. *
  388. * @see java.applet.Applet#destroy()
  389. * @see java.applet.Applet#start()
  390. * @see java.applet.Applet#stop()
  391. */
  392. public void init() {
  393. }
  394. /**
  395. * Called by the browser or applet viewer to inform
  396. * this applet that it should start its execution. It is called after
  397. * the <code>init</code> method and each time the applet is revisited
  398. * in a Web page.
  399. * <p>
  400. * A subclass of <code>Applet</code> should override this method if
  401. * it has any operation that it wants to perform each time the Web
  402. * page containing it is visited. For example, an applet with
  403. * animation might want to use the <code>start</code> method to
  404. * resume animation, and the <code>stop</code> method to suspend the
  405. * animation.
  406. * <p>
  407. * Note: some methods, such as <code>getLocationOnScreen</code>, can only
  408. * provide meaningful results if the applet is showing. Because
  409. * <code>isShowing</code> returns <code>false</code> when the applet's
  410. * <code>start</code> is first called, methods requiring
  411. * <code>isShowing</code> to return <code>true</code> should be called from
  412. * a <code>ComponentListener</code>.
  413. * <p>
  414. * The implementation of this method provided by the
  415. * <code>Applet</code> class does nothing.
  416. *
  417. * @see java.applet.Applet#destroy()
  418. * @see java.applet.Applet#init()
  419. * @see java.applet.Applet#stop()
  420. * @see java.awt.Component#isShowing()
  421. * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
  422. */
  423. public void start() {
  424. }
  425. /**
  426. * Called by the browser or applet viewer to inform
  427. * this applet that it should stop its execution. It is called when
  428. * the Web page that contains this applet has been replaced by
  429. * another page, and also just before the applet is to be destroyed.
  430. * <p>
  431. * A subclass of <code>Applet</code> should override this method if
  432. * it has any operation that it wants to perform each time the Web
  433. * page containing it is no longer visible. For example, an applet
  434. * with animation might want to use the <code>start</code> method to
  435. * resume animation, and the <code>stop</code> method to suspend the
  436. * animation.
  437. * <p>
  438. * The implementation of this method provided by the
  439. * <code>Applet</code> class does nothing.
  440. *
  441. * @see java.applet.Applet#destroy()
  442. * @see java.applet.Applet#init()
  443. */
  444. public void stop() {
  445. }
  446. /**
  447. * Called by the browser or applet viewer to inform
  448. * this applet that it is being reclaimed and that it should destroy
  449. * any resources that it has allocated. The <code>stop</code> method
  450. * will always be called before <code>destroy</code>.
  451. * <p>
  452. * A subclass of <code>Applet</code> should override this method if
  453. * it has any operation that it wants to perform before it is
  454. * destroyed. For example, an applet with threads would use the
  455. * <code>init</code> method to create the threads and the
  456. * <code>destroy</code> method to kill them.
  457. * <p>
  458. * The implementation of this method provided by the
  459. * <code>Applet</code> class does nothing.
  460. *
  461. * @see java.applet.Applet#init()
  462. * @see java.applet.Applet#start()
  463. * @see java.applet.Applet#stop()
  464. */
  465. public void destroy() {
  466. }
  467. //
  468. // Accessibility support
  469. //
  470. AccessibleContext accessibleContext = null;
  471. /**
  472. * Gets the AccessibleContext associated with this Applet.
  473. * For applets, the AccessibleContext takes the form of an
  474. * AccessibleApplet.
  475. * A new AccessibleApplet instance is created if necessary.
  476. *
  477. * @return an AccessibleApplet that serves as the
  478. * AccessibleContext of this Applet
  479. */
  480. public AccessibleContext getAccessibleContext() {
  481. if (accessibleContext == null) {
  482. accessibleContext = new AccessibleApplet();
  483. }
  484. return accessibleContext;
  485. }
  486. /**
  487. * This class implements accessibility support for the
  488. * <code>Applet</code> class. It provides an implementation of the
  489. * Java Accessibility API appropriate to applet user-interface elements.
  490. */
  491. protected class AccessibleApplet extends AccessibleAWTPanel {
  492. private static final long serialVersionUID = 8127374778187708896L;
  493. /**
  494. * Get the role of this object.
  495. *
  496. * @return an instance of AccessibleRole describing the role of the
  497. * object
  498. */
  499. public AccessibleRole getAccessibleRole() {
  500. return AccessibleRole.FRAME;
  501. }
  502. /**
  503. * Get the state of this object.
  504. *
  505. * @return an instance of AccessibleStateSet containing the current
  506. * state set of the object
  507. * @see AccessibleState
  508. */
  509. public AccessibleStateSet getAccessibleStateSet() {
  510. AccessibleStateSet states = super.getAccessibleStateSet();
  511. states.add(AccessibleState.ACTIVE);
  512. return states;
  513. }
  514. }
  515. }