Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Timer;
- import java.util.TimerTask;
- import java.awt.event.*;
- public class EggTimer implements ActionListener {
- private Timer timer;
- private final int startMinutes;
- private int minutes;
- private int seconds;
- private boolean isCounting = false;
- private boolean isStopped = false;
- private NumberDisplay nDisplay;
- public EggTimer(int minutes) {
- nDisplay = new NumberDisplay(minutes);
- this.minutes = minutes;
- this.startMinutes = minutes;
- nDisplay.getStart().addActionListener(this);
- nDisplay.getStop().addActionListener(this);
- nDisplay.getReset().addActionListener(this);
- nDisplay.getSetTime().addActionListener(this);
- }
- public void start() {
- timer = new Timer();
- timer.schedule(new TimerTask() {
- public void run() {
- int secs = getSeconds();
- int mins = getMinutes();
- if( secs == 0 && mins == 0 ) {
- playSound();
- return;
- }
- if( secs > 0 ) {
- setSeconds( secs - 1 );
- } else {
- setMinutes( mins - 1 );
- setSeconds( 59 );
- }
- }
- }, 0, 1000);
- }
- public int getMinutes() {
- return minutes;
- }
- public void setMinutes(int newValue) {
- minutes = newValue;
- nDisplay.setMinute(minutes);
- }
- public int getSeconds() {
- return seconds;
- }
- public void setSeconds(int newValue) {
- seconds = newValue;
- nDisplay.setSecond(newValue);
- }
- private void playSound() {
- }
- public void actionPerformed(ActionEvent e) {
- String command = e.getActionCommand();
- if(command.equals("Start")) {
- if(isCounting == false && isStopped == false) {
- isCounting = true;
- start();
- } else if (isCounting == false && isStopped == true) {
- isCounting = true;
- isStopped = false;
- start();
- }
- } else if(command.equals("Stop")) {
- if(isCounting == true && isStopped == false) {
- try {
- isCounting = false;
- isStopped = true;
- timer.cancel();
- } catch (IllegalMonitorStateException imserr) {
- System.out.println(imserr.getMessage());
- }
- } else if (isStopped == true) {
- }
- } else if(command.equals("Reset")) {
- setMinutes(startMinutes);
- setSeconds(0);
- }
- }
- }
Add Comment
Please, Sign In to add comment