!-- Global site tag (gtag.js) - Google Analytics --> Java Applet Life Cycle - programmingworld

Breaking

Java Applet Life Cycle


                                 Applet Life Cycle 

Every applet inherits a set of behaviors from the Applet class. As a result, when an applet is loaded, it undergoes the series of changes its state as shown in the figure. The applet life cycle states are below:-

  • initialization state/Born state
  • Running or active state
  • Idle or waiting state 
  • Dead or Destroyed state

 Initialization State     

  In Applet initialization state is those state when the object is created. Applet has gone the initialization state when its first loaded.

  • Create objects needed by the applet 
  • Set up the initial value 
  • Load images or fonts
  • Set up color
 To provide any of the behaviors above, we must override the init() method:

public void init() 
{
...............
...............(Action)
}

Running State 

When Applet enters the running state when the system call the start () method of Applet class. In the running state, the process is ready to start. This occurs automatically after the applet initialized. Starting can also occur when the applet is already in "stopped" state. This again starts the applet running. We may override the start() method for creating a thread to control the applet.

public void start() 
{
.............
............
............ (Action and statements)
}

Idle or Stopped State

An applet becomes idle when it is stopped from running. Stopping automatically when we leave the page containing the currently running applet. We can also do by calling the stop() method externally. If we use a thread to run an applet, then we must be use stop() method to terminate the thread.


public void stop()
{
.............
............. (Action)
.............
}

Dead State 

An applet is said to be destroyed when it is removed from memory. When a state totally destroys it is called dead state. This occurs automatically when the destroy() method is call and when we quit the browser.  initialization and destroying stage occurs only once in the applet's life cycle. If the applet has created threads, we override the destroy() method to clean up these threads. 

public void destroy()
{
................
................ (Action)
................
}

Display State

Applet moves to the display state whenever it has to perform all above operations and some output operations on the screen. We must, therefore, override this method if we want to displayed anything on the screen.

public void paint(Graphics g)
{
............
............ (Display statements)
.............
}

 Infect, the paint() method is defined in the applet class. It is inherited from the component class, a superclass of a applet.

  Methods of Applet Life Cycle

There are basically five methods for Applet life cycle.

  • init()
  • start()
  • paint()
  • stop()
  • destroy() 
init():- init() method is used for creating a applet object in java. When init() method is defined than this state is called born state.
It is call all method like start(), paint() etc. It is usually declared and initialize variables. 

start():- After creating a born state by the init() method. We have need to start this method. And we start is by using start() method. By using the start() method we have gone born state to active state.

paint():- For calling the paint() method we have to need to define his predefine method java.awt.Graphics;. By using the paint() method we move to running state to runnable state.

stop():- stop() method is used to move the process into waiting state/blocked state.

destroy():- destroy() method is used to destroy an applet. When an applet is destroyed this state called dead state.