Applets
Introduction
Inheritance
The inheritance hierarchy for a Swing applet
java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet
Four methods of the Applet class
Method | Description |
---|---|
public void init() | Called when the browser first loads the applet. |
public void start() | Called after the init method and every time the user moves to the web page for the applet. |
public void stop() | Called before the destroy method and every time the user moves to another web page. |
public void destroy() | Called when the user exits the browser. |
The init() Method
Invoked when the applet is first loaded and again if the applet is reloaded.
A subclass of Applet should override this method if the subclass has an initialization to perform. The functions usually implemented in this method include creating new threads, loading images, setting up user-interface components, and getting string parameter values from the <applet> tag in the HTML page.
The start() Method
Invoked after the init() method is executed; also called whenever the applet becomes active again after a period of inactivity (for example, when the user returns to the page containing the applet after surfing other Web pages).
A subclass of Applet overrides this method if it has any operation that needs to be performed whenever the Web page containing the applet is visited. An applet with animation, for example, might use the start method to resume animation.
The stop() Method
The opposite of the start() method, which is called when the user moves back to the page containing the applet; the stop() method is invoked when the user moves off the page.
A subclass of Applet overrides this method if it has any operation that needs to be performed each time the Web page containing the applet is no longer visible. When the user leaves the page, any threads the applet has started but not completed will continue to run. You should override the stop method to suspend the running threads so that the applet does not take up system resources when it is inactive.
The destroy() Method
Invoked when the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated.
A subclass of Applet overrides this method if it has any operation that needs to be performed before it is destroyed. Usually, you won't need to override this method unless you wish to release specific resources, such as threads that the applet created.
The inheritance hierarchy and methods of an applet
- To create a Swing applet, you define a class that extends the JApplet class.
- Then, you can override the init, start, stop, and destroy methods of the Applet class as needed.
- These methods are called automatically by the browser, and they control the execution of the applet.