Introduction To Java 2 Programming: Applets
Introduction To Java 2 Programming: Applets
Introduction To Java 2 Programming: Applets
Applets
Overview
Introduction to Applets
The Rules for Applets The Applet Lifecycle
Writing Applets
Creating applets Deploying applets
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
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
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
(Aside: this is a common pattern used in many Java frameworks in various forms)
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
Initialised start()
Stopped destroy()
Destroyed
Writing Applets
Writing applet involves creating a sub-class of
java.applet.Applet, or javax.swing.JApplet (recommended)
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
Writing Applets
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
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
Deploying Applets
Applets are embedded into web pages with the <applet> 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)
Deploying Applets
<applet name=MyApplet code=AppletSubclass.class width=anInt height=anInt codebase=http://where.applet.lives> <param name=parameter1Name value=aValue/> <param name=parameter2Name value=anotherValue/> Your browser is not Java enabled! </applet>
Deploying Applets
<html> <body> <h1>The Basic Applet</h1> <applet code="intro2java.applet.BasicApplet.class" width="100" height="100"> </applet> </body> </html>
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
Examples
The Hello World Applet Interacting with the browser
1. 2. 3. 4. Getting parameters Showing a status message Showing other web pages The Calculator as an applet