I am in the process of creating a jackpot game. At the moment I am stuck trying to cycle through the different pictures and stop on a random picture for 5 different slots.

So far I have this for animating:

    private void animatePictures()
    {
        Thread timer = new Thread()
        {
        public void run()
        {
            while(keepGoing){
            for (int i = 1; i < 4; i++)
            {
                images = new ImageIcon("image" + i + ".png");

                firstPictureLabel.setIcon(images);
                secondPictureLabel.setIcon(images);
                thirdPictureLabel.setIcon(images);
                fourthPictureLabel.setIcon(images);
                fifthPictureLabel.setIcon(images);
                try
                {
                    sleep(100);
                }
                catch(InterruptedException e)
                {

                }

            }
        }
        }

        };
    timer.start();
    }

How could I stop a picture in a slot and keep the others going then stop the next and keep the next ones moving etc

The above code just keeps the pictures moving in all the slots.

Thank You

Recommended Answers

All 3 Replies

You shouldn't be updating your GUI from your own thread, you should always do it from the Swing thread. Rather than have a loop, you should use javax.swing.Timer to schedule time-based activities for Swing. I would suggest 5 Timers, one per label so you can start or stop the individual Timers whenever you want.
ps: images = new ImageIcon("image" + i + ".png"); means you will read an image file every time you update the GUI (10 times per second!), which will be very slow and inefficient. Read all your image files into an array of ImageIcons when your program starts, then just display the icons from that array.

If I wanted to use a handler for the swing timer
how would i stop each one

so far I have this:

Timer t1 = new Timer(500, this);
Timer t2 = new Timer(1000, this);
ImageIcon[] picture = new ImageIcon[3];
//label1
//label2 etc...

public void actionPerformed(ActionEvent e)
{
    //How would I use if statement

    for(int i = 0; i < picture.length(); i++)
    {
        label1.setIcon(picture[i]);
    }

}

how would i stop each one

The Timer class has a methods you could call.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.