Applets and HTML Chapter 13 1 Introduction Applets

  • Slides: 46
Download presentation
Applets and HTML Chapter 13 1

Applets and HTML Chapter 13 1

Introduction • Applets are simply Java programs designed to run from a document (page)

Introduction • Applets are simply Java programs designed to run from a document (page) on the World Wide Web. • Hyper. Text 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

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

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. event. *; Chapter 13 7

Applet Basics, cont. Chapter 13 8

Applet Basics, cont. Chapter 13 8

Applet Basics, cont. • A JApplet is a Container, which permits you to add

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 set. Visible method or the set. Title 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

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 Hello. Applet Chapter 13 11

Applet Basics, cont. • class Hello. Applet Chapter 13 11

Applet Basics, cont. Chapter 13 12

Applet Basics, cont. Chapter 13 12

Running an Applet • Applets are compiled the same way other Java classes are

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 Adder. Applet Chapter 13 14

Programming Example: An Adder Applet • class Adder. Applet Chapter 13 14

Programming Example: An Adder Applet, cont. Chapter 13 15

Programming Example: An Adder Applet, cont. Chapter 13 15

Converting a Swing Application to an Applet • Derive the class from JApplet rather

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 add. Window.

Converting a Swing Application to an Applet • Delete any invocation of add. Window. Listener. • Delete any invocation of set. Title. • Delete any invocation of set. Size. Chapter 13 17

Adding Icons to an Applet • An icon typically is a small picture. •

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 JMenu. Item can also have an icon. Chapter 13 18

Adding Icons to an Applet, cont. • class Duke. Applet Chapter 13 19

Adding Icons to an Applet, cont. • class Duke. Applet Chapter 13 19

Adding Icons to an Applet, cont. Chapter 13 20

Adding Icons to an Applet, cont. Chapter 13 20

Adding Icons to an Applet, cont. • Image. Icon is a class in the

Adding Icons to an Applet, cont. • Image. Icon is a class in the Swing library. • syntax Image. Icon Name_of_Image. Icon = new Imge. Icon(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

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 Hyper. Text 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

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.

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

HTML Basics • Most HTML commands are of the form <command> Some text </command> • example <h 1> My Home Page </h 1> Chapter 13 26

HTML Basics, cont. • Anything between <center> and </center> is centered on the page

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 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>, <t. Ab. Le>

HTML Basics, cont. • HTML is not case sensitive. <table>, <TABLE>, <t. Ab. Le> 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. Chapter 13 29

HTML Basics, cont. • The entire document is enclosed between <html> and </html>. •

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.

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 32

HTML Basics, cont. Chapter 13 33

HTML Basics, cont. Chapter 13 33

Inserting Hyperlinks • The key active element in an HTML document is a link

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> •

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

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

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

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

Placing an Applet in an HTML Document • To display the adder window created by class Adder. Applet, place the following command in an HTML document: <applet code=“Adder. Applet. class” width=400 height=200> </applet> (Actually “. class” code=“Adder. Applet” is optional. works just as well) Chapter 13 40

Placing an Applet in an HTML Document, cont. • This command assumes that the

Placing an Applet in an HTML Document, cont. • This command assumes that the HTML file and the file Adder. Applet. class are in the same directory. – Otherwise, a relative or absolute path name to Adder. Applet. 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 42

Placing an Applet in an HTML Document, cont. Chapter 13 43

Placing an Applet in an HTML Document, cont. Chapter 13 43

Using an Old Web Browser • A Web browser must be set up to

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

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

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. *;

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(friendly. Label); for get. Content. Pane().

The Older Applet Class, cont. – example: substitute add(friendly. Label); for get. Content. Pane(). add(friendly. Label); • Furthermore, class Applet cannot accommodate icons easily. Chapter 13 48

Applets and Security • Your applet is a program that may be run on

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

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

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