Created
December 16, 2017 13:12
-
-
Save anonymous/26a685c92a6278a33fa5d5c954a3026b to your computer and use it in GitHub Desktop.
MyTimer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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