Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
import java.awt.*;
import java.awt.event.*;
public class Counter extends java.applet.Applet implements ActionListener, Runnable {
TextField field;
int bits = 0;
int seconds = 20;
int minutes = 0;
boolean interrupt;
Thread stopwatch = null;
String str = "";
public void init() {
field = new TextField(32);
field.setEditable(false);
setFont(new Font("Arial", 0, 14));
Button start = new Button("Start");
Button stop = new Button("Stop");
start.addActionListener(this);
stop.addActionListener(this);
add(field);
add(start);
add(stop);
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.setFont(new Font("Arial", 0, 18));
g.fillRect(5, 3, 540, 54);
g.setColor(Color.yellow);
g.drawString("Timer : ", 15, 25);
g.drawString(str, 15, 50);
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
if(comm.equals("Start")) {
interrupt = true;
start();
}
if(comm.equals("Stop")) {
interrupt = false;
if (seconds > 0) str = "Question completed: " + field.getText();
else str = "Sorry out of time.";
}
repaint();
}
public void start() {
if(stopwatch == null) {
stopwatch = new Thread(this, "StopWatchApplet");
stopwatch.start();
}
}
public void run() {
Thread thread = Thread.currentThread();
while (stopwatch == thread)
{
try
{
while(seconds != 0 && interrupt)
{
Thread.sleep(10);
bits++;
if(bits % 100 == 0 && bits != 0)
{
bits -= 100;
seconds--;
}
if(!interrupt) --bits;
field.setText(+minutes+" mins : "+seconds+" secs: "+bits+" hundreds of a second.");
}
}
catch(Exception ignore) { }
}
}
public void update(Graphics g) { paint(g); }
}