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