Chapter 9 Applet
Chapter 9 Applet
Pratima Sarkar
Applet is a special type of program that is embedded in the
webpage to generate the dynamic content.
Import
java.applet.Applet
java.awt.*
Example
//First.java
import java.applet.Applet;
import java.awt.Graphics;
}
First.html
<html>
<body>
<applet
code="First.class“
width="300"
height="300">
</applet>
</body>
</html>
How to run an Applet?
Compile java file (javac prog_name.java)
Write html file.
Execute by appletviewer tool . (appletviewer
htmlfile_name.html)
Applet Types:
Local Applets
Remote Applets
Introduction
An applet developed locally and stored in local system is known as
local applet.
Interne
Local Remote
t
Computer Computer
In order to locate and load remote applet, we must know the applet
address. This address is called URL (Uniform Resource Locator). The
URL must be specified in the applet’s HTML document as the value of
CODEBASE attribute.
Eg: CODEBASE = http:// www.netserver/applets
Eg: <html>
<body>
<applet
code="First.class"
width="300"
height="300">
</applet>
</body>
</html>
Designing a webpage (A template)
<HTML>
<HEAD>
Head
Title Tag
Section
< / HEAD>
<BODY>
Body
Applet Tag Section
< / BODY>
< / HTML>
Applet Life Cycle
Begin
Born Initialization
(Load Applet)
start()
stop()
Running Idle Stopped
Display
start()
paint() destroy()
Exit of browser
Applet Life Cycle
1. Initialization State: An applet enters initialization state when it’s first
loaded. It is achieved by overriding init() method of applet class.
In this stage, we can create object needed by applet, set up initial
values, load images or fonts and set up colors.
Syntax: public void init()
{ ………….
…………..
}
2. Running State: Applet enters running state when system calls the start()
method of applet class. This occurs automatically after applet is initialised.
Syntax: public void start()
{ ………….
…………..
}
Applet Life Cycle
3. Idle or Stopped State: An applet enter idle state when it is stopped fro
running. It occurs automatically when we leave currently running applet
page. However, we can manually stop by calling stop() method.
Syntax: public void stop()
{ ………….
…………..
}
}
NumValues.html
<html>
<body>
<applet
code="NumValues.class“
width="300"
height="300">
</applet>
</body>
</html>
Example for getting user input
import java.awt.*;
import java.applet.*;
import java.awt.*;
import java.applet.*;
}
Example to draw ellipse and circles
import java.awt.*;
import java.applet.*;
}
Self study
drawArc()
drawPloygon()
Draw face: