290 likes | 544 Views
Chapter 9. Applets. 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. Overview of Applet. Applet - Introduction.
E N D
Chapter 9 Applets DESIGNED BY K PRAKASH,9985852216
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 DESIGNED BY K PRAKASH,9985852216
Overview of Applet DESIGNED BY K PRAKASH,9985852216
Applet - Introduction • Applets are small java applications used in internet • Downloaded from the internet • Executed using the applet vieweror 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. DESIGNED BY K PRAKASH,9985852216
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. DESIGNED BY K PRAKASH,9985852216
Applet Code • Java applet code uses two classes: • Applet • Graphics DESIGNED BY K PRAKASH,9985852216
Applet Code - contd., • Some of the methods of 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. DESIGNED BY K PRAKASH,9985852216
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) DESIGNED BY K PRAKASH,9985852216
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) { ………………… …………………… } …………….. ………………. } DESIGNED BY K PRAKASH,9985852216
Applet Code - For example import java.awt.*; import java.applet.*; public class exmp extends Applet { public void paint (Graphics g) { g.drawString(“Welcome”, 10, 100); } } In an applet program the applet code must be saved with the file name “filename.java” in a java subdirectory. DESIGNED BY K PRAKASH,9985852216
An Applet - Life cycle DESIGNED BY K PRAKASH,9985852216
Born State • In java, 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() DESIGNED BY K PRAKASH,9985852216
Born Initialization start ( ) stop ( ) Running Idle Stopped display start ( ) destroy () Dead End Load Applet paint () Life cycle of an Applet DESIGNED BY K PRAKASH,9985852216
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() DESIGNED BY K PRAKASH,9985852216
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. DESIGNED BY K PRAKASH,9985852216
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. DESIGNED BY K PRAKASH,9985852216
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) ……………………. } public void destroy() { ………………….. ……………………. (code) ……………………. } General format of destroy() The stop ( ) method must be called atleast one time or it can be called multiple times in the applet life cycle. The destroy () method can be called only once in the applet life cycle. DESIGNED BY K PRAKASH,9985852216
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. DESIGNED BY K PRAKASH,9985852216
Applet Tag - Syntax <APPLET [ CODEBASE = codebase_URL] CODE = AppletFileName.class [ ALT = alternate_text ] [ NAME = applet_instance_name] WIDTH = pixels HEIGHT = pixels [ ALIGN = alignment ] [ VSPACE = pixels ] [ HSPACE = pixels ] > [ < PARAM NAME = attribute1 VALUE = value1 > ] [ < PARAM NAME = attribute2 VALUE = value2 > ] ……………………. ……………………. </APPLET> These attributes are optional that can be used when integrating an appletinto a web page. DESIGNED BY K PRAKASH,9985852216
Applet Tag - contd., • The minimum required attributes are: <APPLET CODE = AppletFileName.class WIDTH = pixels HEIGHT = pixels > </APPLET> DESIGNED BY K PRAKASH,9985852216
Attributes - Description DESIGNED BY K PRAKASH,9985852216
Attributes - Description DESIGNED BY K PRAKASH,9985852216
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. DESIGNED BY K PRAKASH,9985852216
Applet viewer : Welcome.class Welcome For Example <APPLET CODE = welcome.class WIDTH = 400 HEIGHT = 200 > </APPLET> Output of the Applet Program DESIGNED BY K PRAKASH,9985852216
Web Page Designing • Require to run the applet • 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>. DESIGNED BY K PRAKASH,9985852216
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 DESIGNED BY K PRAKASH,9985852216
Head Section • Contain title for a web page • Start with a <HEAD> tag and must end with a </HEAD> tag • The <TITLE>…..</TITLE> tag displays the text in the title bar of the web browser when it displays the page. • This head section is also optional. <HEAD> <TITLE>WELCOME TO JAVA </TITLE> </HEAD> DESIGNED BY K PRAKASH,9985852216
Body Section • Contains the entire information about the web page and its behaviour <BODY> <CENTER> <H1> WELCOME TO JAVA </H1> </CENTER> <BR> <APPLET……..> </APPLET> </BODY> The above program displays the message: WELCOME TO JAVA DESIGNED BY K PRAKASH,9985852216
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 DESIGNED BY K PRAKASH,9985852216