-1

I'm trying to understand how to use one some kind of basic animation. I can use threads, but I was told by multiple people not to multi-thread in java. I think I cold do something like:

Timer t = new Timer(10, somthing);
t.start();
x++;
t.end();

That's my basic understanding of it. Can someone link a tutorial or explain how to make a time do something every 10 seconds or longer?

Zong
  • 6,160
  • 5
  • 32
  • 46

3 Answers3

0

First, why were you told not to multithread? That seems like a pretty limiting rule for modern computers.

It seems as if you wanna schedule a task to fire at a given rate. You should look into Timer#schedule(). It allows you to pass in a task, as well as some configuration for timing that task.

public class TimerDemo {
     public static void main(String[] args) {
          // creating timer task, timer
          TimerTask tasknew = new TimerSchedulePeriod();
          Timer timer = new Timer();

          // scheduling the task at interval
          timer.schedule(tasknew, 0, 10);      
     }

    // this method performs the task
    public void run() {
         System.out.println("timer working");      
    }    
}

Source of code: http://www.tutorialspoint.com/java/util/timer_schedule_period.htm

Vince
  • 14,470
  • 7
  • 39
  • 84
0

If you want to use a swing timer, you can make one like this:

    Timer timer = new Timer();
//delayed timer
    timer.schedule(new TimerTask() {

    @Override
    public void run() {
      //do your code after a delay (delayInMillis)
    }
    }, delayInMillis);

//repeating timer

timer.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
      //do your code every repeatTimeInMillis
    }
    }, 0, repeatTimeInMillis;

My suggestion for animations is to create an integer in which you change with the timers. When you are drawing your animation, you can draw a specific image if the int is a specific variable. (Using if-else statemtents or a switch statement)

I hope I helped :D

loafy
  • 105
  • 1
  • 11
0

"Not to multithread in Java" doesn't sound right. The advice was maybe geared towards Swing and to run your app/do your painting on the Event Dispatch Thread. In which case you would want to do the animation with a java.swing.Timer. The basic construct is

Timer( int deleyInMillis, ActionListener listener )

where the delayInMillis is the milliseconds delayed between ActionEvents fired by the timer. The ActionListener will be the listener listing for the ActionEvents. So every delayInMillis the actionPerformed method will be called.

So you could do something like

Timer timer = new Timer(10000, new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        if (someStoppingCondition) {
            ((Timer)e.getSource()).stop();  
        } else {
            // do something every ten seconds.
        }
    }
});
timer.start();

You can see more at How to Use Swing Timers. You can also see a bunch of examples here and here and here and here and here and here.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720