Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package timer;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- /**
- * @author Vita Wiebe
- * @version LA11: Timer
- */
- /**
- * This class generates a simple GUI stopwatch.
- * Chiefly responsible for the visual component of this
- * application.
- */
- public class TimerView {
- // Fields
- // I established the frame title as a final field
- // to make it easier to change later.
- public final String TITLE = "This is a Timer, silly.";
- private enum buttState {STARTED, PAUSED, STOPPED};
- private buttState state;
- private JFrame win;
- // "Output" is the timer's display of the time elapse.
- private JLabel output;
- private TimerThread timerT;
- /**
- * Our constructor. Builds a window and adds all the goodies
- * that make it a fully-functional timer.
- */
- public TimerView() {
- timerT = null;
- // Our top-level element (container).
- win = new JFrame(TITLE);
- win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- win.setSize(500, 500);
- win.setLocation(0, 0);
- // Create the buttons which will control the timer.
- JButton start = new JButton("Start");
- JButton reset = new JButton("Reset");
- state = buttState.STOPPED;
- TimerView self = this;
- start.addMouseListener(new MouseAdapter()
- {
- @Override
- public void mouseClicked(MouseEvent clicked) {
- if(state == buttState.STOPPED) {
- start.setText("Pause");
- state = buttState.STARTED;
- timerT = new TimerThread(self);
- timerT.start();
- } else if (state == buttState.STARTED) {
- start.setText("Continue");
- state = buttState.PAUSED;
- timerT.suspend();
- } else {
- start.setText("Pause");
- state = buttState.STARTED;
- timerT.resume();
- }
- }
- });
- reset.addMouseListener(new MouseAdapter()
- {
- @Override
- public void mouseClicked(MouseEvent clicked) {
- // Stop the timer
- // Reset the timer to zero.
- start.setText("Start");
- state = buttState.STOPPED;
- timerT.stop();
- output.setText("0.00");
- }
- });
- JPanel buttonPanel = new JPanel();
- buttonPanel.setSize(100, 200);
- buttonPanel.add(start);
- buttonPanel.add(reset);
- // This is our visual representation of
- // the output of the timer.
- output = new JLabel("0.00");
- output.setHorizontalAlignment(JLabel.CENTER);
- output.setFont(new Font("Arial", Font.PLAIN, 36));
- win.add(buttonPanel);
- win.add(output);
- win.setVisible(true);
- }
- public void repaint() {
- if(timerT != null) {
- output.setText("" + timerT.getElapsedSeconds() + "."
- + timerT.getElapsedTenths() + timerT.getElapsedHundredths());
- win.repaint();
- }
- }
- // Nested inner class for running the timer.
- class TimerThread extends Thread {
- // Nested fields
- private int elapsedMilliseconds;
- private int elapsedHundredthsOfASecond;
- private int elapsedTenthsOfASecond;
- private int elapsedWholeSeconds;
- private TimerView timerV;
- private boolean running;
- public TimerThread(TimerView t) {
- elapsedMilliseconds = 0;
- elapsedHundredthsOfASecond = 0;
- elapsedTenthsOfASecond = 0;
- elapsedWholeSeconds = 0;
- running = false;
- timerV = t;
- }
- public int getElapsedHundredths() {
- return elapsedHundredthsOfASecond;
- }
- public int getElapsedTenths() {
- return elapsedTenthsOfASecond;
- }
- public int getElapsedSeconds() {
- return elapsedWholeSeconds;
- }
- public void run() {
- while(true) {
- try {
- // Ten is thousandth of a second (a.k.a. millisecond)
- sleep(10);
- while(elapsedHundredthsOfASecond < 9) {
- elapsedMilliseconds += 1;
- if(elapsedMilliseconds == 100) {
- elapsedHundredthsOfASecond += 1;
- elapsedMilliseconds = 0;
- }
- }
- elapsedHundredthsOfASecond = 0;
- while(elapsedTenthsOfASecond < 9) {
- elapsedTenthsOfASecond += 1;
- }
- elapsedTenthsOfASecond = 0;
- elapsedWholeSeconds += 1;
- timerV.repaint();
- } catch(InterruptedException e) {
- System.err.print(e.getMessage());
- }
- }
- }
- }
- /**
- * @param String[] args, the command-line arguments
- * (not used in this application)
- */
- public static void main(String[] args) {
- new TimerView();
- }
- }
Add Comment
Please, Sign In to add comment