Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to count down seconds??

Status
Not open for further replies.

sjohri214

Programmer
Jul 19, 2002
24
0
0
GB
Hi,

The question I have is, if I have declared an int value (the value 20 for example), how can i make a counter to count down from 20 seconds down to zero?

Any Help would be much appreciated

Thanks

 
what do u mean counter

if u want to delay 20 seconds do
Thread.sleep(20 * 1000);

-pete
 
Hi Pete,

Thanks for the response.
Although I would like to produce a counter in order that I can display to the user how long it will take for the task to be completed.
 
here is a sample to get you started, hope this helps....

Code:
import java.awt.*;
import java.awt.event.*;

public class Counter extends java.applet.Applet implements ActionListener, Runnable {
   TextField field;
   int bits    = 0;
   int seconds = 20;
   int minutes = 0;
   boolean interrupt;
   Thread stopwatch = null;
   String str = "";
public void init() {
      field = new TextField(32);
      field.setEditable(false);
      setFont(new Font("Arial", 0, 14));
      Button start = new Button("Start");
      Button stop = new Button("Stop");
      start.addActionListener(this);
      stop.addActionListener(this);
      add(field);
      add(start);
      add(stop);
}
   public void paint(Graphics g)	{
      g.setColor(Color.blue);
      g.setFont(new Font("Arial", 0, 18));
      g.fillRect(5, 3, 540, 54);
      g.setColor(Color.yellow);
      g.drawString("Timer : ", 15, 25);
      g.drawString(str, 15, 50);
	}

public void actionPerformed(ActionEvent e) {
   String comm = e.getActionCommand();
      if(comm.equals("Start")) {
         interrupt = true;
         start();
      }
      if(comm.equals("Stop")) {
         interrupt = false;
         if (seconds > 0) str = "Question completed: " + field.getText();
         else str = "Sorry out of time.";
      }
   repaint();
}
   public void start() {
      if(stopwatch == null) {
         stopwatch = new Thread(this, "StopWatchApplet");
         stopwatch.start();
      }
   }
   public void run() {
      Thread thread = Thread.currentThread();
         while (stopwatch == thread)
         {
            try
            {
               while(seconds != 0 && interrupt)
               {
                  Thread.sleep(10);
                  bits++;
                     if(bits % 100 == 0 && bits != 0)
                     {
                        bits -= 100;
                        seconds--;
                     }

               		if(!interrupt) --bits;
               		field.setText(+minutes+" mins : "+seconds+" secs: "+bits+" hundreds of a second.");


				}

		   }
		   catch(Exception ignore) { }
        }

   }
   public void update(Graphics g) { paint(g); }
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top