Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets...

28
Chapter 14 Applets
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    0

Transcript of Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets...

Page 1: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

Chapter 14

Applets

Page 2: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

2

Knowledge Goals

• Understand the differing roles of applications and applets

• Understand how a browser operates• Understand the role of HTML

Page 3: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 4: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 5: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 6: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

6

What Is an Applet?

Inheritancehierarchy

forAWT and

Swing

Page 7: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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 {…}

Page 8: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

8

public void actionPerformed(ActionEvent event)

{

int value;

value =

Integer.parseInt(inputField.getText());

inputField.setText("");

outLabel.setText(value + " factorial is ”

+ factorial(value));

}

How Do We Write Applets?

Method call

Page 9: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 10: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 11: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 12: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

12

// Add components

add(label);

add(inputField);

add(button);

add(outLabel);

// Specify a layout manager for the

// window object

setLayout(new GridLayout(4,1));

}

How Do We Write Applets?

Why are the add s not applied to an object?

Page 13: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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?

Page 14: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 15: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

15

How Do You Run an Applet?

The Web An infrastructure of information combined and the network software used to access itUniform 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 theInternet and the Web?

Page 16: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

16

The World Wide Web

Why is the expression"visiting a website"

confusing?

Page 17: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

17

How Do You Run an Applet?

Hypertext Markup Language (HTML)The language used to create or build a Web pageMarkup language A language that uses tags to annotate the information in a documentTagsThe syntactic element in a markup language that indicates how information should be displayed

Page 18: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

18

How Do You Run an Applet?

HTML

Page 19: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

19

How Do You Run an Applet?

Page 20: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

20

How Do You Run an Applet?

Page 21: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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>

Page 22: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

22

How Do You Run an Applet?

The browser determines how the page should be displayed based on the tagsThe 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

Page 23: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

23

How Do You Run an Applet?

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

Page 24: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 25: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

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

Page 26: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

26

How Do You Run an Applet?

Go tothiswebpageand

followthe

directions

Be sure FactInt.class is with this page

Page 27: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

27

How Do You Run an Applet?

Page 28: Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.

28

How Do You Run an Applet?