Wednesday, 15 July 2015

java - How stop object in TimerTask? -



java - How stop object in TimerTask? -

i have 6 objects in class gui moving through code:

anim anim = new anim(); timer timer = new timer(); timer.scheduleatfixedrate(anim, 30, 30);

when object comes point want stop it. in class anim i'm doing:

public class anim extends timertask { @override public void run() { if (t1.x == 300 && t1.y == 300) { seek { thread.sleep(3000); } grab (interruptedexception e) { // todo auto-generated grab block e.printstacktrace(); }

it stops application, , want have stayed @ concrete object. how it?

edit:

ok, works well, not interfere other objects. object moving on , after 1 sec starting position. wish whole time in same position when 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();

use swing timer instead of thread.sleep() , java timer in swing application sometime hangs swing applicaion.

read more how utilize swing timers

sample code:

private timer timer; ... timer = new javax.swing.timer(3000, new actionlistener() { @override public void actionperformed(actionevent arg0) { //do ever want // phone call timer.stop() when status matched } }); timer.setrepeats(true); timer.start(); edit

please have @ post how prepare animation lags in java?

java swing timertask

No comments:

Post a Comment