package awb; import java.awt.*; public class Timer extends Canvas implements Runnable { private int myHours = 0; private int myMinutes = 0; private int mySeconds = 0; private int myTenths = 0; private long myTicks = 0; private boolean myTimerOn; private final static Font FONT = new Font("Helvetica",Font.PLAIN,10); private Thread mySelf; public Timer() { setBackground(Color.black); myTimerOn = false; } public Dimension preferredSize() { return new Dimension(50,15); } public Dimension minimumSize() { return new Dimension(50,15); } public void startTimer() { myTimerOn = true; mySelf = new Thread(this); mySelf.start(); } public void stopTimer() { myTimerOn = false; } public void reset() { stopTimer(); myHours = 0; myMinutes = 0; mySeconds = 0; myTenths = 0; myTicks = 0; repaint(); } public String getElapsedTime() { return toString(); } public int getElapsedTicks() { return (int)myTicks; } public static String changeTicksToString(int ticks) { int t = ticks % 10; ticks /= 10; int s = ticks % 60; ticks /= 60; int m = ticks % 60; ticks /= 60; return makeString(ticks,m,s,t); } public void run() { while (myTimerOn) { incrementClock(); repaint(); try { Thread.sleep(90); } catch (InterruptedException ex) { } } mySelf = null; } public void paint(Graphics g) { g.setFont(FONT); g.setColor(Color.white); g.drawString(toString(),5,13); } public String toString() { return makeString(myHours,myMinutes,mySeconds,myTenths); } public static String makeString(int h, int m, int s, int t) { String time = h + ":"; if (m < 10) time = time + "0" + m + ":"; else time = time + m + ":"; if (s < 10) time = time + "0" + s + ":"; else time = time + s + ":"; time += t; return time; } public long getTicks() { return myTicks; } private void incrementClock() { myTicks++; myTenths++; if (myTenths > 9) { myTenths = 0; mySeconds++; if (mySeconds > 59) { mySeconds = 0; myMinutes++; if (myMinutes > 59) { myMinutes = 0; myHours++; } } } } }