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