Applet is a Java program that can be transported over the internet and executed by a Java-enabled web-browser(if a browser is supporting the applets) or an applet can be executed using the appletviewer utility provided with JDK.
An appLet us created using the Applet class, which is a part of java.applet package. Applet class provides several useful methods to give you full control over the execution of an applet.
There are two types of the applet -
Applets based on the AWT(Abstract Window Toolkit) package by extending its Applet class.
Applets based on the Swing package by extending its JApplet class
Note :
At present, some modern versions of browsers like Google Chrome and Mozilla Firefox have stopped supporting applets, hence applets are not displayed or viewed with these browsers although some browsers like Internet Explorer and Safari are still supporting applets.
Some methods of Applet class
Methods
Description
void init()
The first method to be called when an applet begins its execution.
void start()
Called automatically after init() method to start the execution of an applet.
void stop()
Called when an applet has to pause/stop its execution.
void destoy()
Called when an appLet us finally terminated.
String getParameter(String ParameterName)
Returns the value of a parameter defined in an applet
Image getImage(URL url)
Returns an Image object that contains an image specified at location, url
void play(URL url
Plays an audio clip found at the specified at location, url
showStatus(String str)
Shows a message in the status window of the browser or appletviewer.
The lifecycle of an applet through its methods-
init()- This is the first method called during the lifecycle of an applet. This method kickstarts the execution of an applet. In this method, we usually
initialize variables that are going to be needed throughout the life of an applet.
start() - Method start() is called automatically by init() method. This method is a place where the actual work begins and it is called right after
an appLet us resuming its execution after being in a suspended position when its window was minimized.
stop() - This method is called when an applet's execution has been stopped or suspended for a while because the user has minimized the applet window.
destroy()- This is the last method to be called in the lifecycle of an applet as this method is called only when an applet has finally terminated its execution. This method is a place for clean-up operation in order to free the memory from the resources consumed during the lifetime of an applet.
Advertisement
A simple example of an AWT applet by extending Applet class
import java.awt.*;
import java.applet.*;
/*
<applet code="Applet1" width=400 height=200>
</applet>
*/
public class Applet1 extends Applet
{
public void init()
{
System.out.println("Initializing an applet");
}
public void start()
{
System.out.println("Starting an applet");
}
public void stop()
{
System.out.println("Stopping an applet");
}
public void destroy()
{
System.out.println("Destroying an applet");
}
}
How to run an applet?
To run an applet we must define an <applet> tag inside the comments, within our applet program file.
An applet program doesn't need a main() method to run, hence we run an applet either in :
A browser, but remember, most of the modern versions of browsers are no longer supporting applets. So, running an applet in a browser is not advisable, or
By using appletviewer utility which is provided with JDK. Due to inconsistency in the execution of applets with some browsers,
running an applet using appletviewer is the most common approach these days and we will follow this approach throughout our articles on applets.
In order to run the applet code mentioned above, by using appletviewer, type the following command at command prompt-
appletviewer Applet1.java
Where Applet1.java is the name of java file that contains the code of an applet.
Output
Right after running the applet program using appletviewer a new applet window is displayed to us -
Note :
System.out.println() prints the output only in the command prompt window.
The appLet us also being executed using the appletviewer at command prompt, hence, at the command prompt,
you will be displayed three messages due to the execution of init(), start() and paint() method defined in Applet1.java
D:\Java>appletviewer Applet1.java
Initializing an applet
Starting an applet
As soon as you close the window of your applet, a couple of methods will be called in sequence -
stop() method, stopping the execution of the applet.
destroy() method, terminating the applet completely and freeing up resources consumed by it.
Hence, you will get a couple of new messages at the command prompt due to the execution of stop () and destroy() method.
D:\Java>appletviewer Applet1.java
Initializing an applet
Starting an applet
Stopping an applet
Destroying an applet