Chapter 9 Applets CSC Computer Education PDESIGNED BY

Chapter 9 Applets CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 1

Learning Objectives In this chapter you will learn: �Overview of an applet �Applet code in java �Life cycle of an applet �What are the tags in applet �Steps for running an applet �Three sections in web page CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 2

OVERVIEW OF APPLET CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 3

Applet - Introduction �Applets are small java applications used in internet �Downloaded from the internet �Executed using the applet viewer or web browser �A java applet is delivered to the user in the form of java bytecode. In java, applets are not a stand-alone application. Java Virtual Machine executes all java programs including applets. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 4

Applet - Definition �An applet can display graphics, accept the user interface and create animation. �Java contains two varieties of applets: �java. applet �javax. swing. JApplet Contains the Applet class to provide graphical user interface. Uses the Swing class to provide the swing type of applet. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 5

Applet Code Java applet code uses two classes: Applet Graphics CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 6

Applet Code �Some of the methods-ofcontd. , Java. applet package: �init() � The main ( ) is called directly to initiate the execution of program. �start() � When an applet is loaded, it automatically calls an applet class method for start, running and stopping the applet code. �paint() � Applet class calls this method to display the result of the applet code on the screen. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 7

Applet Code - contd. , In applet class, the paint method requires a Graphics object as an argument. It is defined as: public void paint (Graphics g) CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 8

Applet Code contd. , The general format of- an applet code is: import java. awt. *; import java. applet. *; ………………. public class appletclassname extends Applet { …………………. public void paint (Graphics g) { …………………… } ……………. . ………………. } CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 9

Applet Code - For example import java. awt. *; import java. applet. *; public class exmp extends Applet { public void paint (Graphics g) { g. draw. String(“Welcome”, 100); } } In an applet program the applet code must be saved with the file name “filename. java” in a java subdirectory. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 10

An Applet - Life cycle Dead Idle state Runnin g state Display state Born state CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 11

Born �In java, State the applet is born by calling the method init(). �The following can be done at this stage: �create an object �load images �set initial values �set colors public void init ( ) { …………………. (code) …………. } General format of init() CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 12

Born Initialization Load Applet start ( ) stop ( ) Running Idle Stopped display paint () start ( ) destroy () Dead End Life cycle of an Applet CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 13

Running State The applet enters the running state, when the system calls the start ( ) of applet class. public void start( ) { …………………. (code) …………. } General format of start() CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 14

Display State It performs some output operation on the screen. This state occurs by calling paint ( ) of applet class. public void paint(Graphics g) { …………………. (display) …………. } General format of paint() The paint () method can occur several times in the applet life cycle. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 15

Idle State This state occurs by calling stop ( ) of applet class. An applet becomes idle when an applet class is stopped from running. public void stop() { …………………. (code) …………. } General format of stop() The stop ( ) method must be called atleast one time or it can be called multiple times in the applet life cycle. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 16

Dead State This state occurs by calling destroy ( ) of applet class. The method destroy () is called when an applet is removed from memory. public void destroy() { } …………………. (code) …………. General format of destroy() The stop ( ) method must be called atleast one time or it can be called The destroy () method can be called only once in the applet life cycle. multiple times in the applet life cycle. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 17

Applet Tag �<Applet…. . > �The start tag of applet that indicates the name of the applet to be loaded and how much that space the applet requires. �</Applet> �The end tag of applet denotes with slash(/) that indicates the applet tag is closed. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 18

Applet Tag - Syntax <APPLET </APPLET> [ CODEBASE = CODE = [ ALT = [ NAME = WIDTH = HEIGHT = [ ALIGN = [ VSPACE = [ HSPACE = > [ < PARAM NAME = attribute 1 [ < PARAM NAME = attribute 2 ……………………. codebase_URL] Applet. File. Name. class alternate_text ] applet_instance_name] pixels These attributes are optional that pixels can be used when integrating an alignment ] applet into a web page. pixels ] VALUE = value 1 > ] VALUE = value 2 > ] CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 19

Applet Tagrequired - contd. , The minimum attributes are: <APPLET > </APPLET> CODE = Applet. File. Name. class WIDTH = pixels HEIGHT = pixels CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 20

Attributes - Description Attributes Description CODEBASE = codebase_URL It specifies the base url of the object. It is an Optional attribute CODE = Applet. File. Name. class It is a Required attribute which specifies the url of the directory in which the applet resides. ALT = alternate_text If the browser can understand the applet tag but can’t run Java applets, the text specified for this Optional attribute will be displayed. NAME = applet_instance_name This Optional attribute is specified so that other applets on the page can refer to this applet. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 21

Attributes - Description Attributes WIDTH = pixels HEIGHT = pixels ALIGN = alignment VSPACE = pixels HSPACE = pixels Description This Required attribute specifies the height and width of the applet to be displayed on the page. This Optional attribute specifies the position of the applet on the page. Possible alignment values are: TOP, BOTTOM, LEFT, RIGHT, MIDDLE, ABSMIDDLE, ABDBOTTOM, TEXTTOP and BASELINE These Optional attributes are used to specify the number of pixels above and below (VSPACE) and left and right (HSPACE) of the applet. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 22

How to Run the Applet �Tools required to run an applet program: �Java-enabled Web browser � Views the entire web page containing the applet. �Java applet viewer � Views only the output of the applet. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 23

For Example <APPLET CODE = welcome. class Applet viewer WIDTH = 400: Welcome. class HEIGHT = 200 > </APPLET> Welcome Output of the Applet Program CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 24

�Require. Page to run the. Designing applet Web �Contains Text and HTML tags �Pages are stored using an extension. html �Includes both text and HTML �A web page is divided in to three sections. �Comment section �Head section �Body section A web page is referred by an opening <HTML> and closing HTML tag </HTML>. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 25

Comment Section �Tells what is going on in the web page �In a web page, a comment line must begins with a <! And ends with a > �This comment section is optional �Included anywhere in the web page CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 26

�Contain Section title for a web page Head �Start with a <HEAD> tag and must end with a </HEAD> tag <HEAD> �The <TITLE>…. . </TITLE> tag displays the text in the title <TITLE>WELCOME TO JAVA </TITLE> bar of the web browser when it displays the page. </HEAD> �This head section is also optional. CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 27

Body Section Contains the entire information about the web page and its behaviour <BODY> </BODY> <CENTER> <H 1> WELCOME TO JAVA </H 1> </CENTER> <BR> <APPLET……. . > </APPLET> The above program displays the message: WELCOME TO JAVA CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 28

Summary �Applet can be executed using the applet viewer or web browser. �Applet code uses two classes: Applet and Graphics �Applet states are: born state, running state, display state, idle state, dead state. �In a web page we must include a pair of <APPLET…. . > and </APPLET> tag. �To run an applet program we need one of the following tools: � Java-enabled web browser � Java applet viewer CSC Computer Education (P)DESIGNED BY K PRAKASH, 9985852216 Ltd. 29
- Slides: 29