chapter 131 applets and html chapter 13. 2 objectives learn how to write applets learn to write a...

46
Chapter 13 1 Applets and HTML Chapter 13

Upload: francine-gibbs

Post on 21-Jan-2016

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 1

Applets and HTML

Chapter 13

Page 2: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 4

Introduction

• Applets are simply Java programs designed to run from a document (page) on the World Wide Web.

• HyperText Markup Language (HTML) is the language used to create Web documents.

Page 3: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 6

Introduction to Applets

• An applet is a “small application” or a “little Java program.”

• Applets are Java programs that are typically displayed on a Website and viewed over the Internet.

• An applet can also be run as a stand-alone program on a computer which is not connected to the Internet.

Page 4: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 7

Applet Basics

• An applet is a derived class of class JApplet which is a class in the Swing library.

• When writing an applet, it is a good idea to include all of the following:import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

Page 5: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 8

Applet Basics, cont.

Page 6: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 9

Applet Basics, cont.

• A JApplet is a Container, which permits you to add components to it in the same way you can add components to a JFrame.

• But, applets do not use the setVisible method or the setTitle method, nor do they need any size instructions.

• Applets are displayed automatically, and do not need to have a main method.

Page 7: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 10

Applet Basics, cont.

• Applets typically do not use constructors. Instead they use method init which serves the same purpose.

• Setting colors, adding components, etc. is done in method init.

• Method init has no parameters and is not overloaded.

• Applets do not need to be closed using listeners.

Page 8: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 11

Applet Basics, cont.

• class HelloApplet

Page 9: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 12

Applet Basics, cont.

Page 10: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 13

Running an Applet

• Applets are compiled the same way other Java classes are compiled.

• However, the normal way to run an applet is as part of a Web document.– The applet then is viewed using a Web

browser.• An applet can also be viewed using an applet

viewer which is a program designed to run an applet as a stand-alone program.

Page 11: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 14

Programming Example: An Adder Applet

• class AdderApplet

Page 12: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 15

Programming Example: An Adder Applet, cont.

Page 13: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 16

Converting a Swing Application to an Applet

• Derive the class from JApplet rather than from JFrame.

• Remove method main.• Replace the constructor(s) with a method

named init.

Page 14: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 17

Converting a Swing Application to an Applet

• Delete any invocation of addWindowListener. • Delete any invocation of setTitle.• Delete any invocation of setSize.

Page 15: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 18

Adding Icons to an Applet

• An icon typically is a small picture.• By placing the icon in a JLabel, the icon is

displayed.• A JLabel can consist of text, an icon, or both.• A JButton or JMenuItem can also have an icon.

Page 16: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 19

Adding Icons to an Applet, cont.

• class DukeApplet

Page 17: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 20

Adding Icons to an Applet, cont.

Page 18: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 21

Adding Icons to an Applet, cont.

• ImageIcon is a class in the Swing library.• syntax

ImageIcon Name_of_ImageIcon =

new ImgeIcon(Picture_File_Name);

– Picture_File_Name is a string giving either a relative or absolute path name to the picture file.

Page 19: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 23

Introduction to HTML

• Documents to be read on the Web or using a Web browser typically are expressed in a language called HTML.

• HTML stands for HyperText Markup Language.

• Hypertext contains links (or hyperlinks) which permit you to go to other documents.

Page 20: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 24

Introduction to HTML, cont.

• These documents are called pages.• HTML is not a general-purpose programming

language like Java.• Instead, it is a collection of simple (markup)

commands that can produce something that can be viewed using a Web browser.

Page 21: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 25

Introduction to HTML, cont.

• The commands allow you to include pictures and hyperlinks.

• The commands also allow you to specify headings, subheadings, paragraph beginnings, etc.

• In short, HTML is mostly a language for formatting a manuscript so that it can be viewed on the Web.

Page 22: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 26

HTML Basics

• Most HTML commands are of the form<command>

Some text

</command>

• example<h1>

My Home Page

</h1>

Page 23: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 27

HTML Basics, cont.

• Anything between <center> and </center> is centered on the page when it is displayed.

• Some commands do not need a closing command. For example

<br> begins a new line and

<p> begins a new paragraph.• The browser inserts breaks where necessary

to fit the text appropriately on the screen.

Page 24: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 28

HTML Basics, cont.

• HTML is not case sensitive. <table>, <Table>, <TABLE>, <tAbLe> are all the same…

• An HTML file is a regular text file that you create and edit with a text editor.

• HTML files should end with .html or .htm• Commands such as <table> and </table>

form a “container” (in this case a table container).

Page 25: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 29

HTML Basics, cont.

Page 26: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 30

HTML Basics, cont.

• The entire document is enclosed between <html> and </html>.

• The head of the document is enclosed between <head> and </head>.– The head contains information used by a

browser, but typically not displayed.– It might consist only of a title enclosed

between <title> and </title>, used to name the document.

Page 27: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 31

HTML Basics, cont.

• Two parts of the document are displayed on the screen.– The body is enclosed between <body> and </body> and is the real content of the document.

– The address is optional. It is enclosed between <address> and </address>. It includes the address of the document’s owner and the last date the document was modified.

Page 28: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 32

HTML Basics, cont.

Page 29: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 33

HTML Basics, cont.

Page 30: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 34

Inserting Hyperlinks

• The key active element in an HTML document is a link that the person viewing the document can click to view another HTML document.

• syntax<a href=“Path_to_Document”>

Displayed_Text_to_Click

</a>

Page 31: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 35

Inserting Hyperlinks, cont.

• example<a href=“http://www-cse.uscd.edu/users/savitch”>

Walter Savitch

</a>

• The Path_to_Document can be either a full path name or a relative path name to a HTML file or a URL to any place on the Web.

Page 32: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 36

URLs

• A URL is a kind of path name for the World Wide Web.

• URL stands for Uniform Resource Locator.• URLs are absolute path names to documents

anywhere in the world.• Relative path names can be used for

documents on your own computer.

Page 33: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 37

Displaying the Most Current Version of a Document

• While you are developing an HTML page, you can display the most recent version of the page by clicking the button labeled Reload (or perhaps Refresh).

• Otherwise, for efficiency, the browser may access an earlier copy of the page.

Page 34: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 38

Displaying a Picture

• A picture can be inserted into an HTML document using<img src=“File_with_Picture”>

• example<img src=“images/mypicture.jpg”>

• The picture can be in any directory, but the path name, either full or relative, leading to the picture must be provided.

Page 35: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 40

Placing an Applet in an HTML Document

• To display the adder window created by class AdderApplet, place the following command in an HTML document:<applet code=“AdderApplet.class” width=400 height=200>

</applet>

(Actually “.class” is optional. code=“AdderApplet” works just as well)

Page 36: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 41

Placing an Applet in an HTML Document, cont.

• This command assumes that the HTML file and the file AdderApplet.class are in the same directory.– Otherwise, a relative or absolute path

name to AdderApplet.class is needed.

Page 37: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 42

Placing an Applet in an HTML Document, cont.

Page 38: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 43

Placing an Applet in an HTML Document, cont.

Page 39: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 44

Using an Old Web Browser

• A Web browser must be set up to run applets.• Web browsers do not use the same Java

interpreter used to run Java applications.• Older Web browsers (yours or someone

else’s who may want to view your HTML document) may not be able to run applets from an HTML document.

Page 40: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 45

Using an Old Web Browser, cont.

• Furthermore, Java updates for browsers typically lag core Java language updates.

• Using the older Applet class sometimes can remedy the problem.

• These problems do not exist if you are running applets from the applet viewer using a recent version of Java.

Page 41: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 46

The Older Applet Class

• To use the older Applet class instead of the JApplet class

– remove the Js from JApplet, JButton, JLabel, etc. (that is, use Applet, Button, Label)

– use the following import statementsimport java.awt.*;

import java.awt.event.*;

import java.applet.*;

Page 42: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 47

The Older Applet Class, cont.

– you do not needimport javax.swing.*;

– add components to the applet to itself rather than using a content pane (whatever was done to the content pane of a JApplet should be done directly to the Applet).

Page 43: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 48

The Older Applet Class, cont.

– example:

substituteadd(friendlyLabel);

forgetContentPane().add(friendlyLabel);

• Furthermore, class Applet cannot accommodate icons easily.

Page 44: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 49

Applets and Security

• Your applet is a program that may be run on someone else’s computer.

• Worse, someone else’s applet might be run on your computer!

• Furthermore, you don’t know that an HTML page contains an applet until you load it into your browser, and then it is too late to reject the applet; it is already stored on your computer.

Page 45: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 50

Applets and Security, cont.

• Someone else’s program running on your computer creates serious security concerns.– Will it leave a virus?– Will it alter your files or read confidential

information?– Will it corrupt your operating system?

• Applets cannot do any of these things (at least not easily).

Page 46: Chapter 131 Applets and HTML Chapter 13. 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in

Chapter 13 51

Applets and Security, cont.

• Applets cannot run your programs, nor can they read from or write to files on your computer (unless the applet originated on your computer).