You are on page 1of 15

Java Applet

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

Life Cycle Methods for


Applet
(java.applet.Applet class)
Method Description
public void init() Used to initialized the Applet. It
is invoked only once.
public void start() Invoked after the init() method
or browser is maximized. It is
used to start the Applet.

public void stop() Used to stop the Applet. It is


invoked when Applet is stop or
browser is minimized.
public void destroy() Used to destroy the Applet. It is
invoked only once.
6

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.

How to run an Applet?

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.

public void drawRect(int x, int y, Draws a rectangle with the


int width, int height) specified width and height.
public abstract void fillRect(int x, Used to fill rectangle with the
int y, int width, int height) default color and specified
width and height.
public abstract void Used to draw oval with the
drawOval(int x, int y, int width, specified width and height.
int height)
12

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).

public abstract void Used draw a circular


drawArc(int x, int y, int or elliptical arc.
width, int height, int
13
startAngle, int arcAngle)

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)

public abstract void Used to set the graphics


setColor(Color c) current color to the specified
color.

public abstract void Used to set the graphics


setFont(Font font) current font to the specified
font.
14

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

Displaying Image in Applet (2)

Class Method Description


java.applet.Applet public URL Used to return the
getDocumentBase URL of the
( document in
) which the applet
is embedded.

java.applet.Applet public URL Used to return


getCodeBase() the base URL.18

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

You might also like