Guest User

Untitled

a guest
Jun 20th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. // StopWatchPanel
  2. // Simple multiple-stopwatch application (Panel)
  3. // Author: Christopher Vo (cvo1@cs.gmu.edu)
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.io.*;
  8. import java.util.regex.*;
  9. import javax.sound.sampled.*;
  10. import javax.swing.*;
  11.  
  12. public class StopWatchPanel extends JPanel implements ActionListener, Runnable {
  13. private static final long serialVersionUID = 1220584852629632807L;
  14. private static final Insets stdInset = new Insets(5, 5, 5, 5);
  15. private JTextField name, time;
  16. private JButton startStopButton, resetButton;
  17. private long setTime, lapTime, startTime, timeLeft;
  18. private Boolean running = false;
  19. private static final String SOUND_PIKACHU = "pikachu.wav";
  20. private Thread timerThread;
  21. private Clip currentClip;
  22. private final Runnable displayUpdater = new Runnable() {
  23. public void run() {
  24. time.setText(millisToStr(timeLeft));
  25. }
  26. };
  27.  
  28. private void saveTime() {
  29. time.getCaret().setVisible(false);
  30. time.setEditable(false);
  31. setTime = strToMillis(time.getText());
  32. lapTime = setTime;
  33. time.setText(millisToStr(setTime));
  34. }
  35.  
  36. public StopWatchPanel(final JFrame parentFrame) {
  37.  
  38. // name field ------------------------------------------
  39. name = new JTextField("Event");
  40. name.setMargin(stdInset);
  41. name.setEditable(false);
  42. // double click: edit
  43. name.addMouseListener(new MouseAdapter() {
  44. public void mouseClicked(MouseEvent e) {
  45. if (e.getClickCount() >= 2)
  46. name.setEditable(true);
  47. }
  48. });
  49. // focus lost: save
  50. name.addFocusListener(new FocusAdapter() {
  51. public void focusLost(FocusEvent e) {
  52. name.setEditable(false);
  53. parentFrame.pack();
  54. }
  55. });
  56. // enter: save
  57. name.addKeyListener(new KeyAdapter() {
  58. public void keyPressed(KeyEvent e) {
  59. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  60. name.setEditable(false);
  61. parentFrame.pack();
  62. }
  63. }
  64. });
  65.  
  66. // time field ------------------------------------------
  67. time = new JTextField("00:00:00.000");
  68. time.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
  69. time.setMargin(stdInset);
  70. time.setEditable(false);
  71. // double click: stop timer and edit
  72. time.addMouseListener(new MouseAdapter() {
  73. public void mouseClicked(MouseEvent e) {
  74. if (e.getClickCount() == 2) {
  75. stopTimer();
  76. time.setEditable(true);
  77. time.getCaret().setVisible(true);
  78. }
  79. }
  80. });
  81. // focus lost: save
  82. time.addFocusListener(new FocusAdapter() {
  83. public void focusLost(FocusEvent e) {
  84. saveTime();
  85. parentFrame.pack();
  86. }
  87. });
  88. // enter: save
  89. time.addKeyListener(new KeyAdapter() {
  90. public void keyPressed(KeyEvent e) {
  91. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  92. saveTime();
  93. parentFrame.pack();
  94. }
  95. }
  96. });
  97.  
  98. // buttons ------------------------------------------
  99. resetButton = new JButton("Reset");
  100. resetButton.addActionListener(new ActionListener() {
  101. public void actionPerformed(ActionEvent e) {
  102. stopTimer();
  103. startTime = lapTime = setTime;
  104. time.setText(millisToStr(setTime));
  105. if (currentClip != null && currentClip.isRunning()) {
  106. currentClip.stop();
  107. currentClip.close();
  108. currentClip = null;
  109. }
  110. }
  111. });
  112. startStopButton = new JButton("Start / Stop");
  113. startStopButton.addActionListener(this);
  114.  
  115. // widget layout ------------------------------------------
  116. setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
  117. add(name);
  118. add(time);
  119. add(resetButton);
  120. add(startStopButton);
  121. }
  122.  
  123. // convert string from 00:00:00.000 to milliseconds
  124. public static long strToMillis(String time) {
  125. long result = 0;
  126. try {
  127. Pattern p = Pattern
  128. .compile("([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})(\\.([0-9]{1,3}))?");
  129. Matcher m = p.matcher(time.trim());
  130. m.find();
  131. result += Long.parseLong(m.group(1)) * 3600000
  132. + Long.parseLong(m.group(2)) * 60000 + Long.parseLong(m.group(3))
  133. * 1000;
  134. if (m.group(5) != null)
  135. result += Long.parseLong(m.group(5));
  136. } catch (Exception e) {
  137. // don't complain, just return 0
  138. return 0;
  139. }
  140. return result;
  141. }
  142.  
  143. // convert milliseconds to 00:00:00.000
  144. public static String millisToStr(long time) {
  145. long ms = time;
  146. long h = ms / 3600000;
  147. ms = ms % 3600000;
  148. long m = ms / 60000;
  149. ms = ms % 60000;
  150. long s = ms / 1000;
  151. ms = ms % 1000;
  152. return String.format("%02d:%02d:%02d.%03d", h, m, s, ms);
  153. }
  154.  
  155. // start stop button action
  156. public void actionPerformed(ActionEvent e) {
  157. if (time.isEditable())
  158. return;
  159. if (running) {
  160. running = false;
  161. try {
  162. timerThread.join();
  163. } catch (InterruptedException ie) {
  164. }
  165. lapTime = timeLeft;
  166. time.setText(millisToStr(timeLeft));
  167. } else {
  168. startTime = System.currentTimeMillis();
  169. running = true;
  170. timerThread = new Thread(this);
  171. timerThread.start();
  172. }
  173. }
  174.  
  175. // stop the timer and clean up the thread.
  176. public void stopTimer() {
  177. running = false;
  178. try {
  179. if (timerThread != null && timerThread.isAlive()) {
  180. timerThread.join();
  181. }
  182. } catch (Exception ie) {
  183. }
  184. }
  185.  
  186. // play a given sound file.
  187. private void playSound(String sound) {
  188. try {
  189. BufferedInputStream soundFileStream = new BufferedInputStream(this
  190. .getClass().getResourceAsStream(sound));
  191. AudioInputStream audioInputStream = AudioSystem
  192. .getAudioInputStream(soundFileStream);
  193. AudioFormat audioFormat = audioInputStream.getFormat();
  194. DataLine.Info dataLineInfo = new DataLine.Info(Clip.class, audioFormat);
  195. if (currentClip != null && currentClip.isRunning()) {
  196. currentClip.stop();
  197. currentClip.close();
  198. currentClip = null;
  199. }
  200. currentClip = (Clip) AudioSystem.getLine(dataLineInfo);
  201. currentClip.open(audioInputStream);
  202. currentClip.start();
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. }
  206. }
  207.  
  208. @Override
  209. public void run() {
  210. try {
  211. while (running) {
  212. timeLeft = lapTime - (System.currentTimeMillis() - startTime);
  213. if (timeLeft <= 0) {
  214. playSound(SOUND_PIKACHU);
  215. timeLeft = 0;
  216. lapTime = 0;
  217. running = false;
  218. }
  219. SwingUtilities.invokeAndWait(displayUpdater);
  220. Thread.sleep(50);
  221. }
  222. } catch (Exception e) {
  223. }
  224. }
  225. }
Add Comment
Please, Sign In to add comment