Java Applet Life Cycle : An Overview

applet life cycleJava is used in web-based applications like developing games or large-scale management systems. Java also has the power to create applications that run in web browsers. An applet is a small Java program that runs in your web browser. Applets are Java applications that have the entire Java API at its disposal. Basically, an Applet is a subclass of java.applet.Applet package, but it’s different from a native Java application. It doesn’t consist of a main method unlike a Java application. Embedded in an HTML page, an applet is downloaded on to the client machine when the HTML page.

Learn more about Java programming by taking a course at Udemy.com

Defining an Applet

import java.applet.*; // Importing the two applet classes

import java.awt.*;

public class HelloApplet extends Applet //Extends Applet class

{

public void paint (Graphics g)

{

g.drawString ("Hello World", 25, 50);

}

}

Embedding an Applet in HTML

The Applet cannot be opened directly in the browser and therefore, it should be included in the HTML page for execution by the browser.

<html>

<title>Hello World, My First Applet</title>

<applet code="HelloApplet.class" width=“420" height=“220">

If java is enabled in the browser, HelloWorld will be visible.

</applet>

</html>

The applet tag is required to run an applet. The height and width is specified. These properties define the size of the applet panel such as the amount of space the applet would cover on the page. The code specifies the location path and the name of the applet class file. The class file is generated when the Java code is compiled. The browser does not have power to compile the applet file, it only executes an already compiled applet program. The text between the applet tags defines the applet functionality and it is generally used for developing purposes, however, it is ignored by the browser.

Applet Life Cycle

There are four java.Applet class methods that define the applet life cycle.

They are:

  • public void init(): This method initializes the Applet and is invoked only once in the Applet life cycle. It helps to initialize variables and instantiate the objects and load the GUI of the applet. This is invoked when the page containing the applet is loaded.
  • public void start(): This method is invoked after the init() method. It starts the execution of Applet. In this state, the applet becomes active.
  • public void stop(): It stops the Applet execution. It’s invoked when the Applet stops or when the browser is minimized. This makes the Applet temporarily inactive. The Applet frequently comes to this state in its life cycle and can go back to its start state.
  • public void destroy(): This destroys the Applet and is also invoked only once when the active browser page containing the applet is closed.
  • public void paint() : This method helps to create Applet’s GUI such as  a colored background, drawing and writing. It is a method of java.awt.Graphics package.

Learn the fundamentals of Java by taking a tutorial at Udemy.com

Syntax:         

import java.applet.Applet; // Importing Applet package

import java.awt.Graphics; // Importing Graphics package For GUI

class AppletLifecycle extends Applet

{
// This method loads the applet and is only called only called once in the applet life cycle

public void init()

{

}

//Applet execution starts from this method.

public void start() {

}

// This method stops or pauses the execution

public void stop() {

}

//  This method is executed  only once in the life cycle and terminates applet execution

public void destroy() {

}

// This method is used to paint the design of the applet

public void paint(Graphics g)

{

}

}

Example

import java.applet.Applet;

import java.awt.Graphics;

import java.awt.*; 

class MyfirstApplet extends Applet

{

String str = "";

public void init() // init method

{

str = str + "I am Init ";

}

public void start() // start method

{

str = str + "I am start : ";

}

public void stop() // stop method and holds the applet on pause state

 {

str = str + "I am stop : ";

}

public void destroy() // destroy method, destroys the applet

{

System.out.println("Applet Destroyed.");

}

public void paint(Graphics g) // GUI design of the applet

{

g.drawString(str,500,700);

}

}

The above program demonstrates a simple applet life cycle. It imports the Applet and Graphics class, which is a very crucial part of every applet program. The Graphics class is used to design the applet and the Applet class includes all the important methods of an applet life cycle. The “init” method is called only once when the applet is loaded.  The start method is invoked every time an applet runs or when an applet resumes from its state. The stop method is invoked when the user browser is not in the active window or when a user jumps to the new tab of the browser. The destroy function is called only once when the browser page running the applet is closed or browser is closed.

The paint method is called after the execution of the start method execution. In addition, it is also called every time the applet needs to repaint something. This method is inherited from java.awt. Thus, the string will be printed on the screen when the starts function executes.

Applet Example to calculate sum of two numbers:

import java.awt.*;      //Importing the classes

import java.applet.*;

public class Sum_Demoextends Applet

{

TextField T3,T4;

public void init() // Initializing the import elements of applet

{

T3 = new TextField(11); //adding a text field

T4 = new TextField(11);

add(T3);

add(T4);

T3.setText("0");

T4.setText("0");

}

public void paint(Graphics g) // called after init method

{

int num1, num2, sum;

String str1;

g.drawString("Enter two numbers in Text Field',10,50);//message print on screen 

g.setColor(Color.yellow);

str1=T3.getText(); // Extracting the values entered in the text field

num1=Integer.parseInt(str1);

str1=T4.getText();

num2=Integer.parseInt(str1);

sum=num1+num2;

g.drawString(“The sum of two number is : "+sum,100,800);

showStatus("Two numbers Addition");

}

public boolean action(Event e, Object obj)

{

repaint(); // called every time a user enters the value that is the event occurs

return true;

}

}

The above applet takes two input strings in the text field from the user and displays the sum of the numbers entered by the user. The above applet is loaded in HTML page  like this.

<html>

<title>Sum of Two number</title>

<applet code="Demo_Sum.class " width=“300" height=“200"> Sum of two numbers</applet>

</html>

New to Java? Learn more about applets at Udemy.com

The browser automatically creates the object of the applet class and executes the applet. This is done manually in a Java application in main method of the class.