6.1 Applet Life Cycle

The life cycle of an applet is determined by methods that are automatically called at its birth, its death and when it is momentarily away. We discuss below the details. To illustrate the life cycle calls, here is an applet that displays all calls in both a text area and the java console.

  1. The Begin
  2. Away for a Moment
  3. Back Again
  4. The End
  5. A Minimal Appletviewer

The Begin

An applet extends a panel, so it "is" a component of the AWT. But how come it runs as a program?

  1. The browser starts a Java Virtual Machine (JVM) and gives it control over the part of the screen that is specified width and height of the applet.
  2. This JVM will do all things that belong to any java application, like starting the AWT-Input handler and the garbage collector. (See User Threads).
  3. Next it will create an instance of the applet as specified in the class file of the applet.
  4. The JVM sees the window of the browser as a kind of a Java AWT frame, and adds the applet as a panel to this frame.
  5. As the main routine is in fact already running, there is an other method, init, for the initialization of the applet. It is called automatically by the JVM.
  6. After the call to init, a call to start is done and then a call to the paint routine.

Away for a Moment

Once a browser has loaded an applet, this applet will stay running. Also when the applet is not visible, it is still running in the background. This is even the case when you have clicked to other URLs. Only when you quit the browser, the applet will exit as well.

The stop method is exactly meant to keep in control of the applet at these moments where the applet becomes not visible. By implementing this method, you can stop computations or "threads" like animations. In general you should stop parts of the programs that consume resources.

Back Again

Of course, once you can stop things, you should be able to restart them as well. For this purpose, there is the start method. This method is also automatically called at the beginning, just after init. When you ask the browser to reload a page containing the applet, it will call stop, immediately followed by start.

The End

Just before exiting, so when the browser quits, the applet performs a call to the destroy method. Here some clean-up code can be inserted, however this is in practice seldomly necessary. Please note the difference with the stop method.

A Minimal Appletviewer

As we now have all ingredients that make up an applet, we can mimick the browser with a simple Java application. The JDK appletviewer offers more functionality, but essentially does the same. So all we have to do is to make a frame, and then perform the necessary calls to init, start, stop and destroy. Here is the essential part of the code.

class AppletFramer extends Frame {

  Applet applet;

  AppletFramer (String appletname, int width, int height) {
    setLayout(new BorderLayout());
    try {  // get the class from its name and instantiate it
      applet = (Applet) Class.forName(appletname).newInstance();
    } catch (Exception e) {
      System.out.println(e);
    }
    resize(width, height);
    add("Center", applet); // the applet is a panel
    applet.init();
    applet.start();
    show();
  }

  public boolean handleEvent (Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) {
      applet.destroy();
      dispose();
      System.exit(0);
    } else if (evt.id == Event.WINDOW_ICONIFY) {
      applet.stop();
    } else if (evt.id == Event.WINDOW_DEICONIFY) {
      applet.start();
    }
    return super.handleEvent(evt);
  }
}