Guest User

Untitled

a guest
Feb 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3. import java.awt.event.*;
  4.  
  5. public class EggTimer implements ActionListener {
  6.     private Timer timer;
  7.     private final int startMinutes;
  8.     private int minutes;
  9.     private int seconds;
  10.     private boolean isCounting = false;
  11.     private boolean isStopped = false;
  12.     private NumberDisplay nDisplay;
  13.  
  14.     public EggTimer(int minutes) {
  15.         nDisplay = new NumberDisplay(minutes);
  16.         this.minutes = minutes;
  17.         this.startMinutes = minutes;
  18.         nDisplay.getStart().addActionListener(this);
  19.         nDisplay.getStop().addActionListener(this);
  20.         nDisplay.getReset().addActionListener(this);
  21.         nDisplay.getSetTime().addActionListener(this);
  22.     }
  23.  
  24.     public void start() {
  25.         timer = new Timer();
  26.         timer.schedule(new TimerTask() {
  27.             public void run() {
  28.                 int secs = getSeconds();
  29.                 int mins = getMinutes();
  30.                 if( secs == 0 && mins == 0 ) {
  31.                     playSound();
  32.                     return;
  33.                 }
  34.                 if( secs > 0 ) {
  35.                     setSeconds( secs - 1 );
  36.                 } else {
  37.                     setMinutes( mins - 1 );
  38.                     setSeconds( 59 );
  39.                 }
  40.             }
  41.         }, 0, 1000);
  42.     }
  43.    
  44.     public int getMinutes() {
  45.         return minutes;
  46.     }
  47.    
  48.     public void setMinutes(int newValue) {
  49.         minutes = newValue;
  50.         nDisplay.setMinute(minutes);
  51.     }
  52.    
  53.     public int getSeconds() {
  54.         return seconds;
  55.     }
  56.    
  57.     public void setSeconds(int newValue) {
  58.         seconds = newValue;
  59.         nDisplay.setSecond(newValue);
  60.     }
  61.    
  62.     private void playSound() {
  63.        
  64.     }
  65.    
  66.     public void actionPerformed(ActionEvent e) {
  67.         String command = e.getActionCommand();
  68.        
  69.         if(command.equals("Start")) {
  70.             if(isCounting == false && isStopped == false) {
  71.                 isCounting = true;
  72.                 start();
  73.             } else if (isCounting == false && isStopped == true) {
  74.                 isCounting = true;
  75.                 isStopped = false;
  76.                 start();
  77.             }
  78.            
  79.         } else if(command.equals("Stop")) {
  80.             if(isCounting == true && isStopped == false) {
  81.                 try {
  82.                         isCounting = false;
  83.                         isStopped = true;
  84.                         timer.cancel();
  85.                     } catch (IllegalMonitorStateException imserr) {
  86.                         System.out.println(imserr.getMessage());
  87.                     }
  88.                 } else if (isStopped == true) {
  89.                    
  90.                 }
  91.         } else if(command.equals("Reset")) {
  92.             setMinutes(startMinutes);
  93.             setSeconds(0);
  94.         }            
  95.     }
  96. }
Add Comment
Please, Sign In to add comment