applets, images, and audio chapter 14 csci 1302. csci 1302 – applets, images, and audio2 outline...

21
Applets, Images, and Audio Chapter 14 CSCI 1302

Upload: emory-lawson

Post on 19-Jan-2016

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

Applets, Images, and Audio

Chapter 14CSCI 1302

Page 2: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 2

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 <applet> tag

(cont.)

Page 3: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 3

Outline

• The HTML File and the <applet> 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

Page 4: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 4

Outline

• Case Study: The ImageViewer Component

• Case Study: Image Animations• Playing Audio• Pluggable Look-and-Feel

Page 5: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 5

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

Page 6: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 6

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

Page 7: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 7

The Applet Class

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()

Page 8: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 8

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

Page 9: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 9

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

Page 10: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 10

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

Page 11: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 11

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

Page 12: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 12

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

Page 13: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 13

HTML and the <applet> 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

• <applet> incorporates Java applets into web pages

• The code, width, and height tags are required

Page 14: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 14

HTML and the <applet> 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.html

Page 15: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 15

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

Page 16: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 16

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

Page 17: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 17

Passing Strings to Applets

• Parameters can be declared in the HTML file, then read by the applet

• In the HTML file, use the <param> tag between the opening and closing applet tag

• Use the getParameter method in the Java code

• See DisplayMessage.java

Page 18: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 18

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

Page 19: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 19

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

Page 20: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 20

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

Page 21: Applets, Images, and Audio Chapter 14 CSCI 1302. CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start

CSCI 1302 – Applets, Images, and Audio 21

Case Study: TicTacToe

• See TicTacToe.java