Stopwatch


SUBMITTED BY: knightley

DATE: Sept. 3, 2015, 9:35 a.m.

FORMAT: Java

SIZE: 4.2 kB

HITS: 1101

  1. import javax.swing.JFrame;
  2. ///driver class
  3. public class Stopwatch
  4. {
  5. public static void main(String[] args)
  6. {
  7. JFrame frame = new JFrame("Stopwatch 9.10 Haris Cengic");
  8. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  9. frame.getContentPane().add(new StopWatchPanel());
  10. frame.pack();
  11. frame.setVisible(true);
  12. }
  13. };
  14. //--------------------------------------------------------------//
  15. import javax.swing.JPanel;
  16. import javax.swing.JButton;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ActionEvent;
  19. import javax.swing.JLabel;
  20. import javax.swing.Timer;
  21. import java.awt.Color;
  22. import java.awt.Font;
  23. import java.awt.Dimension;
  24. import java.awt.Graphics;
  25. public class StopWatchPanel extends JPanel
  26. {
  27. private final int START = 100; //START will be set to 100, keeps the timer accurate
  28. private final int WIDTH = 500, HEIGHT = 110; //size of jpanel
  29. private final int SIZE = 80; // size of the font for the stopwatch
  30. private int second; //shows the current second
  31. private boolean hasstarted; //check if the timer is running
  32. private Timer timer; //new timer
  33. private JButton start, stop, clear; //buttons
  34. private JLabel time; //shows the time in seconds
  35. public StopWatchPanel()
  36. {
  37. timer = new Timer(START, new TimerListener());
  38. second = 0;
  39. hasstarted = true; //set the time to 0 and start the timer
  40. start = new JButton("Start");//new button for start option
  41. start.addActionListener(new ButtonListener());
  42. clear = new JButton("Clear");//new button for clear
  43. clear.addActionListener(new ButtonListener());
  44. time = new JLabel("0.0");
  45. time.setFont(new Font("Arial", Font.ITALIC, SIZE));
  46. add(start);
  47. add(clear);
  48. add(time);
  49. setPreferredSize(new Dimension(WIDTH, HEIGHT));
  50. setBackground(Color.red);
  51. }
  52. public final void paintComponent(Graphics page)
  53. {
  54. super.paintComponent(page); //super.paintCompenent invokes paintComponent and it overwrites whatever is there
  55. final int milli = 10; //base 10, dividing second by 10 and %10 to display the time in seconds AND milliseconds
  56. time.setText(" " + second / milli + "." + second % milli + " ");
  57. }
  58. private class ButtonListener implements ActionListener
  59. {
  60. //this class tells if the timer is running and based
  61. //on that changes the button from 'start' to 'stop'
  62. //also it defines what 'clear' does
  63. public void actionPerformed(ActionEvent event)
  64. {
  65. if (event.getSource() == start)
  66. {
  67. if (hasstarted)
  68. {
  69. hasstarted = false;
  70. timer.start();
  71. start.setText("Stop");
  72. } else
  73. {
  74. hasstarted = true;
  75. timer.stop();
  76. start.setText("Start");
  77. }
  78. } else if (event.getSource() == clear)
  79. {
  80. second = 0;
  81. hasstarted = true;
  82. timer.stop();
  83. start.setText("Start");
  84. }
  85. repaint();
  86. }
  87. }
  88. private class TimerListener implements ActionListener
  89. {
  90. public void actionPerformed(ActionEvent event)
  91. {
  92. second++;
  93. repaint();
  94. }
  95. }
  96. }
  97. //----------------------------------------------------------------------------//

comments powered by Disqus