Steve C.

asked • 11/08/19

My Java program will not run in the browser, no sound either. Why?

import java.applet.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.applet.AudioClip;


@SuppressWarnings("deprecation")

public class StopWatch extends Applet implements Runnable, ActionListener {


// Panel to keep all the buttons and labels

Panel p;

Label displayTimer; // label for display times


AudioClip audioClip;


// Button start stop

Button start, stop;


// Time for hour. minute and second

int hour, minute, second;


// string to be displayed on the label of time

String disp;


// State of stopwatch on/off

boolean on;


// initialization

public void init() {

// initially off

on = false;


p = new Panel();

// Setting grid layout of the panel

p.setLayout(new GridLayout(4, 1, 6, 10));


// set the initial time to 00 : 00 : 00

hour = minute = second = 0;


// Label

displayTimer = new Label();

disp = "00 : 00 : 00";

displayTimer.setText(disp);

p.add(displayTimer);


// Start button

start = new Button("Start");

start.addActionListener((ActionListener) this); // create action listener for start button

p.add(start);


// Stop button

stop = new Button("Stop"); // create action listener for stop button

stop.addActionListener((ActionListener) this);

p.add(stop);


add(p);


audioClip = getAudioClip(getCodeBase(), "stopSound.wav"); // play audion clip stop


// starting thread

new Thread(this, "StopWatch").start();

}


// Reset Function

// reset to default value

public void reset() {

try {

Thread.sleep(1);

} catch (Exception e) {

System.out.println(e);

}

hour = minute = second = 0;

}


// update method will update the timer

public void update() {

second++; // increment the second

if (second == 60) { // set the second to 0 if second reached 60

second = 0;

minute++;

if (minute == 60) { // set the minute to 0 if minute reached 60

minute = 0;

hour++; // count the hour

}

}

}


// changing label

public void changeLabel() {


// Properly formatting the display of the timer

if (hour < 10)

disp = "0" + hour + " : ";

else

disp = hour + " : ";


if (minute < 10)

disp += "0" + minute + " : ";

else

disp += minute + " : ";


if (second < 10)

disp += "0" + second + " : ";

else

disp += second;


displayTimer.setText(disp); // set the label text

}


// thread.run function

public void run() {


// while the stopwatch is on

while (on) {

try {

// pause 1 millisecond

Thread.sleep(1);

// update the time

update();

// changeLabel

changeLabel();

} catch (InterruptedException e) {

System.out.println(e);

}

}

}


// actionPerformed To listen to the actions on the start/ stop buttons

public void actionPerformed(ActionEvent e) {


// start a thread when start button is clicked

if (e.getSource() == start) { // check for the start button pressed

// stopwatch is on

on = true;

new Thread(this, "StopWatch").start();

}


if (e.getSource() == stop) { // check for the stop button pressed

// stopwatch off

on = false;

System.out.println("Stop sound");

audioClip.stop();

System.out.println("Stop sound1");

}

}

}

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.