package Views; /* * SWView.java * This file implements a countdown timer view of a stopwatch2 * Created on February 9, 2002 * * @author Stuart Hansen * @version */ import javax.swing.*; import java.awt.*; import java.awt.event.*; // The TimerDisplay has a count down timer that flashes when it // reaches 0 public class TimerDisplay extends SWView { long time; // the duration of the timer JTextField display; // where the timer is being displayed // The constructor public TimerDisplay (long seconds) { super("Count Down Timer"); // time is in tenths of seconds time = 10*seconds; display = new JTextField("00:00"); display.setEditable(false); setDisplayTime(0); display.setBounds(110, 25, 45, 30); getContentPane().add(display); setSize(300, 100); show(); } // setDisplayTime has to translate the parameter to a string // and place it in the textField protected void setDisplayTime (long timeArg) { long displayTime = time - timeArg; // If there is still time, display how much is remaining if (displayTime >= 0) { getContentPane().setBackground(Color.lightGray); String minutes = Long.toString((displayTime/600)%60); if ((displayTime/600)%60 < 10) minutes = "0"+ minutes; String seconds = Long.toString((displayTime/10%60)); if ((displayTime/10%60) < 10) seconds = "0" + seconds; display.setText( minutes + ":" + seconds); } // If the time has expired, flash Green and Gold if (displayTime <= 0) { if ((displayTime % 20) == 0){ getContentPane().setBackground(new Color(0, 125, 0)); } else if ((displayTime % 10) == 0) { getContentPane().setBackground(Color.yellow); } } } }