img
An Applet Skeleton
All but the most trivial applets override a set of methods that provides the basic
mechanism by which the browser or applet viewer interfaces to the applet and controls
its execution. Four of these methods, init( ), start( ), stop( ), and destroy( ), apply to all
applets and are defined by Applet. Default implementations for all of these methods are
provided. Applets do not need to override those methods they do not use. However, only
very simple applets will not need to define all of them.
AWT-based applets (such as those discussed in this chapter) will also override the
paint( ) method, which is defined by the AWT Component class. This method is called
when the applet's output must be redisplayed. (Swing-based applets use a different
mechanism to accomplish this task.) These five methods can be assembled into the
skeleton shown here:
// An Applet skeleton.
import java.awt.*;
import java.applet.*;
/*
<applet code="AppletSkel" width=300 height=100>
</applet>
*/
public class AppletSkel extends Applet {
// Called first.
public void init() {
// initialization
}
/* Called second, after init().
Also called whenever
the applet is restarted. */
public void start() {
// start or resume execution
}
// Called when the applet is stopped.
public void stop() {
// suspends execution
}
/* Called when applet is terminated.
This is the last
method executed. */
public void destroy() {
// perform shutdown activities
}
// Called when an applet's window must be restored.
public void paint(Graphics g) {
// redisplay contents of window
}
}
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home