Chapter 14 Applets Images and audio Prerequisites for part工工工 hapter 8 Inheritance and polymorphism Chapter 9 Abstract Classes and Interfaces Chapter 11 Getting Started with GUI Programmin Chapter 12 Event-Driven Programming 愿生如夏花之绚烂,死如秋叶之静美-泰戈尔 Chapter 13 Creating User Interfaces hapter 14 Applets, Images, Audio Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 14 Applets, Images, and Audio Prerequisites for Part III Chapter 11 Getting Started with GUI Programming Chapter 8 Inheritance and Polymorphism Chapter 13 Creating User Interfaces Chapter 14 Applets, Images, Audio Chapter 9 Abstract Classes and Interfaces Chapter 12 Event-Driven Programming 愿生如夏花之绚烂,死如秋叶之静美---- 泰戈尔
Objectives To explain how the Web browser controls and executes applets(s 14.2) To describe the init, start, stop, and destroy methods in the applet class ( s 142) To know how to embed applets in Web pages(s 14.4) To run applets from appletviewer and from Web browsers(s 14.4) To pass string values to applets from htMl(8 14.5) To write a Java program that can run as both an application and an applet( s 146) To get image files using the uRl class and display images in the panel( s 14.oPtional) To develop a reusable component Image Viewer to display images(s 14.10 Optional To get audio files and play sound(s 14 12 Optional) To package and deploy Java projects using Java archive files (s 14.13 Optional) o To use Swing pluggable look-and-feel (8 14 14 Optional) Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To explain how the Web browser controls and executes applets (§ 14.2). ⚫ To describe the init, start, stop, and destroy methods in the Applet class (§ 14.2). ⚫ To know how to embed applets in Web pages (§ 14.4). ⚫ To run applets from appletviewer and from Web browsers (§ 14.4). ⚫ To pass string values to applets from HTML (§ 14.5). ⚫ To write a Java program that can run as both an application and an applet (§ 14.6). ⚫ To get image files using the URL class and display images in the panel (§ 14.9 Optional). ⚫ To develop a reusable component ImageViewer to display images (§ 14.10 Optional). ⚫ To get audio files and play sound (§ 14.12 Optional). ⚫ To package and deploy Java projects using Java archive files (§ 14.13 Optional). ⚫ To use Swing pluggable look-and-feel (§ 14.14 Optional)
Introduction pple PRogram that runs in Applet Container e appletviewer(test utility for applets) Web browser (IE, Communicator) OExecutes when HTML(Hypertext Markup Language) document containing applet is opened and downloaded O Applications run in command windows Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Introduction ⚫Applet Program that runs in Applet Container ⚫appletviewer (test utility for applets) ⚫Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing applet is opened and downloaded Applications run in command windows
Launching applets o Applications are http://somelocation/file.html started by a call to the main method HTML file ● Applets have no main method o Class file referenced in Applet Classes the html document Locationhttp://somelocation/file.html Browser loads and Browser runs the applet OJDK includes the Loading. appletviewer application Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai -kaiyu 4 Launching applets ⚫ Applications are started by a call to the main method ⚫ Applets have no main method ⚫ Class file referenced in the HTML document ⚫ Browser loads and runs the Applet ⚫ JDK includes the appletviewer application
The TicTacToe Applet ● You start as player"ⅹ" Fig 3.2 Sample execution of the TicTactoe applet Ap回冈國Ap回囻Ap图匾Ap回冈國Ap□回区 Applet Applet 又。 XI XOX XOX X。 Applet started Applet started Applet started Applet started Applet started Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 The TicTacToe Applet ⚫You start as player "X" Fig. 3.2 Sample execution of the TicTacToe applet
The Draw Test Applet Fig 3. 4 mple execution of applet Drawfest Applet Viewer: DrawTest class 口区 Radio Combo button Box Cc回Rots Applet started
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 The DrawTest Applet Fig. 3.4 Sample execution of applet DrawTest. Combo Box Radio button
The Applet class public class MyApplet extends java applet Applet i /* The no-arg constructor is called by the browser when the web page containing this applet is initially loaded, or reloaded ublic Myappleto i /** Called by the browser after the applet is loaded public void inito /* Called by the browser after the inito method, or every time the web page is visited public void start t /* Called by the browser when the page containing this applet becomes inactive public void stop i /* Called by the browser when the web browser exits */ public void destroyo t /* Other methods if necessary .*/
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 The Applet Class public class MyApplet extends java.applet.Applet { ... /** The no-arg constructor is called by the browser when the Web page containing this applet is initially loaded, or reloaded */ public MyApplet() { ... } /** Called by the browser after the applet is loaded */ public void init() { ... } /** Called by the browser after the init() method, or every time the Web page is visited */ public void start() { ... } /** Called by the browser when the page containing this applet becomes inactive */ public void stop() { ... } /** Called by the browser when the Web browser exits */ public void destroy() { ... } /** Other methods if necessary... */ }
The Applet class cont when the applet is loaded the Web browser creates an instance of the applet by invoking the applets no-arg constructor o The browser uses the init, start, stop, and destroy methods to control the applet. By default, these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code properly See figure14.1(a)in textbook Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 The Applet Class, cont. ⚫When the applet is loaded, the Web browser creates an instance of the applet by invoking the applet’s no-arg constructor. ⚫ The browser uses the init, start, stop, and destroy methods to control the applet. By default, these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code properly. See figure14.1 (a) in textbook
11.4.2 Applet lifecycle methods o initO, start, stop), destroy The browser calls this method first initO is called when the applet is loaded User pages back The init() method to your page ompletes and thestarto start is called by method excutes. The start User maximizes method calls the paint() init. and when the start() applet executes user returns to the user interacts aint( with applet and methods that page handles use events execute stope is called when bepaint is el the User pages to another page the user leaves the ca to the spds d User minimizes browser method and then finally to the paint() stop) page destroyo is called destroy() when the browser is closed Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 11.4.2 Applet lifecycle methods ⚫ init(), start(), stop(), destroy() ⚫ init() is called when the applet is loaded ⚫ start() is called by init, and when the user returns to the page ⚫ stop() is called when the user leaves the page ⚫ destroy() is called when the browser is closed
Browser Calling Applet Methods - Loaded JVMloads the B applet ci the applet Created B rowser invokes initO Initialized Browser Browser invokes start invokes stop Started Stopped Browser invokes stop Destroyed)destroye nokes B Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Browser Calling Applet Methods Loaded Initialized Browser invokes init() Started Stopped Destroyed Created Browser creates the applet JVM loads the applet class Browser invokes stop() Browser invokes start() Browser invokes stop() Browser invokes destroy()