Hello, I have two assignments StopWatch.Java and Vote.Java. I'm supposed to use applet, but for some reason my programs don't run in the browser. Originally I was using Chrome and I found out that Chrome does not allow applet to run. I made Edge my default browser and still did not run. I need help. Here are the codes below.
StopWatch.java
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");
}
}
}
Vote.java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class Vote extends JApplet{
JLabel heading;
JButton candidate1;
JButton candidate2;
JButton results;
JLabel message;
public void init(){
heading = new JLabel("Vote for your Leader");
heading.setBounds(110, 45, 255, 55);
candidate1=new JButton("Vote for Candidate1");
candidate1.setBounds(110, 110, 210, 55);
candidate2=new JButton("Vote for Candidate2");
candidate2.setBounds(110,165,210,55);
results = new JButton("See Results");
results.setBounds(110,225,210,55);
message = new JLabel("");
message.setBounds(110,275,210,55);
candidate1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
message.setText("Candidate 1 was elected");
}
});
candidate2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
message.setText("Candidate 2 was elected");
}
});
results.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random random = new Random();
int winner = random.nextInt(2);
switch (winner){
case 0:
message.setText("Candidate 1 is winner");
break;
case 1:
message.setText("Candidate 2 is winner");
break;
default:
message.setText("Draw");
break;
}
}
});
add(candidate1);
add(candidate2);
add(results);
add(message);
add(heading);
setLayout(null);
}
}