Applet Life Cycle

The life cycle of an applet is as shown in the figure below:





  • As shown in the above diagram, the life cycle of an applet starts with init() method and ends with destroy() method.
  • Other life cycle methods are start(), stop() and paint()
  • The methods to execute only once in the applet life cycle are init() and destroy()
  • Other methods execute multiple times.


Below is the description of each applet life cycle method:

init(): 
  • The init() method is the first method to execute when the applet is executed. 
  • Variable declaration and initialization operations are performed in this method.

start(): 
  • The start() method contains the actual code of the applet that should run. 
  • The start() method executes immediately after the init() method.
  • It also executes whenever the applet is restored, maximized or moving from one tab to another tab in the browser.


stop(): 
  • The stop() method stops the execution of the applet. 
  • The stop() method executes when the applet is minimized or when moving from one tab to another in the browser.


destroy(): 
  • The destroy() method executes when the applet window is closed or when the tab containing the webpage is closed. 
  • stop() method executes just before when destroy() method is invoked. 
  • The destroy() method removes the applet object from memory.


paint(): 
  • The paint() method is used to redraw the output on the applet display area. 
  • The paint() method executes after the execution of start() method and whenever the applet or browser is resized.


The method execution sequence when an applet is executed is:
  • init()
  • start()
  • paint()

The method execution sequence when an applet is closed is:
  • stop()
  • destroy()

Example program that demonstrates the life cycle of an applet is as follows:


Output of the above applet program when run using appletviewer tool is:

Applet initialized
Applet execution started
Painting…
Painting…
Applet execution stopped
Applet destroyed

Comments

Popular posts from this blog

PHP Array Functions

String Functions in C Language(C Language)

Object Instance Working with Strings