import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; public class Stopwatch { TargetsTimer timer; int sign = 1; int count = 0; long seconds = 0; /* * A stopwatch clock, used to measure the players' "thinking time" */ public Stopwatch() { timer = new TargetsTimer(100, new ActionListener() { public void actionPerformed(ActionEvent event) { if ((count%=10) == 0) { seconds+=sign; } ++count; } }); } /* * Starts the stopwatch */ public void start() { timer.start(); } /* * Stop the stopwatch * @return the seconds elapsed */ public long stop() { timer.stop(); return seconds; } /* * Return the seconds elapsed * @return the seconds elapsed */ public long getSeconds() { return seconds; } /* * Return a formatted string with the current elapsed time * @param sec the seconds to be formatted * @return a formatted string holding the time */ public String getTimeString(long sec) { String colonColor = "black"; DecimalFormat twoDigits = new DecimalFormat("00"); return "" + twoDigits.format(sec/3600) + ":" + twoDigits.format((sec%3600)/60) + ":" + twoDigits.format((sec%3600)%60); } /* * Reset the stopwatch */ public void reset() { if (timer.isRunning()) { stop(); } seconds = 0; } }