Delay of the swing timer.

Asked

Viewed 181 times

4

I’m making a little game that flashes some images in order on the frame, my goal is that they blink in order, but they’re all flashing together:

public class Logica {

    List<Integer> seqAlea = new ArrayList<Integer>();
    List<Integer> seqInsere = new ArrayList<Integer>();
    Random geraNumero = new Random();
    Timer timer = new Timer(1500, null);

    public void comecaJogo() {
        for (int i = 0; i < 4; i++) {
            numero = geraNumero.nextInt(4) + 1;
            seqAlea.add(numero);
        }
    }

    public void piscaImagen(ImageIcon img1, ImageIcon img1b, JLabel lbl) {
        timer.addActionListener(new ActionListener() {
            int count = 0;
            @Override               
            public void actionPerformed(ActionEvent evt) {
                if(lbl.getIcon() != img1){
                    lbl.setIcon(img1);
                } else {
                    lbl.setIcon(img1b);
                }
                count++;
                if(count == 2){
                    ((Timer)evt.getSource()).stop();
                }
            }
        });
        timer.setInitialDelay(1250);
        timer.start();
    }

On the frame is:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class Main extends JFrame implements ActionListener {

JLabel lblImg1 = new JLabel();
JButton btnImg1 = new JButton("Economize Energia");
final URL resource1 = getClass().getResource("/br/unip/IMGs/img1.jpg");
final URL resource1b = getClass().getResource("/br/unip/IMGs/img1_b.png");
ImageIcon img1 = new ImageIcon(resource1);
ImageIcon img1b = new ImageIcon(resource1b);

JButton btnImg2 = new JButton("Preserve o Meio Ambiente");
JLabel lblImg2 = new JLabel("");
final URL resource2 = getClass().getResource("/br/unip/IMGs/img2.jpg");
final URL resource2b = getClass().getResource("/br/unip/IMGs/img2_b.jpg");
ImageIcon img2 = new ImageIcon(resource2);
ImageIcon img2b = new ImageIcon(resource2b);

JButton btnImg3 = new JButton("N\u00E3o \u00E0 polui\u00E7\u00E3o!");
JLabel lblImg3 = new JLabel("");
final URL resource3 = getClass().getResource("/br/unip/IMGs/img3.jpg");
final URL resource3b = getClass().getResource("/br/unip/IMGs/img3_b.jpg");
ImageIcon img3 = new ImageIcon(resource3);
ImageIcon img3b = new ImageIcon(resource3b);

JButton btnImg4 = new JButton("Recicle!");
JLabel lblImg4 = new JLabel("");
final URL resource4 = getClass().getResource("/br/unip/IMGs/img4.jpg");
final URL resource4b = getClass().getResource("/br/unip/IMGs/img4_b.jpg");
ImageIcon img4 = new ImageIcon(resource4);
ImageIcon img4b = new ImageIcon(resource4b);

Logica jogo = new Logica();

JButton btnComecar = new JButton("Come\u00E7ar");

public static void main(String[] args) {
    Main window = new Main();
    window.setVisible(true);

}

public Main() {

    setBounds(100, 100, 800, 600);
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    lblImg1.setIcon(img1b);
    lblImg1.setBounds(78, 48, 250, 200);
    btnImg1.setBounds(153, 259, 89, 23);
    btnImg1.addActionListener(this);
    add(lblImg1);       
    add(btnImg1);

    btnImg2.setBounds(456, 259, 186, 23);
    btnImg2.addActionListener(this);
    lblImg2.setIcon(img2b);
    lblImg2.setBounds(421, 48, 250, 200);
    add(btnImg2);
    add(lblImg2);

    btnImg3.setBounds(114, 525, 186, 23);
    btnImg3.addActionListener(this);
    lblImg3.setIcon(img3b);
    lblImg3.setBounds(78, 314, 250, 200);
    add(lblImg3);
    add(btnImg3);

    btnImg4.setBounds(456, 525, 186, 23);
    btnImg4.addActionListener(this);
    lblImg4.setIcon(img4b);
    lblImg4.setBounds(421, 314, 250, 200);
    add(lblImg4);
    add(btnImg4);

    btnComecar.setBounds(68, 14, 89, 23);
    btnComecar.addActionListener(this);
    add(btnComecar);

}

public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(btnImg1)) {
        jogo.piscaImagen(img1, img1b, lblImg1, 500);
    }  else if (e.getSource().equals(btnComecar)) {
        jogo.comecaJogo();
        System.out.println(jogo.seqAlea);
        for (int i = 0; i < jogo.seqAlea.size(); i++) {
            switch (jogo.seqAlea.get(i)) {
            case 1:
                System.out.println("Posição: " + i + " " + jogo.seqAlea.get(i));
                jogo.piscaImagen(img1, img1b, lblImg1, 500);
                break;
            case 2:
                System.out.println("Posição: " + i + " " + jogo.seqAlea.get(i));
                jogo.piscaImagen(img2, img2b, lblImg2, 1000);
                break;
            case 3:
                System.out.println("Posição: " + i + " " + jogo.seqAlea.get(i));
                jogo.piscaImagen(img3, img3b, lblImg3, 1500);
                break;
            case 4:
                System.out.println("Posição: " + i + " " + jogo.seqAlea.get(i));
                jogo.piscaImagen(img4, img4b, lblImg4, 2000);
                break;
            }
        }
    }
}

}

How can I make the images blink in order?

1 answer

1

One of the ways to solve the problem is to assign a delay different initial for each animation, e.g:

public void piscaImagen(ImageIcon img1, ImageIcon img1b, JLabel lbl, int initialDelay) {
    Timer timer = new Timer(1500, null);
    timer.addActionListener(new ActionListener() {
        // Mesmo código da pergunta
    });
    timer.setInitialDelay(initialDelay);
    timer.start();
}

And on the switch:

for (int i = 0; i < jogo.seqAlea.size(); i++) {
    System.out.println(jogo.seqAlea.get(i));
    switch (jogo.seqAlea.get(i)) {
    case 1:
        jogo.piscaImagen(img1, img1b, lblImg1, 0);
        break;
    case 2:
        jogo.piscaImagen(img2, img2b, lblImg2, 500);
        break;
    case 3:
        jogo.piscaImagen(img3, img3b, lblImg3, 1000);
        break;
    case 4:
        jogo.piscaImagen(img4, img4b, lblImg4, 1500);
        break;
    }
}

This way each image will start flashing half a second after the previous one (you can increase the delay as needed).

  • I did changing the delay on each switch and this way and did not work, I will edit with the full code

  • Hi Vitor. See if you create separate Timers within the method as this version of the code works as expected (don’t forget to play with the ranges as well. You may want longer intervals).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.