Skip to content

Instantly share code, notes, and snippets.

Created December 16, 2017 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/26a685c92a6278a33fa5d5c954a3026b to your computer and use it in GitHub Desktop.
Save anonymous/26a685c92a6278a33fa5d5c954a3026b to your computer and use it in GitHub Desktop.
MyTimer
package mytimer.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
class ButtonPanel extends JPanel {
private final ActionListener listener;
ButtonPanel(ActionListener listener) {
this.listener = listener;
MyButton start = new MyButton("Start");
MyButton stop = new MyButton("Stop");
MyButton reset = new MyButton("Reset");
add(start);
add(stop);
add(reset);
}
class MyButton extends JButton {
MyButton(String text) {
this(text, new Dimension(100, 48));
}
MyButton(String text, Dimension buttonSize) {
super(text);
setForeground(Color.BLACK);
setBackground(Color.WHITE);
setVerticalAlignment(JButton.CENTER);
setHorizontalAlignment(JButton.CENTER);
setActionCommand(text);
setFont(
new Font("Arial", Font.PLAIN, 24)
);
setPreferredSize(buttonSize);
addActionListener(listener);
}
}
}
package mytimer.gui;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JFrame {
private ButtonPanel buttonPanel;
private TimePanel timePanel;
public GUI(String title) {
super(title);
setLayout(new BorderLayout());
add(
buttonPanel = new ButtonPanel(new ButtonListener()),
BorderLayout.CENTER
);
add(
timePanel = new TimePanel(),
BorderLayout.SOUTH
);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand().toLowerCase()) {
case "start":
timePanel.startTimer();
break;
case "stop":
timePanel.stopTimer();
break;
case "reset":
timePanel.resetTimer();
break;
default:
System.out.println(e);
}
}
}
}
package mytimer;
import mytimer.gui.GUI;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class MyTimer {
MyTimer() {
GUI gui = new GUI("My timer");
gui.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new MyTimer();
});
}
}
package mytimer.util;
import java.util.Arrays;
import java.util.List;
public class TimeCount {
private int hour = 0, minute = 0, second = 0;
public TimeCount countUpSeconds(int secDelta) {
if(secDelta == 0) return this;
second += secDelta;
List<Integer> tmp = TimeCount.divmod(second, 60);
if(tmp.get(0) == 0) return this;
second = tmp.get(1);
return countUpMinutes(tmp.get(0));
}
public TimeCount countUpMinutes(int minDelta) {
if(minDelta == 0) return this;
minute += minDelta;
List<Integer> tmp = TimeCount.divmod(minute, 60);
if(tmp.get(0) == 0) return this;
minute = tmp.get(1);
return countUpHours(tmp.get(0));
}
public TimeCount countUpHours(int hourDelta) {
if(hourDelta == 0) return this;
hour += hourDelta;
return this;
}
// Python-like divmod
private static List<Integer> divmod(int a, int b) {
return Arrays.asList(a / b, a % b);
}
@Override
public String toString() {
return String.format("%02d:%02d.%02d", hour, minute, second);
}
}
package mytimer.gui;
import mytimer.util.TimeCount;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
class TimePanel extends JPanel {
private Timer timer;
private TimeCount timeCount = new TimeCount();
private JLabel timeLabel;
TimePanel() {
timeLabel = new JLabel(
timeCount.toString()
);
timeLabel.setFont(
new Font("Arial", Font.BOLD, 80)
);
add(timeLabel);
timer = new Timer(1000, e -> {
timeCount.countUpSeconds(1);
update();
});
}
void update() {
timeLabel.setText(timeCount.toString());
repaint();
}
void resetTimer() {
timeCount = new TimeCount();
update();
}
void startTimer() {
timer.start();
}
void stopTimer() {
timer.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment