Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Java Applet

Sabyasachi Moitra
moitrasabyasachi@hotmail.com
What is an Applet?
O It is a special type of program that is
embedded in the webpage to generate the
dynamic content.
O It runs inside the browser and works at
client side.
O Applet is mostly used in games and
animation.

2
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.
3
Hierarchy of Applet
Object

Component

Container

Panel

Applet

JApplet
4
Life Cycle of an Applet
Applet is initialized

Applet is started

Applet is painted

Applet is stopped

Applet is destroyed

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

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

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

[NOTE: In this tutorial the second method is


being used to run an applet]

9
Example

10
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 string.
y)
public void drawRect(int x, int Draws a rectangle with the
y, int width, int height) specified width and height.
public abstract void fillRect(int Used to fill rectangle with the
x, 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
Method Description
public abstract void fillOval(int Used to fill oval with the
x, int y, int width, int height) default color and specified
width and height.
public abstract void Used to draw line between the
drawLine(int x1, int y1, int x2, points(x1, y1) and (x2, y2).
int y2)
public abstract void Used draw a circular or
drawArc(int x, int y, int width, elliptical arc.
int height, int startAngle, int
arcAngle)

13
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
Example

15
Output

16
Displaying Image in Applet
Class Method Description
java.awt.Graphics public abstract Used to draw the
boolean specified image.
drawImage(Image
img, int x, int y,
ImageObserver
observer)
java.applet.Applet public Image Used to return the
getImage(URL u, object of Image.
String 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 the
getCodeBase() base URL.

18
Example

19
Output

20
Animation in Applet

21
EventHandling in Applet

22
Output

23
Painting in Applet

24
Output

25
Parameter in Applet

26
Output

27
Applet Communication

28
Output

29
References
O Courtesy of JavaTPoint – Java Applet
Tutorial. URL:
http://www.javatpoint.com/java-applet
O Courtesy of TutorialsPoint – Java Applet
Basics. URL:
http://www.tutorialspoint.com/java/java_ap
plet_basics.htm
O Herbert Schildt, Java: The Complete
Reference, Seventh Edition, TMGH, 2007
30

You might also like