Udemy logo

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:

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.

Page Last Updated: May 2014

Top courses in Java

Java Programming Masterclass covering Java 11 & Java 17
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.6 (164,055)
Bestseller
Java for Absolute Beginners
Nick H
4.6 (5,058)
Java Programming for Complete Beginners
in28Minutes Official
4.5 (25,817)
Complete Core Java In Simple Way
DURGASOFT NAGOOR BABU
4.5 (501)
Develop Minecraft Plugins (Java)
Stephen King
4.7 (2,902)
Bestseller
What's New in Java 9 - Modules and More!
Tim Buchalka's Learn Programming Academy, Dr. Frank Mitropoulos
4.6 (722)
JAVA Immersive Learning Course
Matthew Fried
4.9 (58)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo