Life Cycle of an Applet


Life Cycle of an Applet
Life Cycle of an Applet 



Every java applet inherits a set of defaults from Applet class defined in java.applet package. When an applet is loaded ,it undergoes a series of chnages in its states . 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. When an applet begins, the following methods are called, in this sequence:
 1. init( ) 
2. start( ) 
3. paint( ) 
When an applet is terminated, the following sequence of method calls takes place:
1. stop( ) 
2. destroy( )


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. Syntax:   public void init() { --- }
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( ).

 Syntax:   public void start() { -----}

paint( ): The paint( ) method is called each time your applet’s output must be redrawn. 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. 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.

Syntax:   public void stop() {--------} 


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.

Syntax:  public void stop() {--------} 

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( ).

Syntax  :   public void destroy()   {------} 


Note : 1. All the methods are automatically called when any applet begins execution. 2. In every applet it doesn’t need to place all the methods. 3. Class hierarchy of the applet is java.lang.Object java.awt .Container java .awt.Panel java.applt.Applet


Simple Applet Display Methods: drawString( ): to output a string to an applet, which is a member of the Graphics class. Typically, it is called from within either update( ) or paint( ).

Syntax: void drawString(String message, int x, int y)   
Here, message is the string to be output beginning at x,y.

void setBackground(Color newColor)
void setForeground(Color newColor) newColor: Color.black Color.magenta Color.gray Color.white Color.blue Color.orange Color.cyan Color.pink Color.darkGray Color.red Color.green Color.yellow Color.lightGray



Color getBackground( ) 
Color getForeground( )

void repaint( ) :This version causes the entire window to be repainted.
void repaint(int left, int top, int width, int height): Here, the coordinates of the upper-left corner of the region are specified by left and top, and the width and height of the region are passed in width and height. These dimensions are specified in pixels.