You are on page 1of 4

-1-

Applets
Introduction to Applet
An applet is a small Java program which can be embedded inside a web page or can be downloaded
from the WWW. These applets are executed by web browser on user’s machine. In order to run
applets we depend upon a Java-capable browser. Although they can also be viewed using a tool
called the appletviewer, which is used to check and run applets.
The Applet class must be the super class of any applet program that is to be embedded within a web
page. To use Applet class we must have to import java.applet package.
Creating a Java Applet
Creating applets is more complex from creating a simple application, because Java applets run and
are displayed inside a Web page with other page elements. To create a simple applet, instead of only
print a message on screen, we have to create an applet to make space for our message and then use
graphics operations to paint the message to the screen.
First, we have to set up an environment so that our Java-capable browser can find our HTML files
and applets. Initially, we should keep our HTML files and applet code in the same directory. A
directory to store applet code and HTML files we should create a separate directory using following
command on command prompt –
C:\> mkdir applets
Steps to Create an Applet code
Step 1: Open a editor and edit following code into it.
//MyApplet.java
import java.applet.Applet;
public class MyApplet {
public void init() {
showStatus(“My First Applet . . “);
}
}
Step 2: Save the file with name MyApplet.java.
Step 3: Compile the file with javac.
Step 4: Create an HTML file with name MyApp.html. Insert following code into it –
<HTML>
<BODY>
<APPLET CODE=”MyApplet” WIDTH=400 HEIGHT=300>
</APPLET>
</BODY>
</HTML>
Step 5: Open a Java enabled Web Browser and open MyApp.html file into it.
However, this applet code can also be executed in appletviewer. To view applet in the appletviewer,
we have to insert <APPLET> tag into out applet code as a comment entry. This can be done in
following manner –
//MyApplet.java
import java.applet.Applet;
/*<APPLET CODE=”MyApplet” WIDTH=400 HEIGHT=300>

Sapna Saxena, Lecturer, Chitkara University


-2-
</APPLET>*/
public class MyApplet {
public void init() {
showStatus(“My First Applet . . “);
}
}
Save and compile above file. Compiler ignores the comment entry at the time of compilation.
Now, to view this file in the appletviewer enter following command on command prompt –
C:\applets> appletviewer MyApplet.java
In this case, appletviewer only search the <Applet> tag in the applet code and ignore all the other
code present in the file.
Applet Tag
The complete syntax of <APPLET> tag is –
<APPLET
CODEBASE = codebase URL
ARCHIVE = archive list
CODE = applet class name
OBJECT = serialized Applet
ALT = alternate text
NAME = applet instance name
WIDTH = width of applet in pixels
HEIGHT = height of applet in pixels
ALIGN = alignment (Left / Right / Center)
VSPACE = Vertical space in pixels above the applet
HSPACE = Horizontal space in pixels on each side of applet>
<PARAM NAME = appletAttribute1 VALUE = value>
<PARAM NAME = appletAttribute2 VALUE = value>
(This tag is used to specify attributes for Applet. Applets can access these attributes using
getParameter() method.
</APPLET>

Sapna Saxena, Lecturer, Chitkara University


-3-
Life Cycle of an Applet
During execution of an applet certain methods public void init()
are called sequentially. This is called the Life
Cycle of the Applet. Applet class doesn’t
provide implementation of these methods. A public void start()
subclass of an applet (our applet code) should
override these methods to perform their
desired operation. public void paint()

public void stop()

public void destroy()

Life Cycle of Applet

These methods are –


Initialization of Applet – init()
init() method is used for initialization whenever an applet is loaded into a web browser. This method
is called by web browser to inform that the applet is loaded into the system. It is always called before
calling the start method of the Applet.
The general form of this method is –
public void init()
{
//Code block for initialization
}
Starting an Applet – start()
After the execution of init() method, start method is called immediately. This method is called by a
web browser to inform that applet should start its execution.
The general form of this method is –
public void start()
{
//Code block for start method
}
Painting an Applet – paint(Graphics g)
This method is used to draw an applet in the browser window. Paint method helps to draw an applet
in the browser window in graphical form. To do this we have to pass an object of Graphics class to
the paint function. Then this object of graphics class can be used to draw graphical images or text in
the container (browser window in case of applets).
The general form of this method is –
public void paint(Graphics g)
{
//Code block for painting an applet
}

Sapna Saxena, Lecturer, Chitkara University


-32-
Stopping an Applet – stop()
This method is called when user wants to leave the current page. Stop method is called by the web
browser to inform the applet that it should stop its execution.
The general form of this method is –
public void stop()
{
//Code block for stopping
}
Destroying an Applet – destroy()
This method is called immediately after the stop method. This method is used to destroy and clean up
the resources after exiting from the applet. This function also frees the memory used by the applet..
The general form of this method is –
public void destroy()
{
//Code block
}
Example:
//Program displaying the usage of applet methods
//MyFirstApplet.java
import java.applet.Applet;
import java.awt.*;
public class MyFirstApplet
{
String str;
public void init()
{
Str = “Hello World . . .”;
}
public void start()
{
showStatus(“Start method called . . “);
}
public void paint(Graphics g)
{
g.drawString(str, 10, 100);
}
public void stop()
{
showStatus(“Stop method called . . “);
}
public void destroy()
{
showStatus(“Destroy method called . . “);
}
}

Sapna Saxena, Lecturer, Chitkara University