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 Interview Help
Bharath Thippireddy
4.6 (1,180)
Java Programming for Complete Beginners
in28Minutes Official
4.5 (38,781)
Oracle Java Certification - Pass the Associate 1Z0-808 Exam.
Tim Buchalka's Learn Programming Academy, Goran Lochert
4.5 (5,369)
Bestseller
Java for Absolute Beginners
Nick H
4.6 (7,429)
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.5 (3,634)
Bestseller
Learn Selenium with Java, Cucumber + Live Project
Pavan Kumar
4.6 (4,233)
Bestseller
Java SE 11 Developer 1Z0-819 OCP Course - Part 2
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.3 (993)
Java Tutorial for Beginners
Navin Reddy
4.5 (520)
Java Streams API Developer Guide
Nelson Jamal
4.6 (3,403)

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 Business.

Request a demo