Trouble Understanding Timers

I have only been programming in java for about three months so this is going to be a fairly easy question to answer!

I am trying to create a section of code that runs every 10 seconds. For example a simple animation that accurs while everything else is going on. I thought it would be as simple as importing the Timer class and then creating a object of that timer class, setting the interval and being done with it. However I can’t seem to get it to work right. Any examples of reponses will be welcome.

this is how i have it in my game…

 

	public Timer timer;
	
	public static int newtimer =10000; // 10000 milliseconds
	
	
	timer = new Timer(newtimer);
	
	if (timer.action(elapsedTime)) {
	
			//do something
	)


 

or you could try a thread i use that too



import java.io.*;
import java.util.Date;

public class hThread extends Thread{

      public static int hmins =2;
      public static int hsecs =59;

       public void run(){

	Date d1 = new Date();
			
	for(;hmins >0;){
				
		for (;hsecs>0;){
			
		      try{	
		     			
			Thread.sleep(1000);
		}
				
		catch (InterruptedException e) {

			e.printStackTrace();
		}				
		System.out.println(hmins+":"+hsecs);
		hsecs--;

		if(hsecs ==0){
							
		hsecs =59;
		hmins --;	
		}
					
		if(hmins ==-1){
								
			Date d2 = new Date();
			long diff = (long)d2.getTime() - d1.getTime();
			DateDiff.secs = (int)diff/1000;
			DateDiff.mins = DateDiff.secs/60;	
			System.out.println("difference in seconds : " + DateDiff.secs);	
			System.out.println("difference in minutes : " + DateDiff.mins);
			return;
		}
	}
					
}

		
	}
}




import java.util.Date;
import java.util.GregorianCalendar;
import java.text.DateFormat;

public class DateDiff {

public static  int secs;
public static  int mins;

  public static void main(String[] args) {
    
 		try{
		
			new hThread().start();
			
		}

		catch (Exception e) {
			
			System.out.println("error starting hthread");
		}
	
  }
}


import javax.swing.Timer;

public class newTimer
public Timer timer;
public static int newTimer=1000;

 public void testingmethod()
 {
        timer=new Timer(newTimer);

        if (timer.action(elapsedTime))
        {
                    //do something
         }

  }//end testingmethod

}//end class

What is wrong with the above code?

your missing a bracket from your class for a start and i should have mentioned i use gtge so the timer(elapsedtime) wont work i dont think.
Its easier using a thread…some brain box will show you an easier way im sure.
Ive just gone and look at the api for timer and now my brain hurts …its too complicated

I don’t kno whats wrong with your code but I always use the swing timer like this (see below) and it works a lot for me.

Timer we;//your timer
int interval = 10000;//10 seconds
we = new Timer(interval, new ActionListener(){
public void actionPerformed(ActionEvent e){
//do something here
}
});

//here we tell it to start
we.start();
//this optional, if x is true, then the timer will repeat infinitively
//every y seconds, where y is the delay (or interval)
//otherwise it’ll wait y seconds and do its job once and stop
we.setRepeats(boolean x);

Maybe you’ll have some problems accesing variables inside the inner actionlistener, the compiller will say that you need to declare the variable final) But you can make your own class then add any variable you may need as global and make them point to anyother variable that you may need to use inside the actionlistener

Something else that you should consider is that if your going to put some animation lets say… inside your game loop then you have to tell it that when your timer starts running don’t start it again anymore otherwise it’ll make it start over and over again causing undesired behavor, you can use a boolean for that purpose or access the timer method isRunning().


double lastWhatever;
//blablabla

if(System.currentTimeMillis()-lastWhatever = 1000*(however long you want(seconds)))
{
//whatever you want to do
lastWhatever = System.currentTimeMillis();
}

I know this is crude but it has worked for me so far. Of Course I am counting on a main loop so this gets called every once and a while.

same here.

anyways its a pitty that it has to be checked like 100 times per second. if i want to make things to happen every 10 minutes (600 seconds), it is checked 60000 times wasting time for nothing…

:frowning:

thats why i like the thread approach totally independant of game loop

 
try{	
     Thread.sleep(1000);
}
				
catch (InterruptedException e) {

      e.printStackTrace();
}