0

I want to set my timer to 0 and show the elapsed time. With this code it starts at 1:00 automatically. But I want to start at 0:00 automatically. Here's my code where it starts at 1:00. How can I set this code to a timer where it starts at 0:00?

class TimeTyping extends JPanel implements Runnable{

    private JLabel timelabel;
    private int sec=1,min=1,testtime=0;
    private String str;
    private boolean timefree;
    protected Thread t1 ;

    TimeTyping(int time){

        super();
        setLayout(new FlowLayout());
        setVisible(true);
        setSize(300,300);


        if(time<=5){
            str=""+time+":00";
            timelabel=new JLabel(str);
            timefree=false;
        }
        else{
            timelabel=new JLabel("Free Time Test");
            timefree=true;
        }
        add(timelabel);

        t1 = new Thread(this);
        /*t1.start();
        t1.suspend();*/
    }

    public void run(){
        try{

            if(timefree){
                testtime++;
                run();
                System.out.println("free");
            }
            else{
                //taking min and sec form label
                synchronized(this) {
                    if(sec==0){
                        sec=60;
                        min--;
                    }
                    sec++;
                    testtime++;
                    if(sec>9){
                        timelabel.setText(min+":"+sec);
                    }
                    else{
                        timelabel.setText(min+":0"+sec);
                    }
                }
                Thread.sleep(1000);
                //if test is not over 
                if(min>0 || sec>0){
                    run();  
                }
            }

        }
        catch(Exception e){}

    }

    //return total test time taken in sec
    public int getTestTime(){

        return testtime;

    }

    //return minutes of test time left 
    public int getMin(){
        return min;
    }


    //return sec of test time left
    public int getSec(){
        return sec;
    }

    /*public static void main(String args[]){

        TypingTime t= new TypingTime();

    }*/


}
DPM
  • 1,960
  • 3
  • 26
  • 49
  • I don't understand how this is meant to work at all. Are you trying to use Thread.sleep as the timer? Instead try recording the start-time and updating the gui at regular intervals with the elapsed time. – Adrian Mouat Nov 24 '11 at 12:19
  • Take a look at this draft of TimerLabel. https://gist.github.com/1406350 – alexvetter Nov 29 '11 at 20:35

2 Answers2

0

Why do you set min = 1 and sec = 1 in line 4? Looks to me like that's the reason why your timer starts at 01:00. Also: how do you call the constructor? If you call it with no argument TypingTime t= new TypingTime(); it will ignore all your setup.

Pete
  • 10,720
  • 25
  • 94
  • 139
0

You set min and sec = 1. That's your problem but if you change their values to 0 you will start at min = -1. Add a boolean (e.g. init) to your code. So you can check in the if(sec==0){ line if your program just started. Hope you understand what I mean. :-)

...
private boolean init = false;
...
if (sec == 0 && init) {
    sec = 60;
    min--;
} else if (!init) {
    init = true;
}
... 

Btw, I think you should use the javax.swing.Timer.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
alexvetter
  • 1,998
  • 2
  • 16
  • 42
  • This doesn't look like a very good approach. If what you say is true, your code just papers over the real problem. – Robert Harvey Nov 25 '11 at 21:42
  • His problem is that the timer starts at 1:00 and not at 0:00. With this code snippet it would start at 0:00. I don't like the way he's doing this but I don't want to change the whole code. – alexvetter Nov 26 '11 at 08:56