applets and html - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/fall2005web/slides/rec_111805.pdf ·...

26
Chapter 13 1 Applets and HTML Chapter 13

Upload: others

Post on 16-Jul-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 1

Applets and HTML

Chapter 13

Page 2: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 2

Reminders• Project 8 due Dec 8 @ 10:30 pm• Project 6 regrades due by midnight tonight

– Submit all files (including your exception classes)

• Nothing scheduled for CS180 next week:– No lectures– No labs– No recitations– No discussion groups

Page 3: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 3

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 4: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

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.

Page 5: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 5

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 6: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 6

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 7: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 7

Adding Icons to an Applet, cont.

• class DukeApplet

Page 8: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 8

Adding Icons to an Applet, cont.

Page 9: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 9

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.

• Intro to HTML: http://archive.ncsa.uiuc.edu/General/Internet/WWW/HTMLPrimer.html

Page 10: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 10

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 11: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 11

HTML Basics, cont.

Page 12: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 12

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 13: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 13

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 14: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 14

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 15: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 15

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 16: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 16

Applet in an HTML Document,

Page 17: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 17

Placing an Applet in an HTML Document, cont.

Page 18: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 18

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 19: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 19

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 20: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 20

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 21: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 21

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 22: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 22

The Older Applet Class, cont.

– example:substituteadd(friendlyLabel);forgetContentPane().add(friendlyLabel);

• Furthermore, class Applet cannot accommodate icons easily.

Page 23: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 23

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 24: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 24

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 25: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 25

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

Page 26: Applets and HTML - web.ics.purdue.eduweb.ics.purdue.edu/~cs180/Fall2005Web/slides/rec_111805.pdf · Introduction to Applets • An applet is a “small application” or a “little

Chapter 13 26

Summary

• You have learned how to write applets.• You have learned to write a simple HTML

document.• You have learned how to embed an applet in

an HTML document.