Contents 1. Classes and Objects 2. Inheritance 3. Interfaces 4. Exceptions and Error Handling 5. Intro to Concurrency 6. Concurrency in Java 7. Graphics and Animation 8. Applets 8-1
Chapter 8: Applets After this chapter you will be able to: - Create applets - Read data embedded in HTML into applets 8-2
Creating An Applet Create a subclass of class Applet import java.applet.applet; import java.awt.graphics; public class HelloApplet extends Applet { public void paint (Graphics g) { g.drawstring("this is an Applet", 10, 10); } } and a HTML file containing an applet tag <applet code=helloapplet width=200 height=200> </applet> 8-3
Running An Applet TM Open the HTML file using a Java enabled web browser or the JDK s appletviewer The.class file will be executed when the HTML file loads 8-4
Applet Tag Attributes Required attributes: code: specifies the bytecode (.class) file width: specifies the width of the applet s output area on the web page height: specifies the height of the applet s output area on the web page <applet code=helloapplet width=200 height=200> </applet> 8-5
Applet Tag Attributes 8-6 Optional attributes: align: specifies the alignment of the applet s area on the web page vspace: specifies the spacing (in pixels) above and below the applet s area hspace: specifies the spacing (in pixels) on either side of the applet s area codebase: specifies the directory location of the.class file relative to the directory of the HTML file name: specifies a name that can be used for inter-applet communication
Applet Methods Can override the following methods: init is automatically called once public void init (); start is automatically called after init, and then whenever a user returns to the applet s web page after moving off public void start (); 8-7
Applet Methods stop is called automatically when a user moves off the applet s web page public void stop (); destroy is called automatically when the user exits the browser public void destroy (); paint is called automatically for drawing/redrawing public void paint (Graphics g); 8-8
Passing Data To Applets Can embed data in applet tag with one or more param tags: <applet code=helloapplet width=200 height=200> <param name=message1 value= hello there > <param name=message2 value= bonjour > <param name=message3 value= ni how > </applet> the applet can retrive the value part by calling getparameter to lookup by passing the name part public String getparameter (String name); 8-9
Example init reads in the messages import java.applet.*; import java.awt.*; public class GreetingApplet extends Applet { String [] messages; 8-10 } public void init () { int count = 0; // count the # of messages while (getparameter("message" + (count+1))!= null) count++; messages = new String[count]; // allocate array for (int i = 0; i < count; i++) messages[i] = getparameter("message" + (i + 1)); }...
Output Messages paint outputs the messages import java.applet.*; import java.awt.*; public class GreetingApplet extends Applet { String [] messages; } public void init () {...} public void paint (Graphics g) { for (int i = 0; i < messages.length; i++) g.drawstring(messages[i], 10, 10*(i+1)); } 8-11
AppletContext An applet can interact with the browser environment via an object which supports the AppletContext interface An applet can get it s context object by calling Applet.getAppletContext public AppletContext getappletcontext(); 8-12
Interface AppletContext An applet can call the methods below after getting it s context public interface java.applet.appletcontext { public Applet getapplet(string name); public Enumeration getapplets(); public AudioClip getaudioclip(url url); public Image getimage(url url); public void showdocument(url url); public void showdocument(url url, String target); public void showstatus(string status); } 8-13
AppletContext Methods getapplet retrieves an applet object using the name attribute of the Applet HTML tag. Can have multiple applets on the same web page. public Applet getapplet(string name); getapplets Returns an Enumeration containing all the applets on the current web page public Enumeration getapplets(); 8-14
Interface Enumeration An object supporting the Enumeration interface supports iteration through a list public interface java.util.enumeration { public boolean hasmoreelements(); public Object nextelement(); } Processing all the applets AppletContext ac = getappletcontext(); Enumeration e = ac.getapplets(); while (e.hasmoreelements()) { Applet a = (Applet) e.nextelement(); // process applet } 8-15
Playing Sound AppletContext.getAudioClip returns an AudioClip object using specified url public AudioClip getaudioclip(url url); Interface AudioClip public interface java.applet.audioclip { public void loop(); // play clip continuously public void play(); // play clip once public void stop(); // stop playing } 8-16
AppletContext Methods getimage returns an Image object corresponding to the specified url public Image getimage(url url); showdocument requests the browser to display a new web page using the specified url public void showdocument(url url); target can be specified in multi-frame applets public void showdocument(url url, String target); 8-17
Displaying Messages showstatus displays a message in the status area at the bottom of the browser window public void showstatus(string status); 8-18
Security Issues Java provides several levels of security safe language: no pointers (as in C/C++) makes it harder to write viruses bytecode verification: the virtual machine verifies bytecode before execution custom security (initial release JDK 1.1) Using class SecurityManager can implement a custom security policy to grant/deny requests for resources package java.security supports digital signatures encryption 8-19
Conclusions/Predictions Security is an important area for Java applications Look for package java.security to evolve over time with new features Java will become the language of choice for developing secure applications 8-20
It s Graduation Time Congratulations!! 8-21