Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 131 Applets and HTML Chapter 13

2 2 Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in an HTML document

3 Chapter 133 Outline Applets HTML Applets in HTML

4 Chapter 134 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.

5 Chapter 135 Applets: Outline Introduction to Applets Applet Basics Running an Applet Adding Icons to an Applet

6 Chapter 136 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.

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

8 Chapter 138 Applet Basics, cont.

9 Chapter 139 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.

10 Chapter 1310 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.

11 Chapter 1311 Applet Basics, cont. class HelloApplet

12 Chapter 1312 Applet Basics, cont.

13 Chapter 1313 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.

14 Chapter 1314 Programming Example: An Adder Applet class AdderApplet

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

16 Chapter 1316 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.

17 Chapter 1317 Converting a Swing Application to an Applet Delete any invocation of addWindowListener. Delete any invocation of setTitle. Delete any invocation of setSize.

18 Chapter 1318 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.

19 Chapter 1319 Adding Icons to an Applet, cont. class DukeApplet

20 Chapter 1320 Adding Icons to an Applet, cont.

21 Chapter 1321 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.

22 Chapter 1322 HTML: Outline Introduction to HTML HTML Basics Inserting Hyperlinks Displaying a Picture

23 Chapter 1323 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.

24 Chapter 1324 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.

25 Chapter 1325 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.

26 Chapter 1326 HTML Basics Most HTML commands are of the form Some text example My Home Page

27 Chapter 1327 HTML Basics, cont. Anything between and is centered on the page when it is displayed. Some commands do not need a closing command. For example begins a new line and begins a new paragraph. The browser inserts breaks where necessary to fit the text appropriately on the screen.

28 Chapter 1328 HTML Basics, cont. HTML is not case sensitive.,,, 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 and form a “container” (in this case a table container).

29 Chapter 1329 HTML Basics, cont.

30 Chapter 1330 HTML Basics, cont. The entire document is enclosed between and. The head of the document is enclosed between and. –The head contains information used by a browser, but typically not displayed. –It might consist only of a title enclosed between and, used to name the document.

31 Chapter 1331 HTML Basics, cont. Two parts of the document are displayed on the screen. –The body is enclosed between and and is the real content of the document. –The address is optional. It is enclosed between and. It includes the address of the document’s owner and the last date the document was modified.

32 Chapter 1332 HTML Basics, cont.

33 Chapter 1333 HTML Basics, cont.

34 Chapter 1334 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 Displayed_Text_to_Click

35 Chapter 1335 Inserting Hyperlinks, cont. example Walter Savitch 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.

36 Chapter 1336 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.

37 Chapter 1337 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.

38 Chapter 1338 Displaying a Picture A picture can be inserted into an HTML document using example The picture can be in any directory, but the path name, either full or relative, leading to the picture must be provided.

39 Chapter 1339 Applets in HTML: Outline Placing an Applet in an HTML Document (optional) The Older Applet Class Applets and Security

40 Chapter 1340 Placing an Applet in an HTML Document To display the adder window created by class AdderApplet, place the following command in an HTML document: (Actually “.class” is optional. code=“AdderApplet” works just as well)

41 Chapter 1341 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.

42 Chapter 1342 Placing an Applet in an HTML Document, cont.

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

44 Chapter 1344 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.

45 Chapter 1345 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.

46 Chapter 1346 The Older Applet Class To use the older Applet class instead of the JApplet class –remove the J s 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.*;

47 Chapter 1347 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 ).

48 Chapter 1348 The Older Applet Class, cont. –example: substitute add(friendlyLabel); for getContentPane().add(friendlyLabel); Furthermore, class Applet cannot accommodate icons easily.

49 Chapter 1349 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.

50 Chapter 1350 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).

51 Chapter 1351 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).

52 Chapter 1352 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.


Download ppt "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."

Similar presentations


Ads by Google