A Few Applet Tricks

     

Here are a few neat things that you can do within your applet, including some that might be downright necessary.

Showing a Message in the Browser's Status Bar

The status bar of the browser typically will display messages in the bottom-left corner. This feature is so retro mid-90s, it is impossible to resist.

 

 applet.showStatus("All your base are belong to us. Ha ha ha"); 

Nuff said.

Making the Browser Visit a URL

You can redirect the browser using the showDocument() method, like this:

 

 try {         URL url = new URL(getDocumentBase(),         "http://www.example.com/some.jsp");         applet.getAppletContext().showDocument(url);      } catch (MalformedURLException e) { //deal      } 

Passing Parameters into Applets

You can pass a parameter into an applet from the HTML page that displays the applet. You do so using the applet.getParameter(String s) method. Then, you pass the parameter in via a special HTML tag called <param> .

Here is the code to get the parameter:

 

 public void init() {        String parameterName = "stockPrice";        String s = applet.getParameter(parameterName); } 

This code will return null in the event that no parameter named "stockPrice" can be found.

Here is the HTML required to pass it in:

 

 <applet code=MyApplet width=200 height=200>      <param name=stockPrice value="2.95"> </applet> 

Note that if you are using a servlet container or some other app server like ColdFusion or ASP.NET or PHP, you can set the value of the parameter dynamically ”that is, at runtime.

Loading Image Files in Applets

You can use image files inside your applets.

 

 Image image; public void init() {      //get the image      image = getImage(getDocumentBase(),                  "http://www.example.com/some.gif"); } public void paint(Graphics g) {      //draw image in upper-left corner      g.drawImage(image, 0, 0, this); } 

Playing Audio Files in Applets

You can cause your applet to play audio files that are available via URL as well.

 

 public void init() {         // Load audio clip         AudioClip ac = getAudioClip(getDocumentBase(), "http://www.example.com/some.au"); //play the audio         ac.play();         //stop audio         ac.stop();         //play audio continuously         ac.loop();     } 

With these easy methods , you can supply buttons to your applet that respond just like stereo controls.

We have admittedly skimmed over applets here ( sort of). That's because they simply do not hold the attractiveness they once did. Server-side apps are more powerful in many ways. There is Java WebStart (which allows you to run Web-distributed programs locally with automatic updates), and applets have proven slow and difficult to manage. But they have a certain important place in Javaland, so they should be discussed. With what we've covered here, you certainly have enough to get started. The real appeal of applets of course is in the applications that you write and distribute as applets. Much of what we cover throughout the rest of this book could be created as applets knowing what we have gone over.

If you are eager to see some pretty sophisticated applets, try visiting www.java.com or http://java.sun.com/products/plugin/1.4.2/demos/plugin/applets.html. Sun has some very cool examples here. They probably will put a 5.0 page up, but it isn't there as of this writing, so check back for it.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net