|
The audio output of the news ticker can be remote controlled via Java or JavaScript. Therefor the applet provides two public methods: public void enableSound (boolean enable); public boolean isSoundEnabled (); The method "enableSound" turns the audio output on or off. With the second method "isSoundEnabled" the current status of the audio output can be tested. Both methods can be addressed over JavaScript or Java. The following two examples show how does it work. | ||||
|
This news ticker switches the audio output on or off via JavaScript. The ticker applet has the name "Ticker1". It can be addressed via this name: <applet code=TickerLine.class name="Ticker1" width=369 height=44> A button implemented using JavaScript, named "soundButton", is displayed to the right side of the ticker as shown below: <a href="#" onMouseOver="selectSound(1)" onMouseOut="selectSound(0)" onClick="setSound(); return false;"> <img src="PlaySound.gif" name="soundButton" width=30 height=28 border=0> </a> The reference to "#" at the anchor tag is necessary only for Netscape browsers. The event handler "onClick" in the anchor tag works in all Netscape browsern only if a reference is given. The "return false;" at the handler "onClick" causes that the reference do not execute. One variable and two functions take over now the visualization of the buttons and the control of the audio output: var isSoundEnabled = false; function selectSound (selected) { var imageName; if (isSoundEnabled) imageName = "StopSound"; else imageName = "PlaySound"; if (selected) imageName += "-select"; if (document.images) document.soundButton.src = imageName + ".gif"; } function setSound () { enableSound = !enableSound; document.Ticker1.enableSound (isSoundEnabled); selectSound (1); } The variable "isSoundEnabled" stores the status of the audio output and is set initially to the value "false". In the function "selectSound" the picture name is assembled first. If the sound output is activated, then the picture name begins with "StopSound", otherwise with "PlaySound". If the mouse is over the button, then the argument "selected" has the value "1" and the string "-select" is attached to the name. Finally the string ".gif" is added and the complete picture name is assigned to the SoundButton. Here are the four pictures "PlaySound.gif", "PlaySound-select.gif", "StopSound.gif" and "StopSound-select.gif": The safety query "if (document.images)" is special for the Microsoft Explorer 3, because this browser doesn't know the object "images" and would produce an error message otherwise. The function "setSound" switches the variable "isSoundEnabled" and sets then the audio output in the applet "Ticker1" with the method "enableSound ()" on or off. Afterwards it draws the button again by calling "selectSound (1)". | ||||
|
In this example the audio output is switched on and off by an second applet. Interapplet communication is used. Again a unique name "Ticker2" is assigned to the applet: <applet code=TickerLine.class name="Ticker2" width=369 height=44> This name will be transfered to the second applet "SoundButton" with the parameter "receiver": <applet code=SoundButton.class width=30 height=28> <param name=BackgroundColor value="#f5f5f3"> <param name=ButtonImage value="SoundButton.gif"> <param name=Receiver value="Ticker2"> <param name=OnMessage value="Play sound"> <param name=OffMessage value="Stop sound"> </applet> The other parameters determine the background color of the applet, the image file with the different button phases and the message for the status line. They are un-important for the understanding of the interapplet communication and are not described here. The control of the news ticker takes place via the following code fragment: private boolean isSoundEnabled = false; try { isSoundEnabled = !isSoundEnabled; Applet receiver = getAppletContext ().getApplet (getParameter ("Receiver")); ((TickerLine) receiver).enableSound (isSoundEnabled); } catch (Exception exception) {} The variable "isSoundEnabled" stores the current status of the audio output and is initialized with the value "false". In order to catch possible errors in a simple manner, the following statements are embedded in a try block. First the value of the variable "isSoundEnabled" will be switched. The second statement reads the parameter "receiver"-it contains the name "Ticker2" as an argument-and searches the HTML page for an applet with this name. This applet is then casted in the next line to the class "TickerLine" and the method "enableSound" of this class is called. The value is transferred to the variable "isSoundEnabled" as an argument. | ||||