1 / 28

Chapter 14 Applets

Chapter 14 Applets. Knowledge Goals. Understand the differing roles of applications and applets Understand how a browser operates Understand the role of HTML. Skill Goals. Write an applet to perform a simple task Embed Bytecode within a web page

banagher
Download Presentation

Chapter 14 Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 14 • Applets

  2. Knowledge Goals • Understand the differing roles of applications and applets • Understand how a browser operates • Understand the role of HTML

  3. Skill Goals • Write an applet to perform a simple task • Embed Bytecode within a web page • Construct a simple HTML web page that executes an applet

  4. What Is an Applet? • Applet • A mini-application • Distributed along with web pages • Run under a browser at the viewer’s site • Run under an applet viewer • Is distributed in Bytecode form

  5. What Is an Applet? • Applets differ from applications in several ways • Applets don’t have a main method • Applets are invoked differently • Applets are subject to more security constraints • Applets are not in control of their own destiny • Applets do not have constructors; initializations are done in method init

  6. What Is an Applet? Inheritance hierarchy for AWT and Swing

  7. How Do We Write Applets? • // Applet Factorial computes the factorial of • // its input and stores it in a variable of • // type int, which is displayed on the • // screen. • import javax.swing; // JApplet class • import java.awt.*; // User interface classes • import java.awt.event.*; // Event classes • public class FactInt extends Applet implements ActionListener {…}

  8. How Do We Write Applets? • public void actionPerformed(ActionEvent event) • { • int value; • value = • Integer.parseInt(inputField.getText()); • inputField.setText(""); • outLabel.setText(value + " factorial is ” • + factorial(value)); • } Method call

  9. How Do We Write Applets? • private int factorial(int n) • // Assumption: n is not negative. • { • if (n == 0) • return 1; • else return (n * factorial((n-1))); • } Now, we have to set up a button, a label, and a text input field

  10. How Do We Write Applets? • // Setting up a button, label, and input • // field • private static JTextField inputField; • private static JLabel label; • private static JLabel outLabel; • private static JButton button; We need to write method init

  11. How Do We Write Applets? • public void init() • { // Instantiate components • label = new JLabel("Enter an integer; ” • + “press Enter."); • outLabel = new JLabel("Answer"); • button = new JButton("Enter"); • button.addActionListener(this); • inputField = new • JTextField("Value here"); Note

  12. How Do We Write Applets? • // Add components • add(label); • add(inputField); • add(button); • add(outLabel); • // Specify a layout manager for the • // window object • setLayout(new GridLayout(4,1)); • } Why are the add s not applied to an object?

  13. How Do You Run an Applet? • The Bytecode version of an applet is run on the user’s browser • Yes, but how? • Some definitions are in order first • Computer network • A collection of computing devices connected in order to communicate and share resources Can you name some of the devices in a computer network?

  14. How Do You Run an Applet? • Local area network (LAN) • A network that connects a relatively small number of machines in a relatively close geographical area • Wide area network (WAN) • A network that connects local area networks over a potentially large geographic distance • Internet • A wide area network that spans the planet

  15. How Do You Run an Applet? • The Web • An infrastructure of information combined and the network software used to access it • Uniform Resource Locator (URL) • A standard way of specifying the location of a Web page, containing the hostname, "/", and a file What is the relationship between the Internet and the Web?

  16. The World Wide Web Why is the expression "visiting a website" confusing?

  17. How Do You Run an Applet? • Hypertext Markup Language (HTML) • The language used to create or build a Web page • Markup language • A language that uses tags to annotate the information in a document • Tags • The syntactic element in a markup language that indicates how information should be displayed

  18. How Do You Run an Applet? HTML

  19. How Do You Run an Applet?

  20. How Do You Run an Applet?

  21. How Do You Run an Applet? • HTML • Tags are enclosed in angle brackets (<. . . >) • Words such as HEAD, TITLE, and BODY are called elements and specify the type of the tag • Tags are often used in pairs, with a start tag such as <BODY> and a corresponding end tag with a / before the element name, such as </BODY>

  22. How Do You Run an Applet? • The browser determines how the page should be displayed based on the tags • The browser • Ignores the way we format the HTML document using carriage returns, extra spaces, and blank lines • Takes into account the width and height of the browser window • Reformats the contents to fit your browser window

  23. Tags <HTML>…</HTML> <HEAD>…</HEAD> <TITLE>…</TITLE> <BODY>…</BODY> <P>…</P> <HR> <B>…</B> <I>…</I> <PRE>…</PRE> Meaning Enclose document Enclose heading Enclose title Enclose body of document Enclose paragraph Insert horizontal rule Enclosed should be boldface Enclosed should be in italics Preformatted; display text as-is How Do You Run an Applet?

  24. How Do You Run an Applet? • <APPLET code = “fileName.class” width=250height=150></APPLET> • <APPLET code = “FactInt.class” width=250height=150></APPLET> • Note FactInt.class is the file with the Bytecode version of the Applet FactInt

  25. Yes, But How Do You Run an Applet? • If you are in an integrated environment, your Java system will run the applet in the viewer • To run in a browser, • compile the applet • name the Bytecode file with a .class extension • create the web page with the appropriate <applet …> • put the .class file in the same directory as the web page • access the web page

  26. How Do You Run an Applet? Go to this web page and follow the directions Be sure FactInt.class is with this page

  27. How Do You Run an Applet?

  28. How Do You Run an Applet?

More Related