Applet Life Cycle in Java

Hello everyone, welcome to my new applet life cycle in Java tutorial, which is one of the important topics of Java. This is a detailed guide to the Java applet life cycle.

In this in-depth tutorial, you will learn the following concepts of the Java applet life cycle –

  • What is the applet life cycle?
  • Stages of an applet life cycle in Java with Diagram
  • Methods of applet life cycle
  • How does Applet Life-Cycle work in Java?

Apart from these topics, I will also teach you many more things, such as the program of the applet life cycle in Java, the Syntax of the Applet Life Cycle etc. I will also explain the applet life cycle in Java with examples.

So keep reading this awesome applet life cycle in Java tutorial to learn the introduction and implementation of the Java applet life cycle.

But before going to the applet life cycle topic, I will give you a brief introduction of applets in Java to understand the further explanation of the applet life cycle very well. After going through this whole tutorial, I hope you will have no more confusion about the Java applet life cycle.

So are you ready to start to deep dive into applet in Java with a detailed explanation of its life cycle?

Yes, then let’s get started!!!

An Introduction To Applet Life Cycle In Java

What is an applet?

  • An applet is a Java program that is embedded into the webpage to create dynamic content. It runs inside the web browser and works on the client-side, and it is used to create a dynamic website.
  • It can be executed by browsers running under many platforms, including Linux, Windows, Mac OS etc.
  • The applet works on the client-side, and that’s why it has less response time.
  • An applet is a Java class that extends the java.applet.Applet class.
  • The applet does not use the main() method because when an applet is loaded, it automatically calls certain methods of the applet class to start and executes the applet code. An applet has its own life cycle.

Types of Applet in Java

  • Local Applet – When we create applets on our own and then embed them into web pages, this type of Applet is called a local Applet. Local applets are developed and stored on the local system. In the case of local Applets, the web page doesn’t need to get the information from the internet, and they search for local applets from all the directories of the local computer.
  • Remote Applet – Remote applets are developed by another developer and stored in a remote computer connected to the internet. We can download these applets when connected to the internet and run them in our local system.

What is Applet Life Cycle?

The applet life cycle is defined as the changes in stages of the applet during its execution. In other words, the applet life cycle is the process of how the applet is created, started, stopped, and destroyed during the entire execution of its application.

An applet goes through 4 stages during its life cycle, which are the followings :

  • Born or Initialization state
  • Running state
  • Idle state
  • Dead state

Stages of an Applet Life Cycle

Now we will see what are the steps in the lifecycle of an applet. So let’s get started, and first of all, see the diagram of the Applet life cycle in Java.

Applet Life Cycle in Java
Applet Life Cycle in Java with Diagram

1. Born or Initialization state

It is the first stage of an applet in the applet life cycle. In this state, an applet is just initialized or entered into the initialization state when it is first loaded. init() method is used to initialize an applet.

At this stage following actions may be taken –

  • Load images or font
  • Set up colors
  • Set up initial values
  • Create object as required by the applet

Initialization occurs only once in the applet’s life cycle.

2. Running state

It is the second stage of an applet in the applet life cycle. In this state, the applet starts running. start() method is used to run an applet.

3. Idle state

It is the third state of an applet in the applet life cycle. In this state, an applet becomes idle, which means it stopped from running. stop() method is used to stop an applet.

4. Dead state

In this state, the applet is removed from memory. destroy() method is used to destroy the applet. This occurs automatically when we quit the browser.

Like initialization, destroying occurs only once in the applet’s life cycle.

Methods of Applet Life Cycle

1. init() 

  • init() method is used to initialize an applet.
  • This is the first method of an applet to be called.
  • This method is called only once in the applet life cycle.

Syntax

2. start()

  • This method is used to start an applet.
  • The applet enters into a running state when the start() method is invoked.
  • This method can be used more than once. It is also used to resume the applet.

Syntax

3. paint()

  • This method is used to paint any shapes such as rectangles, squares, eclipses etc.
  • paint() method has one parameter of the type Graphics Class. This Graphics class enables the painting features in an applet.

Syntax

4. stop()

  • This method is used to stop the applet.
  • It is invoked every time the browser is stopped, minimized, or when there is an abrupt failure in the application.
  • This method is invoked automatically when we leave the current web page on which the applet was running.

Syntax

5. destroy()

  • This method is used to destroy the applet.
  •  It can be called only once.
  • This method is called automatically when we want to quit the browser.

Flow of the Methods of Applet Life Cycle

How methods of applet life cycle flow are depicted in the following diagram. The browser invokes these methods automatically, and we don’t need to call them explicitly.

Applet Life Cycle in Java
Flow of the methods of applet life cycle in Java

Syntax of Entire Applet Life Cycle

Here is the syntax of an applet life cycle.

Implementation of Applet Life Cycle

Now we will learn how to implement the applet life cycle. So let’s see the program of the applet life cycle in Java. For this, we need to create two files one is a Java file, and another is an html file.

This is our Java file saved as FirstAppletDemo.java.

  • First of all, import all the essential packages in your Java program, which are java.applet.Applet and java.awt.Graphics packages.
  • Then create a class (FirstAppletDemo in this Example) that will extend the Applet class.
  • Then inside the class, create the paint() method with the type Graphics class as one of its parameter.
  • Now to print on the screen, we will use the drawString() method of the Graphics class by specifying the string and coordinates in its parameters.
  • Next, compile the Java file, and after successful compilation, the class file of our Java program will be created.
  • Next, we need to embed this applet code in the html file. For this, we have to create an html file.
  • You can see the HTML code below, where there is an applet tag where we have to specify the URL of the Java Applet(FirstAppletDemo.class) and attributes for customizing the portion of space available to the applet inside the browser window.
  • Next, save the file with any name using the html extension. Example ( AppletDemo.html)
Now to run an Applet, we have two ways,

  • One way is to open the html file in a Java-enabled web browser.
  • The second way is by using the appletviewer command.

I am going to use the second way by using the appletviewer command. You can see the below screenshots to understand.

Applet Life Cycle in Java – Compilation and Running

Output :

Applet Life Cycle in Java
Applet Life Cycle in Java – Output

Applet Life Cycle in Java PDF and PPT Download

So guys, this is all about the applet life cycle in Java tutorial. I hope you have enjoyed this tutorial and found it helpful. If so, don’t forget to share this post with your friends. And if you have any doubt regarding this applet life cycle post, you can ask your queries in the comment section. I will be happy to solve your questions. Thanks

Leave a Comment