Home | Java Menu | Java Applets | Java Tips | E-mail Us
View Java Applet


/* Sound.java
  *
  * A Java Applet demonstrating multimedia sound.
  *
  * Authored by Khim Theng
  * khim_theng@completeesolutions.com
  *
  * Last modified on 05/11/2000
  */

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Sound extends Applet implements ActionListener
{
    private String sndName;  //String variable for sound
    private AudioClip snd;  //AudioClip variable
    private Button onOffBtn;  //Button to turn sound on and off
    private boolean isPlaying = true;  //Sound flag

    public void init ()
    {
        sndName = getParameter("SOUND");  //Get sound file from the <PARAM> tag in the HTML file

        if (sndName != null)  //If sndName is not empty
            snd = getAudioClip(getCodeBase(), sndName);  //Find the sndName file in the same directory as the .class file

        setBackground(Color.white);  //Set the background color to white

        //Create the sound button
        onOffBtn = new Button("Quiet Down The Pig");  //Assign "Quiet Down The Pig" as the label on the button
        onOffBtn.addActionListener(this);  //Add this as the ActionListener for the sound button

        setLayout(new BorderLayout());

        //Create Panel pig to hold the button
        Panel pig = new Panel();
            pig.add(onOffBtn);  //Add sound button to the Panel pig

        add(pig, "Center");  //Add Panel pig to the BorderLayout

        setVisible(true);
    }  //End of init

    public void start ()
    {
        if (sndName != null && isPlaying)
            snd.loop();  //Play audio clip in a loop
    }

    public void stop ()
    {
        if (sndName != null)
            snd.stop(); 
    }

    public void actionPerformed (ActionEvent ae)
    {
        if (sndName == null)
            return;

        if (isPlaying)
        {
            snd.stop();  //Stop sound
            onOffBtn.setLabel("Listen To Pig Music");  //Change label on button
        }
        else
        {
            snd.loop();  //Play sound repeatedly
            onOffBtn.setLabel("Quiet Down The Pig");  //Change label on button
        }
        isPlaying = !isPlaying;
        onOffBtn.invalidate();  //Forces onOffBtn to be resized with new caption
    }  //End of actionPerformed

}  //End of Sound
                
View Java Applet

Home | Java Menu | Java Applets | Java Tips | Legal
© 1999-2003 Complete e Solutions, Inc.