Applet Program in Java

In this article, we will write an applet program in Java. We will understand what applets are in Java, and the life cycle of an applet in Java, and will write an applet program in Java. So, let’s get started.

History of GUI in Java

Before we understand what applets are, we first need to understand the history of Java GUI. Java was launched in 1996 for the first time i.e. JDK 1.0 was launched. It only supported Character User Interface (CUI). However, in 1996 only, Visual Basic of Microsoft was preferred for Graphical User Interface (GUI) programming.

So, to cope with the market, Java developers decided to add GUI support in Java from Operating System. This means that a platform-dependent i.e. OS dependent GUI support was added in Java. Os dependency means that the size and shape of the buttons and other components will vary from one Operating System to another Operating System.

So, different operating systems had different components. Java developers combined multiple such components from different operating systems and built a library called Abstract Window Toolkit (AWT). So, Java AWT was the GUI-supporting library at that time.

It had multiple components like buttons, panels, etc. however it did not have some of the advanced components like dialogue boxes, etc.

Java applets are a part of this AWT library. However, we will study the Java applets in detail in the further sections of this discussion.

So, the problem with JWT is clearly visible it was platform-dependent. The applets (in JWT) required a browser to run and Internet Explorer was the only browser available at that time. Java developers decided not to go with Microsoft and Internet explorer. They teamed up with a company called Netscape. Netscape is also the developer of Javascript. The Java developers at Sun Microsystems and the developers at Netscape shook hands and formed the Swing library of Java. The Swing library is another GUI library of Java that follows the Observer design pattern.

Since the Swing components are produced by Java itself, Swing is platform-independent. Also, the Swing components are more lightweight as compared to JWT components. Swing has a lot of advantages over JWT. However, that is not the scope of this article.

Now that we have an idea of JWT and Swing and the history of Java GUI, let us jump into learning what applets are.

What are Applets in Java?

We know the basic request-response cycle for any website. This is shown below.

So, we have a client that interacts with the website and sends a request. The request is sent to the browser and the browser sends the request to the server by adding various headers to it. The server does some processing on its end and sends the response back to the browser. The browser then parses the response and renders it on the website for the Client.

Applets are a part of this cycle. Applets are the programs in Java that run inside a web browser and work on the client side. These are the programs that can be embedded in a web page using the APPLET or OBJECT tag in HTML and are hosted on a web server. Applets cannot run by themselves i.e. applets are not stand-alone programs in Java. They are run inside a browser or run inside the applet viewer (a tool provided by Java to run applets) to test and understand their behavior.

Since applets are a part of GUI programming in Java, they are obviously used to make websites more dynamic.

All the applets that we create must extend directly or indirectly from the java.applet.Applet class. Here, “applet” is a package and “Applet” is a class in Java.

Let us now understand the life cycle of an applet which will help us understand applets in more detail.

Applet Life Cycle in Java

The life cycle of an applet is shown below.

The first three methods i.e. init(), start(), and paint() are called when an applet begins and the last 2 methods i.e. stop() and destroy() are called upon applet termination. Let us understand these methods one by one in detail.

init(): The name of the method init suggests that it might be useful for initialization. Yes, this is true. The init() method is the first method that gets called upon an applet creation and is used to initialize the variables. The most important fact about the init() method is that it is only called once in the entire applet life cycle.

start(): This method is used to start/restart an applet. Let us say that an applet is created for the first time. So, the init() method will be called and after it, the start() method will be called for starting the applet. However, now that the applet has been created and say the pallet is stopped due to some reason, the init() method will be not called again. The start() method will be called to restart an applet.

paint(): The paint() method is used to draw/redraw the applet’s output. So, drawing an applet’s output means the first time an applet gets created and we need to draw its output. So, this method will execute after init() and then after start().
The paint() method redraws the applet’s output in several conditions. For instance, the windows running the applet were closed and then started again or the window might get minimized and then maximized again or the window running the applet program might be covered by some other window and then get disclosed again.
The paint() method is a parametrized method. Its syntax looks like this.

stop(): The stop() method is called when the window running the applet program is closed. For instance, we leave the webpage and go to some other web page, in this condition, the stop() method will be called.

destroy(): This method is called when the applet needs to be removed completely from the memory. We free up any resources held by the applet before destroying it. The stop() method is always called before the destroy() method.

Now that we have understood the applet life cycle, let us write an applet program in Java.

Applet Program in Java

The below program shows how to print your name by running an applet in Java.

// A PrintName Applet
// Save file as PrintName.java
import java.applet.Applet;
import java.awt.Graphics;

// HelloWorld class extends Applet
public class PrintName extends Applet
{
    // Overriding paint() method
    @Override
    public void paint(Graphics g)
    {
        g.drawString("Guneet Malhotra", 20, 20);
    }
    
}

Explanation: We have already talked about the fact that every Applet class must be a child class of the java.applet.Applet class. So, the first import statement imports the class Applet from the package applet. The second import statement imports the Graphics class from AWT because we need a reference of Graphics as a parameter to the paint() method. The class PrintName is declared public because it will be accessed by the code outside this program. The paint() method of the Applet class is overridden to perform the function that we want to perform i.e. printing our name. The drawString() is a method of the Graphics class. It contains three parameters. The first is the string that we want to draw. So, we have entered our name into that. The second and third parameters are the X and Y coordinates where the string will be drawn.

The top-left corner has X and Y coordinates as (0,0).

How to run an Applet Program in Java

The applet program can either be run in a browser that supports Java (Applets) or using the appletviewer (a tool provided by Java). We need to create an HTML file with the following code.

Then, we can run this file either in the browser that supports applets, or using appletviewer as shown below.

So, we have discussed an applet program in Java. We hope that you have understood the concepts of applets, their life cycle, and the applet program in java. We also hope that you liked the discussion. See you again soon at PrepBytes.

Leave a Reply

Your email address will not be published. Required fields are marked *