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 Welcome Java Applet 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 Welcome Java Applet WelcomeApplet Run Applet Viewer
Applet vs application WelcomeApplet java: Applet for displaying a message import javax. swing. X public class WelcomeApplet extends JApplet /*大工 nitia1 ize the app1et public void init() add(new JLabel (" Welcome to Java JLabel center))i import javax. swIng.大 public class My FrameWithlable public static void main(String[] args)i JFrame frame new JFrame("My FrameWithLable")i frame. add (new JLabel(" Welcome to Java", Jlabel center))i frame setsize(400, 300) frame setvisible(true)i frame setDefaultCloseOperation (JFrame EXIT ON CLOSE)i frame. setLocationRelativeTo(null);// New since JDK 1.4 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 6 Applet vs. Application import javax.swing.*; public class MyFrameWithlable{ public static void main(String[] args) { JFrame frame = new JFrame("MyFrameWithLable"); frame.add(new JLabel("Welcome to Java", JLabel.CENTER)); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); // New since JDK 1.4 } } // 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)); } }
The Applet Class, cont Applets are created, run, and destroyed by web browser Dont set size for an applet: determined by html file Dont set title for an applet: applets cannot have title bars Can have menus No need to explicitly construct an applet. Construction code placed inside the init method There is no main method An applet cannot be closed. It terminates automatically when the browser No need to call method show An applet is displayed automatically 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 7 The Applet Class, cont. Applets are created, run, and destroyed by web browser – Don’t set size for an applet: determined by HTML file. – Don’t set title for an applet: applets cannot have title bars. Can have menus. – No need to explicitly construct an applet. Construction code placed inside the init method. – There is no main method. – An applet cannot be closed. It terminates automatically when the browser exit. – No need to call method show. An applet is displayed automatically
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 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 pI roper 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 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
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 public MyApplet ()( /** Called by the browser after the applet is loaded public void init() /* Called by the browser after the init() method 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( /* Called by the browser when the Web browser exits * publi d destroy() t /* other methods if necessary
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 9 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... */ }
Browser Calling applet methods oade JVM loads the reload enters web page applet class Browser creates the applet Created B invokes initO aftcr inil Initialized Browser BI rowser y return to pa invokes start invokes stop start k slop!) invokes stop Started Stopped eo to exits another page Browser Browser invokes destroy() Destroyed 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 10 Browser Calling Applet Methods Browser invokes start() Destroyed Browser invokes destroy() Browser invokes stop() Loaded Initialized Browser invokes init() Started Stopped Created Browser creates the applet JVM loads the applet class Browser invokes stop() Browser invokes start()