All of the LiveConnect examples presented so far in this chapter have made use of Java classes from the standard Java libraries from Sun. There is not a whole lot of interesting things you can do with an instance of java.ang.Double, but we have seen some interesting uses of the java.lang.System class, for example.
var f = new java.awt.Frame("Hello World"); var ta = new java.awt.TextArea("hello, world", 5, 20); f.add("Center", ta); f.pack(); f.show();
We saw in Chapter 14, Documents and Their Contents, that the Document object has an applets property which is an array containing JavaObject objects, one for each Java applet in the document. The JavaObject objects in this array represent the Java object of each applet--this will always be an instance of some subclass java.applet.Applet. Because LiveConnect exposes the Java object for each applet on a web page, you can freely read and write public fields of the applet and just as freely invoke public methods of the applet.
Example 19.2 shows some simple HTML that embeds an applet in a web page and includes buttons that start and stop the applet by using LiveConnect to invoke the applet's start() and stop() methods.
<!-- Here's the applet --> <APPLET NAME="animation" CODE="Animation.class" WIDTH=500 HEIGHT=200> </APPLET> <!-- And here are the buttons that start and stop it. --> <FORM> <INPUT TYPE=button VALUE="Start" onclick="document.animation.start()"> <INPUT TYPE=button VALUE="Stop" onclick="document.animation.stop()"> </FORM>
There are a couple of points to note about this example. First, the <APPLET> tag is given a NAME attribute, and the value of that attribute becomes the name of a property in the document object. We've seen this technique before with the <FORM> and <IMG> tags; in this case it allows us to refer to applets by names such as document.animation instead of numbers such as document.applets.
The second point to note about this example is that it calls the start() and stop() methods of the applet--these are standard methods that all applets define; they are the methods that the browser itself calls to start and stop the applet. But you needn't stop at calling the standard methods of the Java Applet class. If your applet defines other methods of its own, you can call any of these as well. If you were working with a full-featured animation applet, for example, you might define an HTML form to serve as a complete control panel for the animation, with Fast-Forward and Reverse buttons, an input field for specifying speed, and so on. The buttons in this control panel could then control the applet by invoking special-purpose methods, such as fast_forward(), provided by the applet.
 In fact, it is safer and more portable to call your own custom methods than to call those that are intended to be called by the browser.
Just as the applets array of the Document object contains JavaObjects that represent the applets embedded in a document with the <APPLET> tag, the embeds array of the Document object contains JavaObjects that represent data embedded in a web page with the <EMBED> tag. This is data that is intended to be displayed by a Navigator plug-in. Do not confuse the Document.embeds array with the Navigator.plug-ins array. The first contains objects that represent a single piece of embedded data, and the second contains Plugin objects that represent the actual plug-ins that are installed in Navigator to display embedded data.
Because the objects in the embeds array are provided by plug-ins, the properties and methods of any of these objects will depend on the particular plug-in in use. In general, you'll have to read the vendor's documentation for any given plug-in to determine how to control it through LiveConnect. If the plug-in that is displaying the data is not Java-enabled, then the corresponding object in the embeds array will be a JavaObject that represents a dummy Java object with no functionality.
Example 19.3 shows how you might use the LiveAudio plug-in (bundled with Navigator 3.0 on most platforms) and LiveConnect to automatically play a sound when the user clicks a button and when the mouse passes over a hyperlink. The example relies upon the play() method of the netscape.plugin.Plugin instance provided by the LiveAudio plug-in. This method, and many others, are detailed by Netscape in their LiveAudio documentation.
<!-- Here we embed some sounds in the browser, with attributes to --> <!-- specify that they won't be played when first loaded. In this --> <!-- example, we use sounds found locally on Windows 95 platforms. --> <EMBED SRC="file:///C|/windows/media/Tada.wav" HIDDEN=true AUTOSTART=false> <EMBED SRC="file:///C|/windows/media/Ding.wav" HIDDEN=true AUTOSTART=false> <EMBED SRC="file:///C|/windows/media/The Microsoft Sound.wav" HIDDEN=true AUTOSTART=false> <!-- Here are some buttons that play those sounds. Note the use of the --> <!-- embeds array and the play() method invoked through LiveConnect. --> <FORM> <INPUT TYPE=button VALUE="Play Sound #1" onClick="document.embeds.play()"> <INPUT TYPE=button VALUE="Play Sound #2" onClick="document.embeds.play()"> <INPUT TYPE=button VALUE="Play Sound #3" onClick="document.embeds.play()"> </FORM> <!-- Here's a hypertext link that plays a sound when the user passes over --> <A HREF="" onMouseOver="document.embeds.play()">Click Me</A>