001/* 002 * Stopwatch.java 003 */ 004package minesweeper; 005 006//import javax.swing.*; 007//import java.awt.*; 008//import java.awt.event.*; 009 010import org.apache.logging.log4j.*; 011 012/** 013 * DESCRIBE {@link Stopwatch} HERE. 014 * 015 * @author 2017-2018 APCS 016 * @author ADD @author TAG FOR EVERYONE WHO CONTRIBUTED TO THIS FILE 017 * @author David C. Petty // https://github.com/wps-dpetty 018 */ 019public class Stopwatch {// Just a note, Stopwatch is traditionally started after the first click 020 /** 021 * log4j {@link Logger}. 022 */ 023 private static Logger logger = LogManager.getLogger(Minesweeper.SHORT); 024 025 private boolean isRunning; 026 private long startTime; 027 028 public Stopwatch() { 029 logger.info(this); 030 isRunning = false; 031 } 032 033 public void startStopWatch() { 034 isRunning = true; 035 startTime = System.currentTimeMillis(); 036 } 037 038 public void stopStopWatch() { 039 isRunning = false; 040 startTime = System.currentTimeMillis(); 041 } 042 043 public double getElapsedTime() { 044 long result = 0; 045 if (isRunning) { 046 result = System.currentTimeMillis() - startTime; 047 } 048 049 return result / 1000.0; 050 } 051 052 public boolean isStopWatchRunning() { 053 return isRunning; 054 } 055 056 public String getFormattedElapsedTime() { 057 String result = "00:00"; 058 if (isRunning) { 059 double elapsedTime = System.currentTimeMillis() - startTime; 060 int seconds = (int) (elapsedTime / 1000.0); 061 int minutes = seconds / 60; 062 seconds -= minutes * 60; 063 //result = minutes + ":" + seconds; 064 result = String.format("%02d:%02d", minutes, seconds); 065 } 066 return result; 067 } 068}