Chapter 16 Applets and multimedia Chapter 12 GUI Basics §10.2,“ Abstract Classes," in Chapter10 Chapter 13 Graphics Chapter 14 Event-Driven programming $10.4, "Inter faces, in Chapter 10 Chapter 15 Creating User Inter faces Chapter 16 Applets and Multimedia Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 1 Chapter 16 Applets and Multimedia Chapter 12 GUI Basics Chapter 14 Event-Driven Programming Chapter 15 Creating User Interfaces §1 0.2, “Abstract Classes,” in Chapter 1 0 Chapter 13 Graphics Chapter 16 Applets and Multimedia §1 0.4, “Interfaces,” in Chapter 1 0
Objectives To explain how the Web browser controls and executes applets($ 15.2) To describe the init, start, stop, and destroy methods in the applet class (s 15.2) To know how to embed applets in Web pages(8 15.4 To run applets from appletviewer and from Web browsers($ 15.4) To pass string values to applets from HTML(8 15.5) To write a Java program that can run as both an application and an applet(s 156) To get image files using the uRl class and display images in the panel (8 15.9 Optional) To develop a reusable component Image Viewer to display images($ 15.10 Optional) To get audio files and play sound(s 15 12 Optional) To package and deploy Java projects using Java archive files(8 15.13 Optional) To use Swing pluggable look-and-feel (8 15.14 Optional) Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 2 Objectives To explain how the Web browser controls and executes applets (§ 15.2). To describe the init, start, stop, and destroy methods in the Applet class (§ 15.2). To know how to embed applets in Web pages (§ 15.4). To run applets from appletviewer and from Web browsers (§ 15.4). To pass string values to applets from HTML (§ 15.5). To write a Java program that can run as both an application and an applet (§ 15.6). To get image files using the URL class and display images in the panel (§ 15.9 Optional). To develop a reusable component ImageViewer to display images (§ 15.10 Optional). To get audio files and play sound (§ 15.12 Optional). To package and deploy Java projects using Java archive files (§ 15.13 Optional). To use Swing pluggable look-and-feel (§ 15.14 Optional)
The JApplet Class The applet class is an awt class and is not designed to work with Swing components. To use Swing components in Java applets, it is necessary to create a Java applet that extends javax. swing JApplet, which is a subclass of java applet applet. JApplet inherits all the methods from the applet class. In addition, it provides support for laying out Swing components Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 3 The JApplet Class The Applet class is an AWT class and is not designed to work with Swing components. To use Swing components in Java applets, it is necessary to create a Java applet that extends javax.swing.JApplet, which is a subclass of java.applet.Applet. JApplet inherits all the methods from the Applet class. In addition, it provides support for laying out Swing components
First Simple applet //WelcomeApplet java: Applet for displaying a message import javax. swing. *i public class WelcomeApplet extends JApplet I /* Initialize the applet * public void init() add (new JLabel(" Welcome to Java, JLabel center))i WelcomeApplet java: Applet for displaying a message import javax. swing. X public class WelcomeApplet extends JApplet i /*大工 ntia1 ize the applet*/ public WelcomeApplet () add (new jlabel(" Welcome to Java" jlabel center))i Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 4 First Simple Applet // WelcomeApplet.java: Applet for displaying a message import javax.swing.*; public class WelcomeApplet extends JApplet { /** Initialize the applet */ public WelcomeApplet() { add(new JLabel("Welcome to Java", JLabel.CENTER)); } } // WelcomeApplet.java: Applet for displaying a message import javax.swing.*; public class WelcomeApplet extends JApplet { /** Initialize the applet */ public void init() { add(new JLabel("Welcome to Java", JLabel.CENTER)); } }
First Simple applet <html> <head> <title>Welcome Java Applet</title> </head> <body> <applet code =WelcomeApplet class width =350 height =200> </applet> </body </html> WelcomeApplet Run Applet Viewer Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 5 First Simple Applet <html> <head> <title>Welcome Java Applet</title> </head> <body> <applet code = "WelcomeApplet.class" width = 350 height = 200> </applet> </body> </html> WelcomeApplet Run Applet Viewer