Download the JDK
Search the Tutorials
Hide the TOC
Trail: Deployment
Lesson: Applets
Section: Getting Started with Applets
Applets
Getting Started with Applets
Importing Classes and Packages for Applets
Defining an Applet Subclass
The Life Cycle of an Applet
Methods for Milestones
Methods for Drawing and Event Handling
Methods for Adding UI Components
What Applets Can and Can't Do
Taking Advantage of the Applet API
Finding and Loading Data Files
Displaying Short Status Strings
Displaying Documents in the Browser
Sending Messages to Other Applets
Playing Sounds
Defining and Using Applet Parameters
Deciding Which Parameters to Support
Writing the Code to Support Parameters
Giving Information about Parameters
Using the APPLET Tag
Practical Considerations When Writing Applets
Security Restrictions
Creating a User Interface
Creating a GUI
Displaying Diagnostics to the Standard Output and Error Streams
Getting System Properties
Threads in Applets
Threads in Applets: Examples
Working with a Server-Side Application
A Simple Network Client Applet
Using a Server to Work Around Security Restrictions
Finishing an Applet
Before You Ship that Applet
The Perfectly Finished Applet
Deploying Applets
General Deployment Considerations
Using the applet Tag
Using the object Tag
Using the embed Tag
Deploying Applets in a Mixed-Browser Environment
Solving Common Applet Problems
Questions and Exercises
Home Page > Deployment > Applets
« Previous • Trail • Next »
Methods for Milestones

> The `Simple` applet, like every other applet, features a subclass of the `Applet` class. The `Simple` class overrides four `Applet` methods 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 is 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.

> Following is the interface for these methods:

> >     public class Simple extends JApplet {
> >         . . .
> >         public void init() { . . . }
> >         public void start() { . . . }
> >         public void stop() { . . . }
> >         public void destroy() { . . . }
> >         . . .
> >     }

> The `init`, `start`, `stop`, and `destroy` methods are discussed and used throughout this tutorial. For more information, you can also refer to the `JApplet` API Specification.

### Overriding These Methods

> Not every applet needs to override every one of these methods. Some very simple applets override none of them. For example, the HelloWorld applet at the beginning of this section 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 `paint` method. (The `paint` method is described on the following page.) Most applets, however, do more.

### The init Method

> The `init` method is useful for one-time initialization that doesn't take very long. In general, the `init` method 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 its `init` method is called. For example, the `Applet` image loading methods simply don't work inside of an applet constructor. The `init` method, 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 the `init` method more than once after the applet has been loaded. See the previous page for more details.

### The start Method

> Every applet that does something after initialization (except in direct response to user actions) must override the `start` method. The `start` method 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 the Threads in Applets section. You'll learn more about handling the events that represent user actions on the next page.

### The stop Method

> Most applets that override `start` should also override the `stop` method. The `stop` method 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.

### The destroy Method

> Many applets don't need to override the `destroy` method, since their `stop` method (which is called before `destroy`) does everything necessary to shut down the applet's execution. However, `destroy` is available for applets that need to release additional resources.

> > Note: You should keep implementations of the `destroy` method as short as possible, because there is no guarantee that this method will be completely executed. The Java Virtual Machine might exit before a long destroy method has completed.

« Previous • Trail • Next »
Previous page: The Life Cycle of an Applet
Next page: Methods for Drawing and Event Handling