|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Thepublic class Simple extends Applet { . . . public void init() { . . . } public void start() { . . . } public void stop() { . . . } public void destroy() { . . . } . . . }Simpleapplet, like every other applet, features a subclass of theAppletclass. TheSimpleclass overrides fourAppletmethods so that it can respond to major events:
init- To initialize the applet each time it's loaded (or reloaded).
start- To start the applet's execution, such as when the applet's loaded or when the user revisits a page that contains the applet.
stop- To stop the applet's execution, such as when the user leaves the applet's page or quits the browser.
destroy- To perform a final cleanup in preparation for unloading.
Not every applet needs to override every one of these methods. Some very simple applets override none of them. For example, the Writing Applets
doesn't override any of these methods, since it doesn't do anything except draw itself. The "Hello World" applet just displays a string once, using its
paintmethod. (Thepaintmethod is described on the following page.) Most applets, however, do more.The
initmethod is useful for one-time initialization that doesn't take very long. In general, theinitmethod should contain the code that you would normally put into a constructor. The reason applets shouldn't usually have constructors is that an applet isn't guaranteed to have a full environment until itsinitmethod is called. For example, theAppletimage loading methods simply don't work inside of a applet constructor. Theinitmethod, on the other hand, is a great place to call the image loading methods, since the methods return quickly.
Browser note: Some browsers sometimes call theinitmethod more than once after the applet has been loaded. See the previous page for more details.Every applet that does something after initialization (except in direct response to user actions) must override the
startmethod. Thestartmethod either performs the applet's work or (more likely) starts up one or more threads to perform the work. You'll learn more about threads later in this trail, in Threads in Applets. You'll learn more about handling the events that represent user actions on the next page.
Most applets that override
startshould also override thestopmethod. Thestopmethod should suspend the applet's execution, so that it doesn't take up system resources when the user isn't viewing the applet's page. For example, an applet that displays animation should stop trying to draw the animation when the user isn't looking at it.Many applets don't need to override the
destroymethod, since theirstopmethod (which is called beforedestroy) does everything necessary to shut down the applet's execution. However,destroyis available for applets that need to release additional resources.The
init,start,stop, anddestroymethods are discussed and used throughout this tutorial. For more information, you can also refer to the Applet API reference page.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.