Applet Programming
Transcript of Applet Programming
-
7/25/2019 Applet Programming
1/23
Applet Programming
-
7/25/2019 Applet Programming
2/23
What is an Applet?
It is a smalljava program that isaccessed on an internet server,transported over the internet ,automatically installed and run aspart of a web document.
Applet class is contained in
java.appletpackage. All applets are subclasses of Applet
class.
-
7/25/2019 Applet Programming
3/23
Some important points
!ecutionof an applet doesnotstartwith a mainmethod on rather applethas its own life cycle..
"utput in an applet is not displayedusing println method but usinggraphic methods.
Applet is not e!ecuted using javainterpreter like a normal javaprogram. It is embedded in a web
page.
-
7/25/2019 Applet Programming
4/23
Applet output window
-
7/25/2019 Applet Programming
5/23
Applet class methods
voidinit#$ called %rst in beginning
voidstart#$called to start e!ecution
voidpaint#&raphics g$paints outputwindow
voidstop#$ called to suspend e!ecution
voiddestroy#$ called to terminateapplet
booleanisActive#$true if applet started
-
7/25/2019 Applet Programming
6/23
Applet tag
'A(()*
+"-AS/ codebase01)2
"-/ 3%le name4
+A)*/ aletrnate te!t2
+5A6/ applet name2
WI-*7/ w
7I&7*/ h
+A)I&5/ alignment2
+8S(A/ pi!els2
+7S(A/ pi!els2
9
+'(A1A65A6 / attribute name 8A)0 / value92
: : :
';A(()*9
-
7/25/2019 Applet Programming
7/23
!ample
import java.awt.
-
7/25/2019 Applet Programming
8/23
public void start#$
msg E/ 3Inside start C4=D
public void paint#&raphics g$
msg E/ 3Inside paint C4=
g.drawString#msg, F,G$=
D
D
drawString is a method of &raphics class
setackground and setBoreground are inomponent class.
showStatus#String s$ can be used to change
status shown in status window.
-
7/25/2019 Applet Programming
9/23
olor class constants
olor.gray
olor.blue
olor.black
olor.green
olor.red olor.orange
olor.pink
olor.white
olor.yellow
olor.cyan olor.magenta
olor.darkgray
olor.lightgray
-
7/25/2019 Applet Programming
10/23
!ample
import java.awt.
-
7/25/2019 Applet Programming
11/23
public void paint#&raphics g$
g.draw)ine#,, F,F@$= ;;g.draw1ect#F,F,H,@$= ;;
g.%ll1ect#F,F,H,@$= ;;
g.draw1ound1ect#F,F,H,@,F@,F@$=
g.%ll1ound1ect#J,,FK,F,G,K$=;;
D
D
-
7/25/2019 Applet Programming
12/23
Synta! of these methods
void draw)ine#int start!,int starty,int end!,intendy$=
void draw1ect#int top, int left, int width, int
height$ void %ll1ect#int top, int left, int width, int height$
void draw1ound1ect#int top, int left, int width,int height, int !diameter, int ydiameter$=
void%ll1ound1ect#int top, int left, int width, intheight, int !diameter, int ydiameter$=
if height and width are eLual, it is a sLuare.
-
7/25/2019 Applet Programming
13/23
-rawing ellipses and circles
void draw"val#int top, int left, intwidth, int height$
void %ll"val#int top, int left, int width,int height$
if height and width are eLual it iscircle, else an ellipse.
-
7/25/2019 Applet Programming
14/23
-rawing arcs and polygons
void drawArc#int top, int left, int width,int height, int startangle, int endangle$
void %llArc#int top, int left, int width,
int height, int startangle, int endangle$
-
7/25/2019 Applet Programming
15/23
-rawing (olygons void draw(olygon#int !+2, int y+2, int
num$
void %ll(olygon#int !+2, int y+2, intnum$
where ! and y are arrays of integersand num is the number of vertices inthe polygon.
-
7/25/2019 Applet Programming
16/23
(assing (arameters
import java.awt.
-
7/25/2019 Applet Programming
17/23
public void init#$
setackground#olor.green$=
setBoreground#olor.red$=
msg / get(arameter#3message4$=
! / Integer.parseInt#get(arameter#3!position4$=
y / Integer.parseInt#get(arameter#3yposition4$=
Dpublic void paint#&raphics g$
g.drawString#msg, !,y$=showstatus#3Applet (arameters is running4$=
D
D
-
7/25/2019 Applet Programming
18/23
7*6) %le
'html9
'body9
'applet code / 3(arameters.class4 width / 3>4
height / 3F4 alt / 30se Mava enabled browser4 9'param name / 3message4 value / 3Welcome toMava4 ;9
'param name / 3!position4 value / 3@4 ;9
'param name / 3yposition4 value / 3H4 ;9';applet9
';body9
';html9
-
7/25/2019 Applet Programming
19/23
*o display a moving banner
import java.awt.
-
7/25/2019 Applet Programming
20/23
public void start#$
t / new *hread#this$=
Nag / true=
t.start#$=D
public void run#$
char ch=
for# = = $
try
repaint#$=
*hread.sleep#G$=
ch / msg.charAt#$=
msg / msg.substring#F,msg.length#$$=
msg E/ ch=
if#ONag$
break=
Dcatch#Interrupted!ception e$ D
D ;; for loop ends
D ;; run method ends
-
7/25/2019 Applet Programming
21/23
public void stop#$
Nag / false=t / null=
D
public void paint#&raphics g$
g.drawString#msg,@,@$=
D
D
;; repaint#$ method is de%ned in &raphics classand gives a call to update#$ method of Appletclass which in turn calls paint#$
-
7/25/2019 Applet Programming
22/23
get-ocumentase#$ returns the directory path containing the
7*6) %le of the applet getodease#$
returns the directory path containing the
applet class %le of the applet
-
7/25/2019 Applet Programming
23/23
!ample
import java.awt.