Life Cycle of applet

Introduction: Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining.

Advantages of Applet:

  • Applets are cross platform and can run on Windows, Mac OS and Linux platform
  • Applets can work all the version of Java Plugin
  • Applets runs in a sandbox, so the user does not need to trust the code, so it can work without security approval
  • Applets are supported by most web browsers
  • Applets are cached in most web browsers, so will be quick to load when returning to a web page User can also have full access to the machine if user allows


Disadvantages of Java Applet:

  • Java plug-in is required to run applet
  • Java applet requires JVM so first time it takes significant startup time
  • If applet is not already cached in the machine, it will be downloaded from internet and will take time
  • Its difficult to desing and build good user interface in applets compared to HTML technology All these methods are optional
Life cycle of applet is governed by browser. There are 5 methods in the life cycle of an applet All these methods are optional

init( ) : Called when the applet is first created, to perform first-time
initialization of the applet
start( ): Called every time the applet moves into sight on the Web browser, to allow the applet to start up its normal operations (especially those that are shut off by stop( )). Also called after init( ).
paint( ) : Part of the base class Component (three levels of inheritance up). Called as part of an update( ) to perform special painting on the canvas of an applet.
stop( ) : Called every time the applet moves out of sight on the Web browser, to allow the applet to shut off expensive operations. Also called right before destroy( ).
destroy( ):
Called when the applet is being unloaded from the page, to perform final release of resources when the applet is no longer used

Detailed Explanation:
init () method: The life cycle of an applet is begin on that time when the applet is first loaded into the browser and called the init() method. The init() method is called only one time in the life cycle on an applet. The init() method is basically called to read the PARAM tag in the html file. The init () method retrieve the passed parameter through the PARAM tag of html file using get Parameter() method All the initialization such as initialization of variables and the objects like image, sound file are loaded in the init () method .After the initialization of the init() method user can interact with the Applet and mostly applet contains the init() method.

Start () method: The start method of an applet is called after the initialization method init(). This method may be called multiples time when the Applet needs to be started or restarted. For Example if the user wants to return to the Applet, in this situation the start Method() of an Applet will be called by the web browser and the user will be back on the applet. In the start method user can interact within the applet.

Stop () method: The stop() method can be called multiple times in the life cycle of applet like the start () method. Or should be called at least one time. There is only miner difference between the start() method and stop () method. For example the stop() method is called by the web browser on that time When the user leaves one applet to go another applet and the start() method is called on that time when the user wants to go back into the first program or Applet.

destroy() method: The destroy() method is called only one time in the life cycle of Applet like init() method. This method is called only on that time when the browser needs to Shut down.

Run this applet and view the messages in the console window.

Example1:

import java.awt.*;
import java.applet.Applet;
public class LifeCycle extends Applet
{
public void init()
{
showStatus("This is Init");
for(int i=0;i<100000000;i++);
}
public void start()
{
showStatus("This is Start");
for(int i=0;i<100000000;i++);
}
public void paint(Graphics g)
{
showStatus("This is Paint");
for(int i=0;i<1000000;i++);
}
public void stop()
{
showStatus("This is Stop");
for(int i=0;i<1000000;i++);
}
public void destroy()
{
showStatus("This is Destroy");
for(int i=0;i<1000000;i++);
}
}

/*

*/

Example: 2
import java.applet.Applet;
import java.awt.Graphics;

//No need to extend JApplet, since we don't add any components;
//we just paint.
public class Simple extends Applet {

StringBuffer buffer;

public void init() {
buffer = new StringBuffer();
addItem("initializing... ");
}

public void start() {
addItem("starting... ");
}

public void stop() {
addItem("stopping... ");
}

public void destroy() {
addItem("preparing for unloading...");
}

private void addItem(String newWord) {
System.out.println(newWord);
buffer.append(newWord);
repaint();
}

public void paint(Graphics g) {
//Draw a Rectangle around the applet's display area.
g.drawRect(0, 0,
getWidth() - 1,
getHeight() - 1);

//Draw the current string inside the rectangle.
g.drawString(buffer.toString(), 5, 15);
}
}

1 comment:

Unknown said...

These programs can't run properly.

Google Search