Download OOP Design Patterns - UNI Department of Computer Science

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

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

Document related concepts
no text concepts found
Transcript
Introduction to Applets
Chapter 21
Applets
• An applet is a Java application that is
intended to be invoked and executed
through a Web browser.
• Click Here To View Cannon World Applet
Security Issues
• Applets are loaded from remote computer
and executed locally. Since an “untrusted”
stranger wrote the applet, applets are
restricted. Applets CANNOT:
– run any local executable program
– read or write to local computer’s file system
– Communicate with any computer except
originating server
– Access many facts about the local computer,
except name and version of OS
Applets vs. Applications
• Much of the code for Applets is similar to
Applications
• Differences:
– Applets are created by subclassing Applet,
instead of Frame
– Applets begin execution with the method
init, instead of main
– Applets are halted and restarted as the Web
browser moves to a new page and returns
Applet class
• Applet is a subclass of Panel, thus it inherits all
of Panel’s graphical component attributes.
• Applets have four methods available for
overriding by clients:
– init() called only once when the applet is first loaded
(use like the constructor)
– start() called to begin execution each time the Webpage containing the applet is exposed
– stop() called each time the Web-page containing the
applet is hidden
– Destroy() called when the applet is about to be
terminated (should free any resource being used)
CannonWorld as an Applet
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class CannonWorld extends Applet // was Frame
{
public static final int FrameWidth = 600;
public static final int FrameHeight = 400;
private
private
private
private
//
int
String
CannonBall
Scrollbar
angle;
message;
cannonBall;
slider;
public CannonWorld() {
public void init() {
// changed the layout manager
setLayout(new BorderLayout());
setSize ( FrameWidth, FrameHeight );
//
setTitle( "Cannon Game" );
angle
message
…
= 45;
= "Angle: " + angle;
Http and Applets
•
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html;
charset=windows-1252">
<TITLE>Cannon World Applet Demo</TITLE>
</HEAD>
<BODY LANG="en-US" DIR="LTR">
<P ALIGN=CENTER><FONT SIZE=5 STYLE="font-size: 20pt">CS II Webpage to Display an Applet</FONT></P>
<P ALIGN=CENTER><BR><BR>
</P>
<P ALIGN=CENTER><APPLET
CODEBASE="http://www.cs.uni.edu/~fienup/cs062s06/sessions/session24/
CannonWorld_Applet/"
CODE="CannonWorldApplet" ALIGN=LEFT WIDTH=600
HEIGHT=400>
</APPLET><BR CLEAR=LEFT><BR><BR>
</BODY>
</HTML>
Parameters in <applet> tag
<APPLET CODEBASE="http://www.cs.uni.edu"
CODE="CannonWorldApplet"
ALIGN=LEFT WIDTH=600 HEIGHT=400>
<PARAM name=name1 value=“value1”>
You do not have a Java enabled browser
</APPLET>
In the Java Applet, getParameter(“name1”) can be
used to access the parameter string.
Loading Resources from the Server
• getImage(URL) – retrieve the image
specified by the URL (jpeg and gif)
• getAudioClip(URL) – return audioClip
object, then play it (or play(URL) short cut)
• getCodeBase() returns URL for codebase
• appletContext.showDocument(URL)
instructs the Web browser to display the a
new page from the URL
URL objects
• Constructed from a string or previous URL
and a string
• URL have an openStream() method which
returns an inputStream object, so you can
read from a URL just like reading from a
file.