need to be able to wait inbetween image changes.

ok, so I have a slot machine game, and whats a slot machine game, if the reels dont spin before changing to the final images.

so what i want to do is:
draw image
wait 100 milliseconds
draw image
wait 100 milliseconds

and so on,

now i have tried


Slot1.setImage(image1);
invalidate(); // <-  the repaint method
Thread.sleep(100);
slot1.setImage(image2);
invalidate();
Thread.sleep(100);
slot2.setImage(image3);

but all it does is wait the 200 milliseconds then changes it to the final image, without showing the image1 or image2.

now i dont have a game loop, because, i dont actually need one with this game, as the screen only needs to be rendered when one button is clicked, not so many times a second.

so what i would like to know, is how can i get this to wait?

Maybe because you are setting the images to slot1 the first two times then setting the final image to slot2…

You need a timer of some sort. Usually this is done with a game loop but if you aren’t using one, you could use java.swing.Timer.

        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //...Perform a task...
            }
        };
        Timer timer = new Timer(1000, taskPerformer); //1 second
        timer.setRepeats(false);
        timer.start();

This should cover all the Swing threading stuff for you, so you don’t need to invokeLater on the EDT.

Just for fun, here is how you might achieve this in LibGDX:

float delay = 1.0f; //delay in seconds

Timer.schedule(new Task(){
    public void run() {
        // Do your work
    }
}, delay);

You could also use incredibly unprofessional loop stuff:


slot1.setImage(image1);
invalidate();
int duration = 200;
long time = System.currentTimeMillis();
boolean done = false;
while(System.currentTimeMillis() - time < duration) {
    if(System.currentTimeMillis() - time > 100 && !done) {
        slot2.setImage(image2);
        invalidate();
        done = true;
    }
}
slot2.setImage(image2);
invalidate();

Hey! :open_mouth: Don’t mock me and my “professional” coding skills! :frowning:

I put the “incredibly unprofessional” there because this was my first idea, and when I finished writing it the others came up with actually good ideas like davedes with the Timers… So it was more directed at me :stuck_out_tongue:

davedes’s answer is the only correct one in this thread. :slight_smile:

I use “incredibly unprofessional” loop stuff myself :stuck_out_tongue:

Problem I have with the swing timer, is that I don’t know if I can use the javax.swing.*; library for this game, as it is not going to be run on a computer, but a mobile device.

I tried to create a simple timer, that has a method getElapsed(); which tells me how many milliseconds have passed. The problem is I don’t know how to incorporate it to add a delay, which is why the “incredibly unprofessional” loop stuff might be my only way of doing it,

And the slot2.setImage() was a type, it should have been slot1, but it doesn’t matter, same concept.

You don’t have to use the javax.swing.* Timer. There’s also a java.util.Timer, have a look at this one, it should be available on mobile devices I guess. It’s fairly simple to use:

slot1.setImage(image1);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
	
	@Override
	public void run () {
		slot1.setImage(image2);
	}
}, 100);
timer.schedule(new TimerTask() {
	
	@Override
	public void run () {
		slot1.setImage(image3);
	}
}, 200);

Come on, my crappiness works as well :frowning:

Lol, I’ll try this when my computer boots up, thanks everyone, helps me out a lot!

The TimerTasks took a bit of modifying, but they worked a treat! Thanks :slight_smile: