Advertisement
Vita_Harvey

TimerView_A

Mar 10th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. package timer;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7.  
  8. /**
  9.  * @author Vita Wiebe
  10.  * @version LA11: Timer
  11.  */
  12.  
  13. /**
  14.  * This class generates a simple GUI stopwatch.
  15.  * Chiefly responsible for the visual component of this
  16.  * application.
  17.  */
  18. public class TimerView {
  19.  
  20.    // Fields
  21.    
  22.    // I established the frame title as a final field
  23.    // to make it easier to change later.
  24.    public final String TITLE = "This is a Timer, silly.";
  25.    
  26.    private enum buttState {STARTED, PAUSED, STOPPED};
  27.    private buttState state;
  28.    
  29.    private JFrame win;
  30.    
  31.    // "Output" is the timer's display of the time elapse.
  32.    private JLabel output;
  33.    
  34.    private TimerThread timerT;
  35.    
  36.    /**
  37.     * Our constructor.  Builds a window and adds all the goodies
  38.     * that make it a fully-functional timer.
  39.     */
  40.    public TimerView() {
  41.    
  42.       timerT = null;
  43.      
  44.       // Our top-level element (container).
  45.       win = new JFrame(TITLE);
  46.       win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  47.       win.setSize(500, 500);
  48.       win.setLocation(0, 0);
  49.      
  50.       // Create the buttons which will control the timer.
  51.       JButton start = new JButton("Start");
  52.       JButton reset = new JButton("Reset");
  53.       state = buttState.STOPPED;
  54.       TimerView self = this;    
  55.       start.addMouseListener(new MouseAdapter()
  56.             {
  57.             @Override
  58.             public void mouseClicked(MouseEvent clicked) {
  59.            
  60.                if(state == buttState.STOPPED) {
  61.                start.setText("Pause");              
  62.                state = buttState.STARTED;
  63.                timerT = new TimerThread(self);
  64.                timerT.start();
  65.                
  66.                } else if (state == buttState.STARTED) {
  67.                      start.setText("Continue");
  68.                      state = buttState.PAUSED;
  69.                      timerT.suspend();
  70.                                              
  71.                } else {
  72.                      start.setText("Pause");              
  73.                      state = buttState.STARTED;
  74.                      timerT.resume();
  75.                }
  76.             }
  77.             });
  78.      
  79.       reset.addMouseListener(new MouseAdapter()
  80.             {
  81.             @Override
  82.             public void mouseClicked(MouseEvent clicked) {                                        
  83.                // Stop the timer
  84.                // Reset the timer to zero.
  85.                start.setText("Start");
  86.                state = buttState.STOPPED;
  87.                timerT.stop();
  88.                output.setText("0.00");
  89.                }
  90.             });
  91.                  
  92.       JPanel buttonPanel = new JPanel();
  93.       buttonPanel.setSize(100, 200);
  94.       buttonPanel.add(start);
  95.       buttonPanel.add(reset);      
  96.      
  97.       // This is our visual representation of
  98.       // the output of the timer.
  99.       output = new JLabel("0.00");
  100.      
  101.       output.setHorizontalAlignment(JLabel.CENTER);
  102.       output.setFont(new Font("Arial", Font.PLAIN, 36));
  103.      
  104.       win.add(buttonPanel);
  105.       win.add(output);
  106.       win.setVisible(true);    
  107.    }
  108.    
  109.    public void repaint() {      
  110.       if(timerT != null) {
  111.          output.setText("" + timerT.getElapsedTime());
  112.          win.repaint();
  113.       }    
  114.    }
  115.      
  116.    
  117.    // Nested inner class for running the timer.
  118.    class TimerThread extends Thread {
  119.    
  120.       // Nested fields
  121.      
  122.       private double elapsedTime;
  123.       private TimerView timerV;
  124.       private boolean running;
  125.      
  126.       public TimerThread(TimerView t) {
  127.          elapsedTime = 0.00;        
  128.          running = false;
  129.          timerV = t;
  130.       }
  131.    
  132.       public double getElapsedTime() {
  133.          return elapsedTime;
  134.       }
  135.      
  136.       public void run() {
  137.          while(true) {
  138.             try {
  139.                // Ten is thousandth of a second (a.k.a. millisecond)
  140.                sleep(10);            
  141.                elapsedTime += 0.01;
  142.                timerV.repaint();
  143.             } catch(InterruptedException e) {
  144.                   System.err.print(e.getMessage());
  145.             }
  146.          }        
  147.       }
  148.    }
  149.    
  150.    /**
  151.     * @param String[] args, the command-line arguments
  152.     * (not used in this application)
  153.     */
  154.    public static void main(String[] args) {
  155.       new TimerView();
  156.          
  157.    }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement