title: Timer Flickers When It Reaches 0
body: <p>importing libraries</p>
  
  <pre><code>public class Countdown1 extends Applet implements Runnable {
  // getting user input
  
      String input = JOptionPane.showInputDialog("Enter seconds: ");
   // converting string to integer
  
      int counter = Integer.parseInt(input);
  
      Thread countdown;
  
      public void start() {
          countdown = new Thread(this);
          countdown.start();
  
      }
  
  // executed by thread
      public void run() {
  
          Timer timer;
  
          timer = new Timer(1000, new ActionListener() /* counting down time inputted */ {
              public void actionPerformed(ActionEvent evt) {
                  if (counter &gt; 0) {
  
                      counter--;
  // repainting each second
  
                      repaint();
                  }
              }
          });
  // timer started 
  
          timer.start();
  
      }
  
      public void paint(Graphics g) {
  //painting text and time 
  
          g.setFont(new Font("Times New Roman", Font.BOLD, 35));
          g.drawString("Seconds: " + String.valueOf(counter), 260, 210);
          setBackground(Color.orange);
          setForeground(Color.magenta);
  
  // change background to cyan when timer reaches 0
          if (counter == 0) {
              setBackground(Color.cyan);
          }
      }
  }
  </code></pre>