Advertisement
Vita_Harvey

TimerView_A

Mar 10th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 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.getElapsedSeconds() + "."
  112.             + timerT.getElapsedTenths() + timerT.getElapsedHundredths());          
  113.          win.repaint();
  114.       }    
  115.    }
  116.      
  117.    
  118.    // Nested inner class for running the timer.
  119.    class TimerThread extends Thread {
  120.    
  121.       // Nested fields
  122.      
  123.       private int elapsedMilliseconds;
  124.       private int elapsedHundredthsOfASecond;
  125.       private int elapsedTenthsOfASecond;
  126.       private int elapsedWholeSeconds;
  127.       private TimerView timerV;
  128.       private boolean running;
  129.      
  130.       public TimerThread(TimerView t) {
  131.          elapsedMilliseconds = 0;
  132.          elapsedHundredthsOfASecond = 0;
  133.          elapsedTenthsOfASecond = 0;
  134.          elapsedWholeSeconds = 0;      
  135.          running = false;
  136.          timerV = t;
  137.       }
  138.    
  139.       public int getElapsedHundredths() {
  140.          return elapsedHundredthsOfASecond;
  141.       }
  142.      
  143.       public int getElapsedTenths()  {
  144.          return elapsedTenthsOfASecond;
  145.       }
  146.      
  147.       public int getElapsedSeconds() {
  148.          return elapsedWholeSeconds;
  149.       }
  150.      
  151.       public void run() {
  152.          while(true) {
  153.             try {
  154.                // Ten is thousandth of a second (a.k.a. millisecond)
  155.                sleep(10);
  156.                while(elapsedHundredthsOfASecond < 9) {
  157.                   elapsedMilliseconds += 1;
  158.                   if(elapsedMilliseconds == 100) {            
  159.                      elapsedHundredthsOfASecond += 1;
  160.                      elapsedMilliseconds = 0;
  161.                   }
  162.                }
  163.                elapsedHundredthsOfASecond = 0;
  164.                while(elapsedTenthsOfASecond < 9) {
  165.                   elapsedTenthsOfASecond += 1;
  166.                }
  167.                elapsedTenthsOfASecond = 0;
  168.                elapsedWholeSeconds += 1;
  169.                timerV.repaint();
  170.             } catch(InterruptedException e) {
  171.                   System.err.print(e.getMessage());
  172.             }
  173.          }        
  174.       }
  175.    }
  176.    
  177.    /**
  178.     * @param String[] args, the command-line arguments
  179.     * (not used in this application)
  180.     */
  181.    public static void main(String[] args) {
  182.       new TimerView();
  183.          
  184.    }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement