Wednesday, 30 November 2011

Applet Skeleton And Lifecycle


Applet skeleton includes different methods for writing applet.

import java.awt.*;
import java.applet.*;

/*
<applet code="AppletSkel" width=300 height=100>
</applet>
*/

public class AppletSkel extends Applet
{
// Called first.

public void init()
{
// initialization
}

/* Called second, after init(). Also called whenever the applet is restarted. */

public void start()
{
// start or resume execution
}

// Called when the applet is stopped.
public void stop()
{
// suspends execution
}

/* Called when applet is terminated. This is the last method executed. */

public void destroy()
{
// perform shutdown activities
}

// Called when an applet's window must be restored.
public void paint(Graphics g)
{
// redisplay contents of window
}
}
THE
When an applet begins, the AWT calls the following methods, 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( )

Note : Above all methods are explained in Applet Life cycle

Applet Life cycle


                  


When Applet is created it under goes series of changes in its state. The applet state include.

1.     Born or initialize state
2.     Running state
3.     Idele state
4.     Dead or destroyed state
                                                                     
Explanation about above is as follows:

Initialization state or init()

Applet enters the initialization state when it is first loaded. This is achieved by overriding  init() method.
    
          public void 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 applet.
·         With init() method following task may do.

Create objects needed by applet.
Set up initial values.
Load images or fonts.
Set up colors.

Running state or start()

An applet enters in running  state when system calls the start().This is achieved by overriding  start() method.

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

Idle or Stopped state or stop()

An applet become idle when it is stopped from running. This is achieved by overriding  stop() method.
                  
                      public void stop()
                      {
           

                      }

·    The 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. stop( ) method is also used to suspend threads that don’t need to run.
·      when the applet is not visible. It can be restarted when start( ) is called if the user returns to the page.

Dead state or destroy()

An applet is said to be dead when it is removed from memory. This is achieved by overriding destroy() method.

public void destroy()
{

}

·         The destroy( ) method is called when the environment determines that  applet
·         needs to be removed completely from memory.
·         this point, you should free up any resources the applet may be using.
·         stop( ) method is always called before destroy( ).
·         Like initialization , destroying stage occurs only once in the applet’s life cycle.

Display state or paint()

Applet moves to the display state whenever it has to perform some output operations on the screen.
This happen immediately after the applet enters into the running state. The paint method is called to accomplish this task.
This is achieved by overriding destroy() method.

public void paint(Graphics g)
{

}

Example

import java.applet.*;
import java.awt.*;

// public void drawString(String message,int x,int y)

/*
  <applet code="AppletLifecycle" width=300 height=300>
  </applet>
*/
public class AppletLifecycle extends Applet
{
  String str = "";

  public void init()
  {
     str += "init; ";
  }

 public void start()
  {
    str += "start; ";
  }

 public void stop()
  {
    // stop
  }

 public void destroy()
  {
   //destroy
  }

  public void paint(Graphics g)
   {
       str += "Paint; ";
       g.drawString(str, 10, 25);
     
 
   }
}

Save source file with name : AppletLifecycle.java
Compile source file             : javac AppletLifecycle.java
Open file in appletviewer     : appletviewer AppletLifecycle.java


Posted by : Ruchita Pandya

7 comments:

  1. Replies
    1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

      Delete
  2. Really useful material. Thanks!

    ReplyDelete
  3. written in a very well organised manner !

    ReplyDelete
  4. thanks a lot...its gave me more knowledge about applet..

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete