ࡱ > [@ ! bjbj44 "2 Vi Vi $ K 2 D D D D , $ R i t h h h D D h D D h f D & @T 0 K j f ~ L 0 " = Y
B
B 95712C Lecture Notes
Wednesday, November 17, 2004
Applets
Reference: Chapter 10 of Core Java, Chapter 15 of Thinking in Java.
Applet Basics
See the HYPERLINK "http://www.andrew.cmu.edu/user/syucel/Java/lectures/Apps-Applets.ppt" presentation slides on applets.
See the Applet class hierarchy in Figure 10-1.
See HYPERLINK "http://www.andrew.cmu.edu/user/syucel/Java/CoreJavaV6/v1/v1ch10/NotHelloWorldApplet/" NotHelloWorldApplet.java and the HYPERLINK "http://www.andrew.cmu.edu/user/syucel/Java/CoreJavaV6/v1/v1ch10/NotHelloWorldApplet/" NotHelloWorldApplet.html.
See HYPERLINK "http://www.andrew.cmu.edu/user/syucel/Java/CoreJavaV6/v1/v1ch10/CalculatorApplet/" CalculatorApplet.java and the HYPERLINK "http://www.andrew.cmu.edu/user/syucel/Java/CoreJavaV6/v1/v1ch10/CalculatorApplet/" CalculatorApplet.html.
Applet Security
Applets cannot do some operations due to security restrictions:
Applets cannot start executable programs.
Applets can only phone home.
Applets cannot read from or write to the local file system.
Applets cannot read environment variables except few.
Applet windows carry a warning message.
Security restrictions can be bypassed by signing the applets. Signed applets can run as freely as any other application. We will not study it in this course.
As a reference, see the online Java tutorial on this topic: HYPERLINK "http://java.sun.com/docs/books/tutorial/jar/sign/intro.html" http://java.sun.com/docs/books/tutorial/jar/sign/intro.html
Applet Specific Features
Interactions with the browser
Browsers may pass information to the applets. Applets may ask the browser to fetch a multimedia file, to show a message on the status line, or to show a different page.
Passing Information to Applet
This is done via the param tag of the html file like . The applet code can read this parameter via the getParameter(font). getParameter() returns a String.
Communicating with the browser
showStatus(String) shows a message on the status line of the browser.
Applet may ask the browser to display a URL. This happens through the AppletContext interface.
getAppletContext().showDocument(URL url)
getAppletContext().showDocument(URL url, String target)
See HYPERLINK "http://www.andrew.cmu.edu/user/syucel/Java/CoreJavaV6/v1/v1ch10/Bookmark/" Bookmark.java and HYPERLINK "http://www.andrew.cmu.edu/user/syucel/Java/CoreJavaV6/v1/v1ch10/Bookmark/" Bookmark.html
Multimedia
Applets can easily access the image and audio files from their codebase.
Image cat = getImage(getDocumentBase(), images/cat.gif);
AudioClip meow = getAudioClip(getDocumentBase(), audio/meow.au);
Play(getDocumentBase(), audio/meow.au);
Writing Programs as both an Applet and an Application
You can write your program to run as an applet and as an application. Here are some specifics:
Applets dont execute a main method. They execute init() and start().
Applets dont have a main frame window. However, you can open up dialog boxes and other windows/frames.
The size of applets is given by the html file. Dont call the setSize(), the setTitle(), the setDefaultCloseOperation(), and the show() methods on applets.
Applets may get parameters from the browser. Applications may get parameters from the command line.
Applets may use the browser. Applications dont have browsers.
You can do the following for simple programs:
public class MyApplet extends JApplet { // variable declaration for MyApplet like private JButton b; // etc.
public static void main( String[] args) { JFrame f = new JFrame();
f.setTitle("Main Frame");
f.setSize(300, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyApplet a = new MyApplet();
a.init();
f.getContentPane().add(a);
f.show(); } public void init () { b = new JButton(Applet Button); // create the instances.
add(b);}
// other methods. }
For applets that utilize the browser functionality, you need to provide empty implementation for AppletStub and AppletContext interfaces.
See HYPERLINK "http://www.andrew.cmu.edu/user/syucel/Java/CoreJavaV6/v1/v1ch10/AppletApplication/" AppletApplication files.
Jar Files
Jar format is a better choice than the zip format for packaging Java programs. Applet class files and all the resources (like multimedia files, resource files) can be archived in a jar file. This way, the applets will load much faster.
Have