Home | | Web Programming | An Applet Skeleton

Chapter: Java The Complete Reference : The Java Library : The Applet Class

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.

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 often 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

 

}

 

}

 

Although this skeleton does not do anything, it can be compiled and run. When run, it generates the following empty window when viewed with appletviewer. Of course, in this and all subsequent examples, the precise look of the appletviewer frame may differ based on your execution environment. To help illustrate this fact, a variety of environments were used to generate the screen captures shown throughout this book.


Applet Initialization and Termination

 

It is important to understand the order in which the various methods shown in the skeleton are called. When an applet begins, the following methods are called, in this sequence:

 

         init( )

 

         start( )

 

         paint( )

 

When an applet is terminated, the following sequence of method calls takes place:

 

         stop( )

 

         destroy( )

 

Let’s look more closely at these methods.

init( )

 

The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet.

 

start( )

 

The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Whereas init( ) is called once—the first time an applet is loaded—start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).

 

paint( )

 

The paint( ) method is called each time an AWT-based applet’s output must be redrawn. This situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored. paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required.

 

stop( )

 

The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page, for example. When stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.

 

destroy( )

The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).

Overriding update( )

 

In some situations, an AWT-based applet may need to override another method defined by the AWT, called update( ). This method is called when your applet has requested that a portion of its window be redrawn. The default version of update( ) simply calls paint( ).

 

However, you can override the update( ) method so that it performs more subtle repainting. In general, overriding update( ) is a specialized technique that is not applicable to all applets, and the examples in this chapter do not override update( ).

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : The Applet Class : An Applet Skeleton |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.