Download presentation
Presentation is loading. Please wait.
Published byAntony Bailey Modified over 8 years ago
1
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,9985852216
2
2 CSC Computer Education (P) Ltd. 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
3
3 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,9985852216
4
4 CSC Computer Education (P) Ltd. 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. DESIGNED BY K PRAKASH,9985852216
5
5 CSC Computer Education (P) Ltd. 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
6
6 CSC Computer Education (P) Ltd. Applet Code Java applet code uses two classes: Applet Graphics DESIGNED BY K PRAKASH,9985852216
7
7 CSC Computer Education (P) Ltd. 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
8
8 CSC Computer Education (P) Ltd. 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
9
9 CSC Computer Education (P) Ltd. 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) { ………………… …………………… } …………….. ………………. } import java.awt.*; import java.applet.*; ……………… ………………. public class appletclassname extends Applet { ………………… ………………. public void paint (Graphics g) { ………………… …………………… } …………….. ………………. } DESIGNED BY K PRAKASH,9985852216
10
10 CSC Computer Education (P) Ltd. 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); } 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
11
11 CSC Computer Education (P) Ltd. An Applet - Life cycle Born state Runnin g state Display state Idle state Dead state DESIGNED BY K PRAKASH,9985852216
12
12 CSC Computer Education (P) Ltd. 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) ……………………. } public void init ( ) { ………………….. ……………………. (code) ……………………. } General format of init() DESIGNED BY K PRAKASH,9985852216
13
13 CSC Computer Education (P) Ltd. Born Dead Running stop ( ) start ( ) display destroy () End Initialization start ( ) Stopped Idle Load Applet paint () Life cycle of an Applet DESIGNED BY K PRAKASH,9985852216
14
14 CSC Computer Education (P) Ltd. Running State The applet enters the running state, when the system calls the start ( ) of applet class. public void start( ) { ………………….. ……………………. (code) ……………………. } public void start( ) { ………………….. ……………………. (code) ……………………. } General format of start() DESIGNED BY K PRAKASH,9985852216
15
15 CSC Computer Education (P) Ltd. Display State It performs some output operation on the screen. This state occurs by calling paint ( ) of applet class. The paint () method can occur several times in the applet life cycle. public void paint(Graphics g) { ………………….. ……………………. (display) ……………………. } public void paint(Graphics g) { ………………….. ……………………. (display) ……………………. } General format of paint() DESIGNED BY K PRAKASH,9985852216
16
16 CSC Computer Education (P) Ltd. Idle State This state occurs by calling stop ( ) of applet class. An applet becomes idle when an applet class is stopped from running. The stop ( ) method must be called atleast one time or it can be called multiple times in the applet life cycle. public void stop() { ………………….. ……………………. (code) ……………………. } public void stop() { ………………….. ……………………. (code) ……………………. } General format of stop() DESIGNED BY K PRAKASH,9985852216
17
17 CSC Computer Education (P) Ltd. Dead State This state occurs by calling destroy ( ) of applet class. The method destroy () is called when an applet is removed from memory. The stop ( ) method must be called atleast one time or it can be called multiple times in the applet life cycle. public void destroy() { ………………….. ……………………. (code) ……………………. } public void destroy() { ………………….. ……………………. (code) ……………………. } General format of destroy() The destroy () method can be called only once in the applet life cycle. public void destroy() { ………………….. ……………………. (code) ……………………. } public void destroy() { ………………….. ……………………. (code) ……………………. } DESIGNED BY K PRAKASH,9985852216
18
18 CSC Computer Education (P) Ltd. Applet Tag The start tag of applet that indicates the name of the applet to be loaded and how much that space the applet requires. The end tag of applet denotes with slash(/) that indicates the applet tag is closed. DESIGNED BY K PRAKASH,9985852216
19
19 CSC Computer Education (P) Ltd. 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 ] > [ ] ……………………. <APPLET [ CODEBASE=codebase_URL] CODE=AppletFileName.class [ ALT=alternate_text ] [ NAME=applet_instance_name] WIDTH=pixels HEIGHT=pixels [ ALIGN=alignment ] [ VSPACE=pixels ] [ HSPACE=pixels ] > [ ] ……………………. These attributes are optional that can be used when integrating an applet into a web page. DESIGNED BY K PRAKASH,9985852216
20
20 CSC Computer Education (P) Ltd. Applet Tag - contd., The minimum required attributes are: <APPLET CODE = AppletFileName.class WIDTH = pixels HEIGHT = pixels > <APPLET CODE = AppletFileName.class WIDTH = pixels HEIGHT = pixels > DESIGNED BY K PRAKASH,9985852216
21
21 CSC Computer Education (P) Ltd. Attributes - Description AttributesDescription CODEBASE = codebase_URL It specifies the base url of the object. It is an Optional attribute CODE = AppletFileName.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. DESIGNED BY K PRAKASH,9985852216
22
22 CSC Computer Education (P) Ltd. Attributes - Description AttributesDescription WIDTH = pixels This Required attribute specifies the height and width of the applet to be displayed on the page. HEIGHT = pixels ALIGN = alignment 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 VSPACE = pixels These Optional attributes are used to specify the number of pixels above and below (VSPACE) and left and right (HSPACE) of the applet. HSPACE = pixels DESIGNED BY K PRAKASH,9985852216
23
23 CSC Computer Education (P) Ltd. 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
24
24 CSC Computer Education (P) Ltd. For Example <APPLET CODE = welcome.class WIDTH = 400 HEIGHT = 200 > <APPLET CODE = welcome.class WIDTH = 400 HEIGHT = 200 > Applet viewer : Welcome.class Welcome Output of the Applet Program DESIGNED BY K PRAKASH,9985852216
25
25 CSC Computer Education (P) Ltd. 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 and closing HTML tag. DESIGNED BY K PRAKASH,9985852216
26
26 CSC Computer Education (P) Ltd. Comment Section Tells what is going on in the web page In a web page, a comment line must begins with a This comment section is optional Included anywhere in the web page DESIGNED BY K PRAKASH,9985852216
27
27 CSC Computer Education (P) Ltd. Head Section Contain title for a web page Start with a tag and must end with a tag The ….. tag displays the text in the title bar of the web browser when it displays the page. This head section is also optional. WELCOME TO JAVA WELCOME TO JAVA DESIGNED BY K PRAKASH,9985852216
28
28 CSC Computer Education (P) Ltd. Body Section Contains the entire information about the web page and its behaviour WELCOME TO JAVA WELCOME TO JAVA The above program displays the message: WELCOME TO JAVA DESIGNED BY K PRAKASH,9985852216
29
29 CSC Computer Education (P) Ltd. 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 and 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.