Powerpoint slides

Download Report

Transcript Powerpoint slides

Applets and HTML
Chapter 13
Chapter 13
1
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.
Chapter 13
4
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.
Chapter 13
6
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.*;
Chapter 13
7
Applet Basics, cont.
Chapter 13
8
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.
Chapter 13
9
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.
Chapter 13
10
Applet Basics, cont.
• class HelloApplet
Chapter 13
11
Applet Basics, cont.
Chapter 13
12
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.
Chapter 13
13
Programming Example: An
Adder Applet
• class AdderApplet
Chapter 13
14
Programming Example: An
Adder Applet, cont.
Chapter 13
15
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.
Chapter 13
16
Converting a Swing
Application to an Applet
• Delete any invocation of addWindowListener.
• Delete any invocation of setTitle.
• Delete any invocation of setSize.
Chapter 13
17
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.
Chapter 13
18
Adding Icons to an Applet,
cont.
• class DukeApplet
Chapter 13
19
Adding Icons to an Applet,
cont.
Chapter 13
20
Adding Icons to an Applet,
cont.
• ImageIcon
is a class in the Swing library.
• syntax
ImageIcon Name_of_ImageIcon =
new ImgeIcon(Picture_File_Name);
is a string giving either a
relative or absolute path name to the
picture file.
– Picture_File_Name
Chapter 13
21
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.
Chapter 13
23
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.
Chapter 13
24
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.
Chapter 13
25
HTML Basics
• Most HTML commands are of the form
<command>
Some text
</command>
• example
<h1>
My Home Page
</h1>
Chapter 13
26
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.
Chapter 13
27
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).
Chapter 13
28
HTML Basics, cont.
Chapter 13
29
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.
Chapter 13
30
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.
Chapter 13
31
HTML Basics, cont.
Chapter 13
32
HTML Basics, cont.
Chapter 13
33
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>
Chapter 13
34
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.
Chapter 13
35
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.
Chapter 13
36
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.
Chapter 13
37
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.
Chapter 13
38
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”
code=“AdderApplet”
is optional.
works just as well)
Chapter 13
40
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.
Chapter 13
41
Placing an Applet in an HTML
Document, cont.
Chapter 13
42
Placing an Applet in an HTML
Document, cont.
Chapter 13
43
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.
Chapter 13
44
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.
Chapter 13
45
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 statements
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
Chapter 13
46
The Older Applet Class,
cont.
– you do not need
import 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).
Chapter 13
47
The Older Applet Class,
cont.
– example:
substitute
add(friendlyLabel);
for
getContentPane().add(friendlyLabel);
• Furthermore, class Applet cannot
accommodate icons easily.
Chapter 13
48
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.
Chapter 13
49
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).
Chapter 13
50
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).
Chapter 13
51