In Java, Applets is a program that can run on a Web Page. Applet runs in a browser, and it can be embedded in the web browser (HTML page) using the Object tag. Applet makes Java more compatible and allows it to create dynamic web pages.

Java Applet has a life cycle with appropriate methods assigned to each of them. To work with applets, you must go through these thoroughly.

Applets have five life cycle events, which of them, three events are initiated when an Applet begins:

  • init()
  • start()
  • paint()

Besides this, the remaining two events are initiated when an applet is about to terminate.

  • stop()
  • destroy()

init(): This is the very first method that is called, and this method is called only once for a particular applet. In this method, you can initialize variables.

start(): After the init() method, the start() method will be called. start() method is called whenever the Applet is restarted. For instance, if the user leaves the webpage and tries to revisit the webpage, the start() will be called, whereas init() won’t be called again.

paint(): This is a method of graphics class, and it is called whenever the output is required from the Applet in the web browser. This method contains parameters to create a graphical representation.

stop(): Whenever the web browser closes the HTML page, stop() method is called. When stop() is called, the Applet doesn’t stop because it can be restarted by calling the start() method.

destroy(): When the system wants to remove the Applet completely from the web browser and memory as well, stop() method will be called before the destroy() method always.

Benefits of Applets

Java applets have great features that make Java suck a successful language. Below are some on the benefits of using Java Applets:

  • Applets reduce the response time of the client-side server.
  • Applets works on browsers and browsers make it suitable for almost all the operating systems.
  • Applets are secure.
  • Applets support audios as well as animations.
  • Applets can display documents.

All these features make Applet the first choice of developers who uses Java to develop web applications.

Restriction with Applets

Since Applet is secured, it has the following restrictions:

  • Applets can’t read system properties.
  • Applets can’t load libraries.
  • Applets can’t read and write files on the host side.
  • Applets can’t establish a network connection expect with its native host.

Program Illustrations

import java.applet.Applet;
import java.awt.Graphics;

/*
<applet code="firstapplet" width=200 height=60>
</applet>
*/

// firstapplet class extends Applet
public class firstappplet extends Applet {
 // Overriding paint() method
 @Override
 public void paint(Graphics gr) {
  gr.drawString("Hello World", 20, 20);
 }
}

In this program, two packages are imported “Applet and Graphics”. The graphics package is used to create graphics interfaces in the program. The paint() method is linked with the graphics class and in order to create generate a graphical interface, you have to override the paint() method.

An object (“gr”) of graphics class is created, and later drawString method is used of graphics function to print the string “Hello World” with two dimensions “x”, “y”.

A general definition of drawString method seems like this:

void drawString(String Message, int x, int y)

Here the message is the output that needs to be printed at the location (x,y). For this program, Hello World will be printed at location 20,20 in the Applet window.

How to Run a Java Applet program

In general, there are two ways to run an applet program. One of them is pretty long and required more effort. Whereas the other one is efficient and quick. You must focus on the second method.

It is because in the program itself the applet script is added that will automatically create an Applet Viewer.

Follow the steps to execute the Applet program:

Step 1. First, compile the program using the following command:

javac firstapplet.java

Step 2. Once the program is compiled successfully without any errors, you have to launch the appletviewer. Run the following command:

appletviewer firstapplet

Here you need to use the keyword appletviewer followed by the name of the class where the paint method is overridden.

Once both of these steps are done, you will see a new applet viewer. Refer to the image attached below:

As you can see, in the Applet Viewer the message “Hello World” is shown as per the give coordinates “x” and “y” given in the drawString() method. If you want, you can print the message in the Applet Viewer window at a different location by changing the coordinate values given in “x” and “y”.

Apart from this, in the left corner of the Applet Viewer, there is a “Status Bar”. In order to print the status of the Applet, you can use the “showStatus()” method. You can also change the background of the applet window. By default the background color is white.

Final Notes

Although applets are very efficient and secure in 2020, none of the browsers actually supports applets. It is because the applets have dozens of restrictions and these applets don’t work flawlessly all the time. Therefore after 2017, support of applets was reduced and after the release of Java SE 11 in September 2018, applets were completely removed. The applets are usually practiced for small projects.

Last modified: July 13, 2020

Author

Comments

Write a Reply or Comment

Your email address will not be published.