280 likes | 674 Views
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
E N D
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 • Construct a simple HTML web page that executes an applet
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
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
What Is an Applet? Inheritance hierarchy for AWT and Swing
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 {…}
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
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
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
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
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?
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?
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
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?
The World Wide Web Why is the expression "visiting a website" confusing?
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
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>
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
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?
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
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
How Do You Run an Applet? Go to this web page and follow the directions Be sure FactInt.class is with this page