Download presentation
Presentation is loading. Please wait.
Published byEmory Lawson Modified over 9 years ago
1
Applets, Images, and Audio Chapter 14 CSCI 1302
2
CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start Method –The stop Method –The destroy method The JApplet Class The HTML File and the tag (cont.)
3
CSCI 1302 – Applets, Images, and Audio3 Outline The HTML File and the tag –Viewing Applets Using the Applet Viewer Utility –Viewing Applets from a Web Browser Passing Strings to Applets Enabling Applets to Run as Applications Case Study: TicTacToe Case Study: Bouncing Ball Displaying Images
4
CSCI 1302 – Applets, Images, and Audio4 Outline Case Study: The ImageViewer Component Case Study: Image Animations Playing Audio Pluggable Look-and-Feel
5
CSCI 1302 – Applets, Images, and Audio5 Introduction Provide a way to interact with users using Java through the web Differ slightly from standalone applications Applets do not require a main method, however, all the techniques you have learned up to now still apply
6
CSCI 1302 – Applets, Images, and Audio6 The Applet Class Provides the essential framework that enables browsers to run applets Applets rely on the browser invoking the methods init(), start(), stop(), and destroy() An instance of the applet is created using the no-arg constructor when the page is loaded See TemplateApplet.java
7
CSCI 1302 – Applets, Images, and Audio7 The Applet Class
8
CSCI 1302 – Applets, Images, and Audio8 The init Method Invoked after the applet is created or recreated Typically used to create new threads, load images, set up user-interface components, and getting string parameter values from the HTML page Should be implemented if initialization is needed by the applet
9
CSCI 1302 – Applets, Images, and Audio9 The start Method Invoked after the init method Also called whenever the page containing the applet becomes active again Should be used for operations that need to be performed whenever the page is visited
10
CSCI 1302 – Applets, Images, and Audio10 The stop Method Invoked when the user leaves the page containing the applet Should be used to suspend currently running threads to free system resources
11
CSCI 1302 – Applets, Images, and Audio11 The destroy Method Invoked when the browser notifies the applet that the browser is exiting Should be used to release resources, etc. The stop method is always called before destroy
12
CSCI 1302 – Applets, Images, and Audio12 The JApplet Class Inherits from Applet, but is able to use Swing components Add components in the same manner as adding to a JFrame See WelcomeApplet.java Cannot be run standalone, must be run in the context of an HTML page which makes it visible by default
13
CSCI 1302 – Applets, Images, and Audio13 HTML and the Tag HTML is a markup language used to present documents on the web Uses tags to instruct browsers how to render pages or perform other actions incorporates Java applets into web pages The code, width, and height tags are required
14
CSCI 1302 – Applets, Images, and Audio14 HTML and the Tag See optional tags on pg. 502 –codebase – Base directory for classes –archive – Packaged jar file of classes to be used –vspace and hspace – Padding around applet –align – Aligns the applet in the browser –alt – Text displayed if the browser doesn’t run Java See welcome.htmlwelcome.html
15
CSCI 1302 – Applets, Images, and Audio15 Using the Applet Viewer Utility Java provides a utility that allows you to test applets without using a browser From the command line, issue the appletviewer command on the html file with your applet
16
CSCI 1302 – Applets, Images, and Audio16 Viewing Applets from a Browser To view an applet in a browser, open the html file in your favorite browser The html and class file can also be moved to a web server and served remotely See TVApplet.java
17
CSCI 1302 – Applets, Images, and Audio17 Passing Strings to Applets Parameters can be declared in the HTML file, then read by the applet In the HTML file, use the tag between the opening and closing applet tag Use the getParameter method in the Java code See DisplayMessage.java
18
CSCI 1302 – Applets, Images, and Audio18 Running Applets as Applications Both JFrame and JApplet are subclasses of Container All user-interface components, layout managers, and event-handling features are the same Applications are run from their main methods, browsers create an instance of the applet and control it through init, start, stop, and destroy
19
CSCI 1302 – Applets, Images, and Audio19 Running Applets as Applications Applets have security restrictions: –Not allowed to read from, or write to, the file system of the computer –Not allowed to run programs on the browser’s computer –Not allowed to establish connections between the user’s computer and any other computer, except the server on which the applet is stored
20
CSCI 1302 – Applets, Images, and Audio20 Running Applets as Applications Generally, applets can be converted to applications with no loss of functionality Must not violate the security restrictions imposed on applets Can implement a main method in the applet to enable it to run as an application See pg. 509 and DisplayMessageApp.java
21
CSCI 1302 – Applets, Images, and Audio21 Case Study: TicTacToe See TicTacToe.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.