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 ‘FirstApplet.java‘. 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 FirstApplet.java.


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.black
  •   Color.blue
  •   Color.cyan
  •   Color.darkGray
  •   Color.gray
  •   Color.green
  •   Color.lightGray
  •   Color.magenta
  •   Color.orange
  •   Color.pink
  •   Color.red
  •   Color.white
  •   Color.yellow

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

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


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More