Consider the following fragment from a Java applet:
class Buzzer extends Applet { public int pitch; public void buzz() { //Play a sound at the specified pitch. ... } ... }
To include this applet in your HTML page, use the <APPLET> tag, specifying the ID attribute. This enables you to refer to the applet from the scripting language, as shown in the following example:
<APPLET CODE="Buzzer.class" ID=doorbell>
You can then define buttons that enable the user to control the applet, as shown in the following example:
<INPUT TYPE=button VALUE="Higher" NAME="BtnHigher"> <INPUT TYPE=button VALUE="Lower" NAME="BtnLower"> <INPUT TYPE=button VALUE="Play" NAME="BtnPlay">
The NAME attribute lets you refer to each button from the scripting language.
In the scripting portion of your HTML page, you can define OnClick handlers for each button, making them manipulate the applet, as follows:
<SCRIPT language = "VBScript"> Sub BtnHigher_OnClick document.doorbell.pitch = 880 End Sub Sub BtnLower_OnClick document.doorbell.pitch = 440 End Sub Sub BtnPlay_OnClick document.doorbell.buzz End Sub </SCRIPT>
Now, when a user clicks a button on the Web page, the appropriate VBScript handler is invoked, which in turn manipulates the Java applet. Note that when the applet is referenced from VBScript, its name has the identifier document attached to it.
Using scripting, Java applets can be driven just like ActiveX controls. The script can read and write most of the public variables of the applet, as well as call its public methods (including passing parameters and reading return values). However, only classes derived from java.Applet class are directly accessible from the scripting language. If your Java applet includes other classes that you want to make available to the scripting language, you must define public methods in your Applet subclass that delegate to those classes.
Note Returning a long data type from a Java applet to a web page script causes an overflow error. This is due to rounding of the maximum and minimum values of the long integer. Specifically, when placed in a com.ms.com.Variant, a Java long integer becomes a VT_R8, or a Variant.VariantDouble. When the upper and lower limits for a Java long integer are returned to the script with a Variant, the resultant value's last four digits are rounded from 5808 and 5807 to 6000.
You can also embed both Java applets and ActiveX controls in the same Web page and use VBScript handlers to connect them. For example, you can read values from an ActiveX control and pass them to a Java applet, or vice versa. You can even fire events from a Java applet. This automatic scripting capability is provided by the Microsoft VM. As a result, your script code can manipulate Java applets developed with any tool, not just ones developed with Microsoft® Visual J++®.