0

I have 6 objects in the class GUI which are moving through the code:

Anim anim = new Anim();
Timer timer = new Timer();
timer.scheduleAtFixedRate(anim, 30, 30);

When an object comes to a point I want to stop it. In class Anim I'm doing:

public class Anim extends TimerTask {
    @Override
    public void run() {
       if (t1.x == 300 && t1.y == 300) {
         try {
            Thread.sleep(3000);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }

It stops application, and I want have stayed at a concrete object. How to do it?

EDIT:

Ok, now it works well, does not interfere with other objects. But the object is moving on and after 1 second back to the starting position. I wish the whole time he was in the same position when a sleep.

if (Main.auto1.x == 700 && Main.auto1.y == 350) {
        timer = new javax.swing.Timer(1000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                Main.auto1.x = 700;
                Main.auto1.y = 350;
            }
        });
        timer.setRepeats(false);
        timer.start();

1 Answers1

3

Use Swing Timer instead of Thread.sleep() and Java Timer in a Swing application that sometime hangs the Swing applicaion.

Read more How to Use Swing Timers

Sample code:

private Timer timer;
...
timer = new javax.swing.Timer(3000, new ActionListener() {
    
    @Override
    public void actionPerformed(ActionEvent arg0) {

       //do what ever you want to do
       // call timer.stop() when the condition is matched
         
    }
});
timer.setRepeats(true);
timer.start();

EDIT

Please have a look at my another post How to fix animation lags in Java?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I changed the class Anim on: public class Anim extends TimerTask { public void run() { if (t1.x == 300 && t1.y == 300) {{ timer = new javax.swing.Timer(3000, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { timer.stop(); } }); ... ... timer.setRepeats(true); timer.start(); But not performed any pause. Where am I now set paused, for example 5 seconds? – user3756381 Jun 19 '14 at 13:16
  • It would be great if you can share a reproducible code. – Braj Jun 19 '14 at 15:45
  • Ok, now it works well, does not interfere with other objects. But the object is moving on and after 1 second back to the starting position. I wish the whole time he was in the same position when a sleep. if (Main.auto1.x == 700 && Main.auto1.y == 350) { timer = new javax.swing.Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { Main.auto1.x = 700; Main.auto1.y = 350; } }); timer.setRepeats(false); timer.start(); – user3756381 Jun 19 '14 at 19:42
  • Instead of comment why don't add it in your original post as an EDIT as I did. Is it readable in comment? – Braj Jun 19 '14 at 19:43
  • How to fix the car started to get the vehicle from the place where you were stopped and out of this where he were without timer? – user3756381 Jun 19 '14 at 20:06
  • you have stored the coordinate some where in the code. Just use it and don't reset it. – Braj Jun 19 '14 at 20:07