/* a fancy java stopwatch */ import java.awt.*; import java.applet.*; public class stopwa extends Applet implements Runnable{ Thread moveThread; private static final int MOVEPAUSE = 10; public boolean stopWatch = false, myGo = false, myPause = false; public Image offScreenImage; public long t1, tpause; public String timeout = "0", timeout1 = "", timeout2 = "", timeout3 = ""; public Color colorBack = new Color(0, 80, 0); public int dt = 0, minutes=0, hours=0; public double time=0.0; public void start() { if(myGo) { if(moveThread == null){ moveThread = new Thread(this); moveThread.start(); } } } public void stop() { if(moveThread != null){ moveThread.stop(); try { moveThread.join(); } catch (InterruptedException e) { } } moveThread=null; } public void run() { while (true){ try { moveThread.sleep(MOVEPAUSE); }catch(InterruptedException e) { } repaint(); } } public void init() { super.init(); offScreenImage = createImage(180,130); //{{INIT_CONTROLS setLayout(null); resize(200,220); //}} repaint(); } public void update(Graphics g){ Graphics offg = offScreenImage.getGraphics(); paint(offg); g.drawImage(offScreenImage, 0, 0, this); } public void paint(Graphics g){ g.setColor(colorBack); g.fillRect(0,0,180,130); g.setColor(Color.white); g.drawRect(3,3,174,124); g.setColor(Color.black); g.fillRect(6,6,168,40); if (stopWatch) { dt = (int)(System.currentTimeMillis() - t1); time = dt/1000.; if (time >= 60) { time = time - 60.0; t1 = t1 + 60000; minutes++; } if (minutes == 60) { minutes = 0; hours++; } if (hours < 1) { if (minutes < 1) { timeout = ""+time; } else { if (time < 10) { timeout = ""+minutes+":0"+time; } else { timeout = ""+minutes+":"+time; } } } else { String outmin = ""+minutes; if (minutes < 10) outmin = "0"+minutes; String outsec = ""+time; if (time < 10) outsec = "0"+time; timeout = ""+hours+":"+outmin+":"+outsec; } } g.setColor(Color.red); g.setFont(new Font("Courier", Font.BOLD, 24)); g.drawString(timeout,15,37); g.setFont(new Font("Courier", Font.BOLD, 18)); if (!timeout1.equals("")) { // lap time 1 display g.setColor(Color.white); g.drawString("1>",15,70); g.setColor(Color.red); g.drawString(timeout1,45,70); } if (!timeout2.equals("")) { // lap time 2 display g.setColor(Color.white); g.drawString("2>",15,90); g.setColor(Color.red); g.drawString(timeout2,45,90); } if (!timeout3.equals("")) { // lap time 3 display g.setColor(Color.white); g.drawString("3>",15,110); g.setColor(Color.red); g.drawString(timeout3,45,110); } } public boolean handleEvent(Event evt){ if (evt.id == Event.KEY_PRESS) { String thePressedKey = ""+(char)evt.key; if (thePressedKey.equalsIgnoreCase("s")) { stopWatch = !stopWatch; if (stopWatch) { t1 = System.currentTimeMillis(); minutes = 0; hours = 0; timeout = "0"; timeout1 = ""; timeout2 = ""; timeout3 = ""; myGo = true; myPause = false; start(); } else { myGo= false; myPause = false; stop(); } repaint(); } if (thePressedKey.equalsIgnoreCase("p")) { myPause = !myPause; if (!myPause) { t1 = t1 + System.currentTimeMillis() - tpause; stopWatch = true; myGo = true; start(); } else { tpause = System.currentTimeMillis(); stopWatch = false; myGo= false; stop(); } repaint(); } if (thePressedKey.equalsIgnoreCase("c")) { minutes = 0; hours = 0; timeout = "0"; timeout1 = ""; timeout2 = ""; timeout3 = ""; myPause = false; myGo = false; stopWatch = false; stop(); repaint(); } if (thePressedKey.equals("1")) { timeout1 = timeout; repaint(); } if (thePressedKey.equals("2")) { timeout2 = timeout; repaint(); } if (thePressedKey.equals("3")) { timeout3 = timeout; repaint(); } } if (evt.target == this && evt.id == Event.MOUSE_DOWN) { return true; } return false; } //{{DECLARE_CONTROLS //}} }