Download C:\class\java\slides\Chap 10 (Applets).shw

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Applets
Chapter 10
Copyright © 1998-2002 Delroy A. Brinkerhoff. All Rights Reserved.
Chapter 10
Slide 1 of 24
Java™ and Web Browsers
Relating versions
P Browsers must be Java™-aware or support a JVM plug-in
P Netscape
< 2.0 is Java™ 1.0 aware
< 4.0 or 4.1 claimed to be Java™ 1.1 aware
< 4.5 is Java™ 1.1 aware
< 4.5 (maybe earlier versions) supports JVM plug-ins
P Internet Explorer
< 3.0 is Java™ 1.0 aware
< 4.0 is Java™ 1.1 aware
< Modern versions (exact version is unknown) support JVM plug-ins
via ActiveX
Chapter 10
Slide 2 of 24
Competing Tags
Running Java™ on a web page
P <APPLET> tag formally adopted at HTML 3.2
< Supported by Netscape 2.0
< Supported by Internet Explorer 3.0
< Deprecated at HTML 4.0
P Netscape introduced <EMBED> tag
P Internet Explorer introduced <OBJECT> tag
P HTML adopted the <OBJECT> at HTML 4.0
P <OBJECT> was expanded to work with applets, plug-ins,
ActiveX, images, etc.
P <OBJECT> may not be supported by all browsers yet
Chapter 10
Slide 3 of 24
HTML Files
To launch Java™ applets (pre HTML 4.0)
<HTML>
<HEAD><TITLE> Java Applet Test </TITLE></HEAD>
<BODY>
required
tags
<APPLET code="WinHello.class"
codebase="relativePath/"
width="100"
height="100" >
</APPLET>
</BODY>
</HTML>
codebase is not required if .html & .class
file are in the same directory
Chapter 10
Slide 4 of 24
HTML Files
To launch Java™ applets (post HTML 4.0)
<HTML>
<HEAD><TITLE> Java Applet Test </TITLE></HEAD>
<BODY>
required
tags
<OBJECT classid="java:WinHello.class"
codetype="application/octet-stream"
codebase="relativePath/"
width="100"
height="100" >
</OBJECT>
</BODY>
</HTML>
codebase is not required if .html & .class
file are in the same directory
Chapter 10
Slide 5 of 24
Applets and JAR Files
Jar files improve applet efficiency (p. 625 and Chapter 1, Slide 16)
P Each .class file (or other resource file) requires a separate
HTTP request (a connection is opened, the file transferred,
and the connection closed)
P Placing all files in a Jar reduces the overhead of multiple
connections
<APPLET code="WinHello.class"
archive="Hello.jar"
codebase="relativePath/"
width="100"
height="100" >
</APPLET>
Chapter 10
Slide 6 of 24
Java™ Plug-In
Separating the JVM and the browser
P Packaged with the JRE
< http://java.sun.com/products/plugin/
< May be redistributed
< Required for Java™ 1.2 (and newer)
features, including Swing
< Located in the “Control Panel” (1.3+)
< Located in “Programs” (1.2)
Chapter 10
Slide 7 of 24
HTMLConverter
Loading the Java™ plug-in from an HTML page
P Free from Sun
< http://java.sun.com/products/plugin/
< htmlconv13.zip
< Add unzip directory to classpath
< cd to html directory
< java HTMLConverter
Chapter 10
Slide 8 of 24
Viewing Applets
Appletviewer, Netscape, Internet Explorer, and HotJava™
P Applet is started through an
HTML file
P View with applet viewer:
appletviewer file.html
P If a browser is registered to
handle HTML files, double click
P File | Open Page
Chapter 10
Slide 9 of 24
Applet Execution Options
A summary
Appletviewer
Netscape
<APPLET> / 1.1
Okay
Okay
<OBJECT> / 1.1
Fail
Okay
<APPLET> / 1.2+
Okay
Fail
<APPLET> / 1.1 Converted
Fail
Okay
<APPLET> / 1.2+ Converted
Fail
Okay
Chapter 10
Slide 10 of 24
Applet Basics
Browser interpreted java programs
P Are based on the AWT
< import java.applet.Applet;
< import java.awt.*;
< import java.awt.event.*;
< extends Applet class
// if GUI based
P Are started in an HTML file with the <APPLET> tag
(deprecated at HTML 4.0) or the <OBJECT> tag
< code
< codebase
< width
< height
< align
applet file name
relative path (from html file) to applet (.class file)
width of applet window in pixels or percent
height of applet window in pixels or percent
left or right (several other also available)
P Are interpreted by a browser or appletviewer
Chapter 10
Slide 11 of 24
Applet Example
“Hello world” on the world wide web
import java.applet.Applet;
import java.awt.Graphics;
public class WinHello extends Applet
{
public void paint(Graphics g)
// most work done here
{
g.drawString("Hello world", 10, 20);
}
}
Chapter 10
Slide 12 of 24
Swing Applet Basics
Browser plug-ins
P Based on awt and on Swing
< import java.awt.*;
< import javax.swing.*;
< import java.awt.event.*;
< import javax.event.*;
< extends JApplet class
// if GUI based
// may be needed
P Started with <applet>
< run HTMLConverter
< added tags load the plug-in
P Browser loads the JVM as a plug-in
Chapter 10
Slide 13 of 24
Swing Applet Example
Similar to a Swing application
import java.awt.*;
import javax.swing.*;
public class JWinHello extends JApplet
{
public void init( )
{ Container content = getContentPane( );
content.add(new SketchPanel( ));
}
}
class SketchPanel extends JPanel
{
public void paintComponent(Graphics g)
{ super.paintComponent(g);
g.drawString("Hello world", 10, 20);
}
}
Chapter 10
Slide 14 of 24
Arguments from HTML to Applet
Configurable applets
P <APPLET> and </APPLET> (or <OBJECT> and </OBJECT>)
tags surround
< 0 or more <PARAM> tags
< Default text displayed if the browser is not Java™ aware
P <PARAM> tags
< Used to pass parameters or arguments to applets
< Attribute: name=”variableName”
< Attribute: value=”variableValue”
< For example: <PARAM name=”html_money” value=”217”>
P Applet reads parameters with the getParameter method
< Returns a String value, which is converted if necessary
< String Name = getParameter(”variableName”);
< int money = Integer.parseInt(getParameter(”html_money”).trim());
Chapter 10
Slide 15 of 24
Example of APPLET and PARAM
Starting Java™ applets from HTML files
<HTML>
<HEAD><TITLE> Convert Pennies </TITLE></HEAD>
<BODY>
<APPLET code=WinMoney.class height=300 width=300>
<PARAM name = "html_money" value = "217">
Output from a Java applet showing 217 pennies as an
equivalent ount in dollars, quarters, dimes, nickels, and pennies
</APPLET>
</BODY> </HTML>
Chapter 10
Slide 16 of 24
Example Java™ Applet
Convert pennies to higher denominations
import java.applet.*;
import java.awt.*;
public class WinMoney extends Applet
{
int money, dollars, quarters, dimes, nickels;
// instance vars
public void init( )
{
money =
Integer.parseInt(getParameter(”html_money”).trim());
calculate(money);
}
Chapter 10
Slide 17 of 24
From AWT Applications to Applets
Step by step conversion
P Create an HTML page with an Applet or Object tag
P Eliminate main (not used in applets)
< Remove setSize
< Remove setTitle(); (use the <title> tag instead)
< Remove setLocaton if called
< Remove addWindowListener(this) and related code
P Derive your class from Applet not Frame
< import java.applet.*;
P Rename the constructor to public void init()
P If the frame implicitly uses a BorderLayout (the default for a
Frame), you must explicitly add a BorderLayout to the applet
P Applets cannot have menus
Chapter 10
Slide 18 of 24
From Swing To JApplets
Similar to Applet conversions
P Create an HTML page with an Applet tag
< Process the HTML file with HTMLConverter
< Make sure the plug-in is installed
P Eliminate main (not used in applets)
< Remove setSize
< Remove setTitle( ); (use the <title> tag instead)
< Remove setLocaton if called
< Remove addWindowListener(this) and related code
P Derive your class from JApplet not JFrame
P Rename the constructor to public void init()
P BorderLayout it the default for JFrame and JApplet
P JApplets can have menus
Chapter 10
Slide 19 of 24
Dual-Mode Programs
Programs that are applications and applets
// main from SpirographConsole-- still extends Applet
public static void main(String args[ ])
{
Frame f = new SpiroFrame();
// extends Frame
SpirographConsole console = new SpirographConsole();
}
f.add("Center", console);
f.setSize(600, 400);
f.setTitle("Spirograph Console");
console.init();
f.show();
Chapter 10
// should follow init
Slide 20 of 24
Dual-Mode Programs
Continued
import java.awt.*;
import java.awt.event.*;
public class SpiroFrame extends Frame implements WindowListener
{
public SpiroFrame( )
{
addWindowListener(this);
}
public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
} // class SpiroFrame
Chapter 10
Slide 21 of 24
Applet Security Summary
Restrictions increase from applications to down-loaded applets
Capabilities
JA
AV
BF
BU
Read local file
U
U
Y
Y
Write local file
U
U
Y
Y
Get file information
U
U
Y
Y
Delete file
U
Y
Y
Y
Run program
U
U
Y
Y
Read the users.name property
U
U
U
Y
Connect to network port on server
U
U
U
U
Connect to network port on other host
U
U
U
Y
Load Java library
U
U
U
Y
U
U
Y
U
U
U
BF: Browser loading local file
JA: Java application (stand-alone)
Y
Call exit
Create pop-up window
BU: Browser loading URL
AV: Applet Viewer
Chapter 10
Warn
Slide 22 of 24
Displaying New Web Pages
Like a hypertext link in an applet
// only runs through a web browser
import java.net.*;
import java.applet.*;
public class Display extends Applet
{ public void init()
{ try
{ URL
location = new URL("http://icarus.weber.edu/home/dab/");
AppletContext
context = getAppletContext();
context.showDocument(location);
}
catch (MalformedURLException E)
{
System.err.println("URL error " + E); }
}
} // class Display
Chapter 10
Slide 23 of 24
Connecting An Applet to its Host
“Applets can only phone home”
P URL(String url)
P URL(URL base, String relName)
P Member methods
< String
< String
< int
< String
< InputStream
getHost( )
getFile( )
getPort( )
getProtocol( )
openStream( )
name of host serving the URL
gets file name of URL
P Related java.Applet methods
< public URL getDocumentBase( )
< public URL getCodeBase( )
P URL source = new URL(getDocumentBase(), "employe.dat");
Chapter 10
Slide 24 of 24