Applet life cycle: – Every java applet inherits a set of default behavior from the applet class. As a result, when an applet is loaded, it undergoes series of changes in its state and is called applet life cycle. The applet life cycle includes –

  1. Born or initialization state
  2. Running state
  3. IDLE or stop state
  4. Dead or destroyed state

Initialization state: – Applet enters the initialization state when it is first loaded. This is a achieved by the calling init() method of applet class. The applet is born. At this state, we may do the following, it required.

  1. Create object needed by applet
  2. Set up initial values
  3. Load images or font
  4. Set of colors

The initialization occurs only once in the applet life cycle. To provide any of the behavior mentioned above, we must override the init() method. Syntax of the init() method is –

public void init()
{
                Body of init() method
}

Running state: – Applet enters a running state when the system calls the start() method of applet class. This occurs automatically after the applet is initialized. Starting can also occurs if the applet is already in stopped state. For example, we may leave the web page containing the applet temporarily to another page and return back to the page. This again start the applet running. Note that, unlike init() method, the start() method may be called more than once. We may override the start() method to create a thread to control the applet. General format of the start() method is –

public void start()
{
                Body of start() method
}

Idle or stop statement: – An applet becomes IDLE when it is stopped from running. Stopping occurs automatically when we live the page containing the currently running applet. We can also do so by calling the stop() method explicitly. If we use a thread to run the applet, we must use stop() method to terminate the thread. We can achieve this by overriding the stop() method. Syntax of the stop method is –

public void stop()
{
                Body of stop() method
}

Dead or destroyed state: – An applet is said to be dead when it is removed from memory this occurs automatically by invoking the destroy() method when we quit the browser. Like initialization, destroying state occurs only once in the applet life cycle. If the applet has created any resources like thread, we may override the destroy() method to clean up these resource. Syntax of the destroy method is –

public void destroy()
{
                Body of destroy() method
}
Advertisements