tags:

views:

183

answers:

2

I am trying to make several rectangles show up when the applet is started with a couple colors added in...I already have the beep audio file but from what i know, it will play throughout the whole program. Can someone please tell me how to make the beep sound occur only when the rectangles are clicked?

+1  A: 

There is some simple Audio API directly in Applet that you can use like this:

public class MyApplet extends Applet
{
    protected AudioClip beep;
    protected relativeBeepPath = "mybeep.wav";

    public void init()
    {
        //....
        try {
            beep = getAudioClip( new URL( getDocumentBase(), relativeBeepPath ) );
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
    }

    public void myRectangleWasClicked()
    {
        if( beep != null )
            beep.play();
    }
}
x4u
A: 

thank for you answer...however, when i put this code into my program, it decided not to run. I probably should have posted my code before but here it is now...

p.s.-my code currently doesnt run so can you also tell me how to fix it. thanks


import java.awt.; import java.applet.; import javax.swing.*;

public class rectangles_java extends JApplet {

public static void main(String[] args)

{

Toolkit.getDefaultToolkit().beep();

}

public void init() { setBackground(Color.BLACK); }

public void paint ( Graphics g ) 
{
    g.setColor(Color.BLUE);
    g.drawString("This is the drawing rectangle example",0, 25);

    g.drawRect( 70,70,65,50 );
    g.drawRect( 51,65,27,11 );
    g.drawRect( 34,91,47,68 );
    g.drawRect( 120,12,150,91);

}

}

nubprogrammer