Java Applet Basics and Architecture Guide
Java Applet Basics and Architecture Guide
1
Syllabus
Applet
Applet Basics Introduction, limitations of AWT. Applet architecture- HTML
parameter to Appletget, DocumentBase) and APPLET tag Passing
-
Contents
1.1 Introduction
(1-1)
1 . 1 introduction
be used in internet.
Applets
environment.
are the small Java programs
that can
etworking
transferred over the internet from mputer to another
one computer to.
continuously.
2. If we want some special effects such as sound, animation and much. more then
the applets are used.
3. If we want a particular application should be used by any user who might be
located remotely. Then in such situation the applets are embedded into the web
pages and can be transferred over the internet.
Difference between Applet and Application
Sr. No. Applets Applications
1 Applets do not have main method. On
APpication programs have main meth0d
loading of applets some methodssof
Withinthe mainmethodthe call toanother
appiet class get called automatically.
methods of Java class is given.
2. Applets cannot run independently.
Appications program run independently
They can be either embedded in web
page or can be run
using appletviewer.
3. Applets cannot be read from file.
Application programs make useof I/O
Similarly appletscan not write to files functions and can read afile orwritetofile
Applets cannot communicaie with
Java programs can communicate with other
others on the network.
programs in distributed environment.
5. Appleis cannot execute any program on local
Applications can execiute a program on
local computer.
çomputer,
TECHNICAL PUBLICATIONS an
up-thrust for knowledge
Advanced JAVA Programming
1-3
Review Question Applet
1.
Differentiate applet and application with any four
points.
1.2 Applet Architecture
There are various methods which
typically used are
in
applet for
termination purpose. These methods are: initialization and
1. Initialization
2. Running state
3. Idle state
4. Dead or
destroyed state (Refer Fig. 1.2.1 for applet's life cycle)
Applet
loaded
Borm init()
start() Applet
destroyed
Stopl)
Running ldle
destoy() End
Daad
Start()
Paint() Exit
browser
a) init()
b) start() )paint()
When applet is terminated
following method are invoked in sequence.
a) stop0 b) destroy0
1. Initialization state
When applet gets loaded it enters in the initialization state. For this
purpose the
init() method is used. In this method
you can initialize the
required variables. This
method is called only once initially at the execution of the
program. The can syntax
be,
Public void init()
Iinitialization of variables
TECHNICAL PUBLICATIONS an
up-thrust for knowledge
1-4
Advanced JAVA Programming
4. Setting up of colors.
2. Running state
3. Display state
Applet enters in the display state when it wants to display some output. This ma
happen when applet enters in the running state. The paint() method is for
is
displaying or drawing the conternts on the screen. The syntax
public void paint(Graphics g)
4. Idle state
This is an idle state in which applet becomes idle. The stop() method is invokeu
5. Dead state
method
to be dead then it
is removed from memory. The
When applet is said
to
w e want to
terminate applet completely and want
invoked when
destroy() is
remove it from the memory.
to destroy method.
I t is a good practice
to call stop prior
Review Questions
1 Describe following states of applet life cycle:
a) Initialization state. b) Running state. c)Display state.
2. Explain applet life cycle with suitable diagram.
Attribute Description
CODEappletfilename The specified applet can be loaded in the web page. This
attribute must be specified in order to embed the applet in the
HTML file.
WIDTH pixels This attribute specifies the width and height of the applet.
HEIGHT=pixels
ALIGNalignment This attribute is for specifying the alignment. Various
alignments are TOP, BOTTOM, LEFT, RIGHT, MIDDLE,
ABSMIDDLE, ABSBOTTOM, TEXTTOP and BASELINE.
This is
an optional attribute.
CODEBASE= codebase URL It specifies the name of the directory in which the applet is
stored. This attribute is
required when the attribute is not there
in the current
waorking directory. This is an
optional attribute.
RIGHT this
this attribute
either LEFT or
ALIGN is
When
horizontal
lank
blank spaces the brow
HSTACE= pixels amount of
the
specifies
. This is an optional attribute
around the applet.
should leave
Review Questions
available in < applet> tag.
[Link] all attributes
with its major attributes only.
2 Explain <applet> tag
3. Describe the folloving attributes ofapplet:
(i) Codebase (i) Alt (ii) Width(iv) Code
For the Applet class the package [Link] is required. This class provides the
applet's life cycle method such as init(), start() and paint().
The Graphics class is supported by the package [Link].
Here is a simple applet.
Example Program
import [Link].";
import [Link].";
public class FirstApplet extends Applet
W e always need these two packages to be imported in the program. The [Link]
package consists of java awt classes. Here awt stands for abstract window toolkit.
The AWT provides the support for window based graphical interface such as for
drawing screen, windows, buttons, text boxes, menus and so on. The other
imported package is [Link]. This is essential because we need to use Applet
class in our applet program which is included in [Link] package.
The functionalities that are required to run applet inside the web browser are
supported by [Link].
Then comes
Public claSs FirstApplet extends Applet
The class FirstApplet is a subclass of class Applet. Hence the keyword extends is
used. Java requires that your applet subclass (here it is FirstAplet) should be
declared as public. Hence is the declaration!
W e have then defined a method
Java Program[[Link]]
import [Link].";
import [Link]."
public class FirstApplet extends Applet
Step 2: Compile your applet source program using javac compiler, i.e.
Diltest>javac [Link]
Step 3: Write following code in notepad/wordpad and save it with filename and
extension .html. For example following code is saved as
Exe_FirstApplet.html, The code is
<html>
<body>
applet code="FirstApplet width=300
height 100>
/applet>
</body>
</html>
For
embedding the applet in the HTML document the <APPLET>
tag is used. i
APPLET> tag supplies the name of the
load the
applet to be loaded. we ant
Suppose va
applet FirstAppletprogram in the web
page then the applet tag wu e,
TECHNICAL PUBLICATIONS a n
up-thrust for knowledge
Applet
Advanced JAVA Programming 1-9
<APPLET CODE= [Link]
WIDTH = 500
HEIGHT = 300
</APPLET>
wwwwwe
EDtest\Exe FirstApplethm
Edit View Favorites Toos Help
Fle
Seach Favorites
*********q************
Go Lis Note this
Adress 1E 0:testExe [Link]
Search Web URL
[Link].
import [Link].
Embedded Applet in
<applet code-"FirstApplet" width=300 height=100
</applet> Java
public class FirstApplet extends Applet
First Applet",50,30);
[Link]("This is my
of the
I n above code we have added applet code at the beginning program.
am.
width=300 height=100>
applet code="FirstApplet'
</applet
.This will help to understand Java that the source program 1S an applet wi#
wit hthe
name FirstApplet.
Step 2: By this edition you can run your applet program merely by Appletia..
iewer
command.
D:1test>javac [Link]
D:test> Appletviewer [Link]
LApplet Viewer:FirstApplet
Applet wwwwwww
Applet started.
import [Link].";
import [Link].
g)
public void paint(Graphics
[Link]("Welcome to
Java Applet",50,30);
Output
Applet started.
Inside the applet, you read the values passed through the PARAM tags with the
getParameter() method ofthe Applet class.
The program below demonstrates this idea-
Example 1.5.1 Hoe can parameters be passed toan applet ? Write an applet to accept user name
intheform ofparameter and print "Hellousername>
Solution:
import [Link];
import [Link];
applet code="WelcomeParam" width=200 height=200>
Sparam name-"Usemame' value='Parth'
s/applet
String msg
public void init()
msg getParameter("Username");
"Welcome"
+ msg
msg=
paint(Graphics g)
public void
g . d r a w S t r i n g ( m s g , 5 0 , 5 0 ) ;
Output
App
Applet
Welcome Parth
Applet started
tagand
Example1.5.2 applet progran that accepts trw0input,sriigs usingParam>
Write an
concatenate the strings anddisplay it in
statusS window.
Solution: [Link]
import [Link]."
import [Link].";
String s1 = [Link]('str1");
String s2 = [Link]('str2");
String s3;
TECHNICAL PUBLICATIONS an
up-thrust for knowledge
Advanced JAVA Programming 1- 13
Applet
s3=s1+52;
showStatus(s3);
Output
FirstSecond
Example 1.5.3 Design an applet which accepts username as a parameter för html page and
display number of characters fron it.
String str =
[Link]('uname");
int len;
len=[Link]();
Applet
Applet started.
Example 1.5.4 How to pass parameter to an applet? Write an appletfo accept AccountNo and
balance in form of parameter and print message "loru balance if the balance is less than 500
Solution:
import [Link]."
import [Link].";
balance
t
[Link]([Link]("Balance");
=
Applet
Low Balance
Applet started.
Review Questionis
with suitable example.
1. Explain <PARAM> Tag ofapplet
with an example:
2. Explain following methods for applet
1) Passing parameter to applet
two directories.
Following applet program shows the demonstration of these
Java Program
[Link].*;
import [Link].
import [Link].";
/getDocumentBase Method
URL appletDocDir = getDocumentBase():
Output
Applet started.
[Link]
.Using add method we can add the button control on the applet window. Following
how to add button to the applet.
program shows
Example Program
import [Link].":
import [Link].";
import [Link].";
/
code="Button1" width=350 height=200>
<applet
</applet>
add(button);
Output
Applet Viewer Bu
Applet
Click e
Applet started.
Example Program
import [Link].
import [Link].";
import [Link].";
add(tf);
Output
Applet
Applet started.
Java Program|[Link]]
import [Link].";
import [Link].";
TECHNICAL PUBLICATIONS an
up-thrust for knowledge
Advanced JAVA Programming
1-19 APP et
[Link]('Pune");
[Link]( Mumbai");
[Link]('Delhi");
[Link](Kolkata");
add(city):
Output
Applet
Mumbai
Pune
Mumbai
Delhi
Kolkata
wwwwwww
Applet started.
1.7.4 Checkboxes
The Checkbox class is used to create check boxes. It is used to tum an option true or
false. Following program shows the use of checkbox.
Example Program
import [Link].
[Link].";
import [Link].";
capplet code="CheckBoxDemo" width=350 height=200>
</applet
public class CheckBoxDemo extends Applet
TECHNICAL PUBLICATIONS an
up-thrust for knowledge
4 i e r c e oJAVA Programming
1-20 Applet
cb1=new
Checkbox("Adhar Card",true);
Checkbox
cb2=new
Checkbox("'Pan Card')
Checkbox
add(cb1);
add(cb2);
Output
X
Applet Viewer CheckB.
Applet
Card
Adhar Card Pan
*************************
Applet started.
TECHNICAL PUBLICATIONS- an
up-thrust for knowledge