Skip to content

Instantly share code, notes, and snippets.

@553224019
Created June 13, 2019 17:04
Show Gist options
  • Select an option

  • Save 553224019/b3f3fe1639d25fd4fcf16f59c513ec34 to your computer and use it in GitHub Desktop.

Select an option

Save 553224019/b3f3fe1639d25fd4fcf16f59c513ec34 to your computer and use it in GitHub Desktop.
Simple stopwatch created using Swing
import javax.swing.*;
import java.awt.*;
public class Stopwatch extends JFrame {
public static void main(String[] s) {
JFrame j = new JFrame();
j.setVisible(true);
j.setSize(200, 300);
j.setResizable(false);
j.setTitle("Watch");
Box box = Box.createVerticalBox();
WatchPanel watch = new WatchPanel(20, 20, 70);
watch.setSize(200, 220);
watch.setLocation(0, 0);
box.add(watch);
Box controller = Box.createVerticalBox();
JButton startBtn = new JButton();
startBtn.setText("START");
startBtn.addActionListener(e -> watch.start());
JButton pauseBtn = new JButton();
pauseBtn.setText("PAUSE");
pauseBtn.addActionListener(e -> watch.pause());
JButton resetBtn = new JButton();
resetBtn.setText("RESET");
resetBtn.addActionListener(e -> watch.reset());
controller.add(startBtn);
controller.add(pauseBtn);
controller.add(resetBtn);
controller.setLocation(200, 0);
box.add(controller);
j.add(box);
}
}
class WatchPanel extends JPanel implements Runnable {
private int x, y, r;
private long startTimeMillis, accTimeMillis = 0;
private boolean ticking = false, reset = false;
WatchPanel(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
this.startTimeMillis = System.currentTimeMillis();
new Thread(this).start();
}
void start() {
this.startTimeMillis = System.currentTimeMillis();
this.ticking = true;
this.reset = false;
}
void pause() {
this.accTimeMillis += System.currentTimeMillis() - this.startTimeMillis;
this.ticking = this.reset = false;
}
void reset() {
this.accTimeMillis = 0;
this.startTimeMillis = System.currentTimeMillis();
this.ticking = false;
this.reset = true;
}
@Override
public void paint(Graphics g) {
super.paint(g);
int dx, dy;
final double RAD = Math.PI / 180;
long sumTimeMillis = accTimeMillis + System.currentTimeMillis() - startTimeMillis;
int min = (int) sumTimeMillis / 60000;
int sec = (int) (sumTimeMillis / 1000) % 60;
g.setColor(Color.WHITE);
g.fillRect(0, 0, r * 3, r * 3);
g.setColor(Color.BLACK);
g.drawOval(x, y, r * 2, r * 2);
g.setColor(Color.RED);
dx = (int) ((r - 10) * Math.sin(6 * RAD * sec));
dy = (int) ((r - 10) * Math.cos(6 * RAD * sec));
g.drawLine(x + r, y + r, x + r + dx, y + r - dy);
g.setColor(Color.BLUE);
dx = (int) ((r - r / 2.5) * Math.sin(6 * RAD * min));
dy = (int) ((r - r / 2.5) * Math.cos(6 * RAD * min));
g.drawLine(x + r, y + r, x + r + dx, y + r - dy);
g.setColor(Color.BLACK);
int d = 28;
for (int i = 1; i <= 12; i++) {
dx = (int) ((r - 10) * Math.sin(RAD * d));
dy = (int) ((r - 10) * Math.cos(RAD * d));
g.drawString(String.valueOf(i), x + r + dx - 4, x + r - dy + 5);
d += 30;
}
}
@Override
public void run() {
while (true) {
if (ticking)
this.repaint();
try {
Thread.sleep(200);
} catch (Exception ignored) {}
if (reset) {
this.repaint();
this.reset = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment