Applet Programming
Applet Programming
A Java applet is a special kind of Java program that a browser enabled with Java technology
can download from the internet and run. An applet is typically embedded inside a web page
and runs in the context of a browser. An applet must be a subclass of the java.applet.Applet
class. The Applet class provides the standard interface between the applet and the browser
environment.
The Applet class is contained in the java.applet package.Applet contains several methods that
give you detailed control over the execution of your applet.
Applet Basics:
All applets are subclasses of Applet. Thus, all applets must import java.applet. Applets must
also import java.awt. AWT stands for the Abstract Window Toolkit. Since all applets run in
a window, it is necessary to include support for that window by importing java.awt package.
Applets are not executed by the console-based Java run-time interpreter. Rather, they are
executed by either a Web browser or an applet viewer.
Execution of an applet does not begin at main( ). Output to your applet’s window is not
performed by System.out.println( ). Rather, it is handled with various AWT methods, such
as drawString( ), which outputs a string to a specified X,Y location. Input is also handled
differently than in an application.
Once an applet has been compiled, it is included in an HTML file using theAPPLET tag. The
applet will be executed by a Java-enabled web browser when it encounters the APPLET tag
within the HTML file.
To view and test an applet more conveniently, simply include a comment at the head of your
Java source code file that contains the APPLET tag.
Here is an example of such a comment:
/*
<applet code="MyApplet" width=200 height=60>
</applet>
*/
This comment contains an APPLET tag that will run an applet called MyApplet in a window
that is 200 pixels wide and 60 pixels high. Since the inclusion of an APPLET command makes
testing applets easier, all of the applets shown in this tutorial will contain the appropriate
APPLET tag embedded in a comment.
1
The Applet Class:
Applet extends the AWT class Panel. In turn, Panel extends Container, which extends
Component. These classes provide support for Java’s window-based, graphical interface.
Thus, Applet provides all of the necessary support for window-based activities
Applet Architecture:
An applet is a window-based program. As such, its architecture is different from the so-called
normal, console-based programs .
First, applets are event driven. it is important to understand in a general way how the event-
driven architecture impacts the design of an applet.
Here is how the process works. An applet waits until an event occurs. The AWT notifies the
applet about an event by calling an event handler that has been provided by the applet. Once
this happens, the applet must take appropriate action and then quickly return control to the
AWT.
An Applet Skeleton:
These five methods can be assembled into the skeleton shown here:
// An Applet skeleton.
import java.awt.*;
import java.applet.*;
/*
<applet code="AppletSkel" width=300 height=100>
</applet>
*/
public class AppletSkel extends Applet
{
// Called first.
public void init()
{
// initialization
}
/* Called second, after init(). Also called whenever
the applet is restarted. */
public void start()
{
// start or resume execution
}
// Called when the applet is stopped.
public void stop()
{
// suspends execution
2
}
/* Called when applet is terminated. This is the last
method executed. */
public void destroy()
{
// perform shutdown activities
}
// Called when an applet's window must be restored.
public void paint(Graphics g)
{
// redisplay contents of window
}
}
It is important to understand the order in which the various methods shown in theskeleton are
called. When an applet begins, the AWT calls the following methods, in this sequence:
1. init( )
2. start( )
3. paint( )
When an applet is terminated, the following sequence of method calls takes place:
1. stop( )
2. destroy( )
init( ):init( ) method is called once—the first time an applet is loaded. The init( ) method is
the first method to be called. This is where you should initialize variables.
start():The start( ) method is called after init( ). It is also called to restart an applet after it
has been stopped(i.e start() method is called every time, the applet resumes execution).
Paint():The paint( ) method is called each time your applet’s output must be redrawn. This
situation can occur for several reasons. For example, the window in which the applet is running
may be overwritten by another window and then uncovered. Or the applet window may be
minimized and then restored. paint( ) is also called when the applet begins execution.Whatever
the cause, whenever the applet must redraw its output, paint( ) is called. The paint( ) method
has one parameter of type Graphics.
stop( ):The stop() method is called when the applet is stopped(i.e for example ,when the
applet is minimized the stop method is called).
destroy( ):The destroy( ) method is called when the environment determines that your applet
needs to be removed completely from memory(i.e destroy() method is called when the applet
is about to terminate).The stop( ) method is always called before destroy( ).
3
Simple Applet programme:
SimpleApplet.java
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleApplet" width=300 height=100>
</applet>
*/
g.drawString(msg,20,20);
}
}
Output:
4
How To Run an Applet Programe:
SimpleApplet.html
<html>
<body>
<applet code="SimpleApplet" width=300 height=100></applet>
</body>
</html>
Here, message is the string to be output and x and y are x-coordinate ,y-coordinate
respectively. In a Java window, the upper-left corner is location 0,0.
5
Example
* A simple applet that sets the foreground and background colors and outputs a string. */
import java.awt.*;
import java.applet.*;
/*
<applet code="Sample" width=300 height=200>
</applet>
*/
public class Sample extends Applet
{
String msg;
public void init()
{
setBackground(Color.gray);
setForeground(Color.white);
msg = "Inside init( ) --";
}
// Initialize the string to be displayed.
public void start()
{
msg += " Inside start( ) --";
}
// Display msg in applet window.
public void paint(Graphics g)
{ msg += " Inside paint( ).";
g.drawString(msg, 10, 30);
}
}
Output:
Requesting Repainting:
Whenever your applet needs to update the information displayed in its window, it simply
calls repaint( ).The repaint( ) method is defined by the AWT. It causes the AWT run-time
system to execute a call to your applet’s update( ) method, which, in its default
implementation, calls paint( ).
< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels]
[HSPACE = pixels]
>
[< PARAM NAME = AttributeName VALUE = AttributeValue>] [<
PARAM NAME = AttributeName2 VALUE = AttributeValue>]
...
[HTML Displayed in the absence of Java]
</APPLET>
CODEBASE: CODEBASE is an optional attribute that specifies the base URL of the
applet code, which is the directory that will be searched for the applet’s executable
class file (specified by the CODE tag).
CODE :CODE is a required attribute that gives the name of the file containing your
applet’s compiled .class file. This file is relative to the code base URL of the applet,
which is the directory that the HTML file was in or the directory indicated by
CODEBASE if set.
ALT :The ALT tag is an optional attribute used to specify a short text message that
should be displayed if the browser understands the APPLET tag but can’t currently
run Java applets.
NAME: NAME is an optional attribute used to specify a name for the applet instance.
Applets must be named in order for other applets on the same page to find them by
name and communicate with them.
WIDTH AND HEIGHT :WIDTH and HEIGHT are required attributes that give the
size (in pixels) of the applet display area.
ALIGN: ALIGN is an optional attribute that specifies the alignment of the applet.The
possible values:LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP,
ABSMIDDLE,and ABSBOTTOM.
7
VSPACE AND HSPACE :These attributes are optional. VSPACE specifies the space,
in pixels, above and below the applet. HSPACE specifies the space, in pixels, on each
side of the applet.
PARAM NAME AND VALUE: The PARAM tag allows you to specify applet specific
arguments in an HTML page. Applets access their attributes with the getParameter( )
method.
________