// Processed by NMI's Java Code Viewer 4.8.2 © 1997-2000 B. Lemaire // Website: http://njcv.htmlplanet.com E-mail: info@njcv.htmlplanet.com // Copy registered to Evaluation Copy // Source File Name: TurnTimer.java import java.awt.*; class TurnTimer extends Canvas { private Timer timer; private int time; private String minutes; private String seconds; private Image offImage; private Graphics offGraphics; private Font font; private Dimension d; private Component target; public int index; public int behavior; private boolean blinker; static int TIMER; static int WINNER = 1; public TurnTimer(Component component) { minutes = new String("00"); seconds = new String("00"); font = new Font("TimesRoman", 1, 16); behavior = TIMER; blinker = true; target = component; resize(50, 25); time = ClientData.timeLimit; setBackground(Color.black); timer = new Timer(this, 1000, true, 1001); d = size(); } public void start() { timer.start(); } public void stop() { timer.stop(); } public void restart() { timer = new Timer(this, 1000, true, 1001); time = ClientData.timeLimit; timer.start(); if(offGraphics != null && behavior == TIMER) { createTimeStrings(); offGraphics.clearRect(2, 2, d.width - 4, d.height - 4); offGraphics.drawString(minutes + ":" + seconds, 6, 17); repaint(); } } private void createTimeStrings() { if(time / 60 < 10) minutes = "0" + String.valueOf(time / 60); else minutes = String.valueOf(time / 60); if(time % 60 < 10) { seconds = "0" + String.valueOf(time % 60); return; } else { seconds = String.valueOf(time % 60); return; } } public void changeBehavior(int i) { behavior = i; if(i == TIMER) { resize(50, 25); offGraphics = null; repaint(); return; } if(i == WINNER) { resize(80, 25); offGraphics = null; repaint(); timer = new Timer(this, 500, true, 1001); timer.start(); } } public void paint(Graphics g) { update(g); } public void update(Graphics g) { if(offGraphics == null) { d = size(); offImage = createImage(d.width, d.height); offGraphics = offImage.getGraphics(); offGraphics.setFont(font); offGraphics.setColor(Color.white); offGraphics.draw3DRect(0, 0, d.width - 2, d.height - 2, true); createTimeStrings(); offGraphics.setColor(Color.red); if(behavior == TIMER) offGraphics.drawString(minutes + ":" + seconds, 6, 17); else if(behavior == WINNER) { offGraphics.setColor(Color.green); offGraphics.drawString("WINNER", 6, 17); } g.drawImage(offImage, 0, 0, this); return; } if(behavior == TIMER) { createTimeStrings(); offGraphics.clearRect(2, 2, d.width - 4, d.height - 4); offGraphics.drawString(minutes + ":" + seconds, 6, 17); } else if(behavior == WINNER) { offGraphics.setColor(Color.green); if(blinker) offGraphics.drawString("WINNER", 6, 17); else offGraphics.clearRect(2, 2, d.width - 4, d.height - 4); } g.drawImage(offImage, 0, 0, this); } public boolean handleEvent(Event event) { if(event.target == timer && event.id == 1001) { if(behavior == TIMER) { time--; repaint(); if(time == 0) { timer.stop(); target.handleEvent(new Event(this, 1001, null)); } } else if(behavior == WINNER) { blinker = !blinker; repaint(); } return true; } else { return false; } } }