Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Applet Program in Java

Last Updated on May 22, 2023 by Prepbytes

In Java, an applet is a special type of program that runs within a web browser. It is designed to provide dynamic and interactive content on a webpage. Applets were commonly used in the early days of the Internet for creating interactive games, animations, and other graphical applications.

History of GUI in Java

We must first comprehend the background of Java GUI in order to grasp what applets are. JDK 1.0, or the initial version of Java, was released in 1996. Only Character User Interface (CUI) was supported. However, Microsoft Visual Basic was the programming language of choice for Graphical User Interface (GUI) development beginning in 1996.

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.

A component of this cycle is applets. Applets are Java applications that operate on the client side and are executed inside a web browser. These are the programs that are hosted on a web server that may be embedded in a web page using the HTML APPLET or OBJECT element. Applets are not Java stand-alone programs since they cannot run alone. To test and comprehend their behavior, they are launched within a browser or via the applet viewer (a tool given by Java to run applets).

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.

Conclusion
In conclusion, applets in Java are special programs that run within web browsers to provide interactive and dynamic content on webpages. They were commonly used in the past but have declined in popularity due to security concerns and the rise of alternative web technologies. Applets have a specific lifecycle with methods like init(), start(), stop(), and destroy(). They are embedded in HTML webpages using the < applet> or < object> tags. Applets can be developed using Java development environments and the Java Development Kit (JDK).

Frequently Asked Questions (FAQs) about Java Applets

Q1. Are Java applets still supported by modern web browsers?
Ans. No, modern web browsers have limited or removed support for Java applets due to security concerns. It is recommended to use alternative web technologies like JavaScript, HTML5, and WebGL for web-based interactive content.

Q2. Can I run Java applets on any operating system?
Ans. Java applets can be executed on any operating system that has a Java Runtime Environment (JRE) installed. However, due to decreasing support, running applets may require older browser versions or specialized applet containers.

Q3. How can I deploy and distribute a Java applet?
Ans. To distribute a Java applet, you need to compile the applet code into a .class file and package it along with the required resources into a .jar (Java Archive) file. The .jar file can be embedded in an HTML webpage and distributed to users.

Q4. Are there any alternatives to Java applets for web-based interactive content?
Ans. Yes, there are several alternatives to Java applets for web-based interactivity. JavaScript, HTML5, and related frameworks/libraries offer rich functionality for creating interactive web applications, games, and animations.

Q5. Can applets access local resources on the user’s computer?
Ans. Applets run in a sandboxed environment with restricted access to the user’s computer resources. They have limited permissions to ensure security and prevent unauthorized access to local files or system resources.

Q6. Is it still worth learning applet programming in Java?
Ans. Given the declining support for applets in modern web development, it is not considered essential to learn applet programming. However, understanding applet concepts may provide insights into historical web development practices and help in maintaining legacy systems.

Leave a Reply

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