An applet is a small programme that runs within another application, typically a web browser. Applets are written in the Java programming language and are used to provide interactive and dynamic features on web pages. Unlike standalone applications, applets cannot run on their own; they require a runtime environment provided by a web browser or an applet viewer.
Applications of applets
Applets have a variety of applications in web development and other areas. Some of the most common applications include:
-
Interactivity on websites: Applets are used to add interactive elements to web pages, such as games, animations and interactive forms.
-
Data visualisation: Applets can display graphs and dynamic tables, which is useful for visualising data in real time.
-
Educational simulations: In the field of education, applets are used to create interactive simulations that help students understand complex concepts.
-
Calculation tools: Applets can provide calculation and analysis tools that run directly in the browser, without the need to install any additional software.
-
Security and authentication: Some applets are used in security systems for user authentication and data encryption.
How does an applet work?
To understand how an applet works, it is important to be familiar with the applet’s life cycle, which consists of several key methods:
-
init(): This method is called once when the applet is loaded for the first time. This is where the resources required by the applet are initialised.
-
start(): This method is called every time the applet is displayed on the screen. If the user navigates to another page and then returns, the start() method is called again.
-
paint(Graphics g): This method is used to draw on the screen. It is called whenever the applet needs to be redrawn, such as when the browser window is resized.
-
stop(): This method is called when the applet is no longer visible. This is where you can free up resources that are not needed whilst the applet is not in use.
-
destroy(): This method is called when the applet closes down completely. This is where all resources are released and the final clean-up tasks are carried out.
How do you create an applet?
Creating an applet involves several steps, from writing the code to embedding it in a web page.
-
Write the applet code: The first step is to write the Java code for the applet. Here is a simple example of an applet that displays a message on the screen:
-
Compile the code: Once you have written the code, you need to compile it using the Java compiler (javac). This will generate a .class file containing the applet’s bytecode. javac HelloWorldApplet.java
-
Create an HTML file: To run the applet in a browser, you need to create an HTML file to contain it.
-
Run the applet: Open the HTML file in a browser that supports Java applets. The applet should run and display the message “Hello, World!”.


