Java – Applet, Life Cycle of Applet and Methods4 min read

The applet in Java is a special type of internet-based program, that runs on the web browser and works at the client-side. It is used to make the webpage more dynamic and provide interactive features that cannot be applied by the HTML alone.

  • Any applet in Java is a class that extends the java.applet.Applet class.
  • An Applet class does not contain the main() method.
  • JVM(Java Virtual Machine) is needed to view an applet. To run an applet application, the JVM can use either a plug-in of the Web browser or a separate runtime environment.

Some of the benefits of using Java Applet:

  1. It is secured. It runs in Sandbox which is a Byte-code verifier.
  2. Since it works at the client side so less response time.
  3. It can be executed by the browsers that are running under any platform.

One of the Drawbacks of Java Applet is that for executing applets, plugins are required at the client browser.

Life Cycle of a Java Applet

Java Applet
Java Appplet

The various stages that an Applet undergoes from its creation to its destruction of objects is known as the Applet life cycle.

Applet life cycle consists of 5 states and each of the states is a signified method and called callback methods.

Following are the states in Applet:

init() method: This method is used for the initialization of an Applet. It is only executed once in a program.

start() method: This method is used to start the Applet and called automatically after the JVM calls init() method. It can be called repeatedly.

paint() method: This method is called immediately after the start(). It is inherited from java.awt, used to paint the object providing the graphic class object.

stop() method: This method is called automatically to stop the Applet. It can also be called repeatedly in the same applet.

destroy() method: This method is used to destroy the Applet and also executed only once in the program.

Applet Class:

All the Applet present is an extension of the java.applet.Applet class. These classes are necessary for the execution of an applet. They include various methods for the applet such as methods to fetch and display images and play an audio clip, resize an applet, to get applet parameters, etc.

Demonstrate a simple Applet example in Java.

After saving this code with ‘‘. We need to create an HTML file to include this applet. Create an HTML file named first.html within the same folder where you have saved the

Display Methods in Applet:

As we know applet is displayed in a window and for the input/output operations, they use AWT. To output a string, we use drawString().

The general form for drawString():

In the above form, this method is written, beginning at x,y which is the location we want the line to begin. This is a graphics class.

  • To set a background color of an applet’s window, we use setBackground().
  • To set the foreground color, we use setForeground().

The general form of setBackground() and setForeground() are:

The class Color defines the constants shown here that can be used to specify colors:

  •   Color.cyan
  •   Color.darkGray
  •   Color.gray
  •   Color.lightGray
  •   Color.magenta
  •   Color.white
  •   Color.yellow

The following shows the source code on how to display background color.

The default foreground color is black and the default background color is light gray.


Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More