Professional Documents
Culture Documents
What is an Applet?
l O It
is a special type of program that is
embedded in the webpage to generate the
dynamic content.!
l O It runs inside the browser and works at
client side.!
l O Applet is mostly used in games and
animation.!
2
1
Advantages &
Disadvantages
Advantages
O !It works at client side so less response time.
O !Secured.
O !It can be executed by browsers running under many
platforms, including Linux, Windows, Mac, etc.
Disadvantages
O !Plugin is required at client browser to execute
applet.
Hierarchy of Applet
Object
Component
Container
Panel
Applet
JApplet
2
Life Cycle of an Applet
Applet is initialized
Applet is started
Applet is painted
Applet is stopped
Applet is destroyed
3
Life Cycle Methods for
Applet
(java.awt.Graphics class)
Method Description
public void paint(Graphics g) Used to paint the Applet. It
provides Graphics class
object that can be used for
drawing oval, rectangle, arc
etc.
O By html file
- To execute the applet by html file, create an
applet and compile it.
- After that create an html file and place the
applet code in html file.
- Now click the html file.
4
How to run an Applet? (2)
O By appletviewer tool
- To execute the applet by appletviewer
tool, create an applet that contains an
applet tag in comment and compile it.
- After that run it by: appletviewer
myapplet.java in the command
line.
Example
10
5
Output
11
Displaying Graphics in
Applet
(java.awt.Graphics class)
Method Description
public abstract void Used to draw the specified
drawString(String str, int x, int y) string.
6
Method Description
public abstract void Used to fill oval with the
fillOval(int x, int y, int default color and
width, int height) specified width and
height.
public abstract void Used to draw line between
drawLine(int x1, int y1, int the points(x1, y1) and (x2,
x2, int y2) y2).
Method Description
public abstract void fillArc(int Used to fill a circular or
x, int y, int width, int height, elliptical arc.
int startAngle, int arcAngle)
7
Example
15
Output
16
8
Displaying Image in Applet
Class Method Description
java.awt.Graphics public abstract Used to draw
boolean the specified
drawImage(Ima image.
ge img, int x,
int y,
ImageObserver
observer)
java.applet.Applet public Image Used to return
getImage(URL the
u, String object of Image.
image) 17
9
Example
19
Output
20
10
Animation in Applet
21
EventHandling in Applet
22
11
Output
23
Painting in Applet
24
12
Output
25
Parameter in Applet
26
13
Output
27
Applet Communication
28
14
Output
29
15