1 / 29

Chapter 9

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.

mercia
Télécharger la présentation

Chapter 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 Applets DESIGNED BY K PRAKASH,9985852216

  2. 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. Overview of Applet DESIGNED BY K PRAKASH,9985852216

  4. 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

  5. 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. Applet Code • Java applet code uses two classes: • Applet • Graphics DESIGNED BY K PRAKASH,9985852216

  7. 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. 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. 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

  10. 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

  11. An Applet - Life cycle DESIGNED BY K PRAKASH,9985852216

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Applet Tag - contd., • The minimum required attributes are: <APPLET CODE = AppletFileName.class WIDTH = pixels HEIGHT = pixels > </APPLET> DESIGNED BY K PRAKASH,9985852216

  21. Attributes - Description DESIGNED BY K PRAKASH,9985852216

  22. Attributes - Description DESIGNED BY K PRAKASH,9985852216

  23. 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. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

More Related