    Contents    Previous    Next

#### Writing an Applet

# Writing a Subclass of Applet

To write an applet, you need to implement a subclass of the Applet class. This page describes the Applet methods that your subclass might need to implement. To see how other programmers have implemented Applet subclasses, go to the code examples.

Three methods perform much of an applet's work:

init()
  Called just after the applet is created, this method is useful for resizing the applet's display area, downloading resources, getting attribute values, getting fonts and colors, and so on.
start()
  This method typically performs (or starts a thread to perform) the real work of the applet. By default, this method is called every time the applet's page is visited, whether or not the applet is visible. However, the user can specify that applet execution be delayed until the user requests it.
paint()
  Called automatically every time the applet's screen display needs to be drawn -- such as when the applet first becomes visible, when it's scrolled, or when the applet requests to be repainted -- this method should simply draw the applet's representation onscreen.

Two more methods clean up after the applet:

stop()
  Called when the user leaves the applet's page, this method should stop the applet's execution.
destroy()
  Called when the applet is discarded (such as when the applet is reloaded, which requires it to be unloaded first), this method should release the applet's resources.

Your Applet subclass can use many Applet utility methods to:

- use certain resources (such as fonts and colors)
- handle events (such as mouse clicks)
- perform common operations (such as play a sound or display an image)

See the Applet class documentation for information on all Applet methods and variables. See the code examples for examples of creating Applet subclasses.

    Contents    Previous    Next