Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java 2 Programming Lecture 10 Applets.

Similar presentations

Presentation on theme: "Introduction to Java 2 Programming Lecture 10 Applets."— Presentation transcript:

1 Introduction to Java 2 Programming Lecture 10 Applets

2 Overview Introduction to Applets –The Rules for Applets –The Applet Lifecycle Writing Applets –Creating applets –Deploying applets Examples: Interacting with the browser –Parameters –Displaying status messages –Showing documents

3 Introduction to Applets Applets are applications that are deployed over the Internet –Designed to run inside a browser –Are embedded in HTML pages –Core part of Java Not are popular as they were (or forecasted to be) –Patchy browser support –Can be slow to download –Macromedia Flash, etc. offer similar functionality

4 Introduction to Applets But do provides a number of benefits… Easy to deploy (web components) No need for installation or upgrades Provide more sophisticated functionality than a web page/form Allow for proprietary client-server protocols Re-use code from traditional applications Very secure

5 The Rules for Applets An applet cannot (usually) do the following: Cannot load libraries or define native methods Cannot read or write files on the client Cannot make network connections except to the server it came from Cannot start any program on the client Cannot read certain system properties Cannot pretend to be a local application –Applet windows look different

6 The Rules for Applets Ensures that an applet cannot damage the client –Otherwise opens potential for viruses, security breaches, trojan horses, etc –Applets are considered to be untrusted code Rules are enforced by a Security Manager –Installed by the JVM in the browser The rules are known as a security policy Alternate policies can be used on request –But only if the user decides to trust the code

7 The Applet Lifecycle Applets are loaded, started and managed by the browser Browser is a container that provides services to the applet –Similar to Robots in the Robocode arena Browser drives the applet through life-cycle methods –The methods mark milestones in the applet life –Instruct it to carry out some basic operations –Form a contract between the browser and the applet (Aside: this is a common pattern used in many Java frameworks in various forms)

8 The Applet Lifecycle Four basic lifecycle methods –Defaults provided by java.applet.Applet init() – Initialises the applet when (re-)loaded –Perform initialisation here rather than constructor –Guarantees complete environment is available (e.g. parameters) start() – Starts the applet running, after loading or user revisits page stop() – Stops the applet running, when user leaves page or quits destroy() – Perform final clean-up before its unloaded

9 The Applet Lifecycle

10 Writing Applets Writing applet involves creating a sub-class of –java.applet.Applet, or –javax.swing.JApplet (recommended) Remember, JApplet is a top-level Swing container –can add usual Swing components Applet base class provides a number of useful methods Applets also have a context object, that provides other functionality –E.g. driving the browser, communicating with other applets

11 Writing Applets

12 Useful Methods getParameter() – get a parameter set in the web page showStatus() – show a message in the status bar getImage() – load an image getAudioClip(), play() – play a sound file getAppletContext() – get context object getAppletContext().showDocument() – instruct the browser to show another webpage

13 Writing Applets Tip for development/debugging: –Use the appletviewer tool for viewing and testing applets –Easier to control than in a browser –Avoids problems with caching of applets Example…A Basic Applet

14 Deploying Applets Applets are embedded into web pages with the tag –Instructs the browser to display an applet in that location of the web page The applet tag can be used to set several properties about the applet –the class to load and run as an applet –height and width –Location of class files (codebase)

15 Deploying Applets Your browser is not Java enabled!

16 Deploying Applets The Basic Applet

17 Writing Applets Beware of CLASSPATH! –Applets classes are loaded from the same directory as the HTML page –Unless an alternate directory is set with the codebase attribute

18 Examples The Hello World Applet Interacting with the browser 1.Getting parameters 2.Showing a status message 3.Showing other web pages 4.The Calculator as an applet

Download ppt "Introduction to Java 2 Programming Lecture 10 Applets."

Similar presentations

Ads by Google