呼叫之间的摆动延迟

时间:2015-11-22 21:30:26

标签: java swing timer delay

我正在尝试创建一个加载4个图像的程序,将其放入4个标签中,并更改框架中每个标签的图标,关于列表中的随机数,就像是闪烁图像一样。它需要按照在comecaJogo()中排序的顺序闪烁每个图像,但是当我在actionPerformed中按下btnComecar时,所有图像似乎同时发生变化:

这是我的逻辑课:

public class Logica {

    List<Integer> seqAlea = new ArrayList<Integer>();
    List<Integer> seqInsere = new ArrayList<Integer>();
    int placar = 0;
    boolean cabouGame = false;
    Random geraNumero = new Random();
    int numero;
    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();
    }
}

在框架中:

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() {

    lblImg1.setIcon(img1b);
    lblImg1.setBounds(78, 48, 250, 200);
    add(lblImg1);

    btnImg1.setBounds(153, 259, 89, 23);
    btnImg1.addActionListener(this);
    add(btnImg1);

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

    btnImg2.setBounds(456, 272, 186, 23);
    btnImg2.addActionListener(this);
    lblImg2.setIcon(img2b);
    lblImg2.setBounds(421, 61, 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) {
    // TODO Auto-generated method stub
    if (e.getSource().equals(btnImg1)) {
        jogo.piscaImagen(img1, img1b, lblImg1);
    } 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);
                break;
            case 2:
                System.out.println("Posição: " + i + " " + jogo.seqAlea.get(i));
                jogo.piscaImagen(img2, img2b, lblImg2);
                break;
            case 3:
                System.out.println("Posição: " + i + " " + jogo.seqAlea.get(i));
                jogo.piscaImagen(img3, img3b, lblImg3);
                break;
            case 4:
                System.out.println("Posição: " + i + " " + jogo.seqAlea.get(i));
                jogo.piscaImagen(img4, img4b, lblImg4);
                break;
            }
        }
    }
}
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

  

一次,就像记忆游戏一样:)

这可能有多种方式,例如......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main extends JFrame implements ActionListener {

    JButton btnImg1 = new JButton();
    JButton btnImg2 = new JButton();
    JButton btnImg3 = new JButton();
    JButton btnImg4 = new JButton();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new Main();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public Main() {

        JPanel buttons = new JPanel(new GridLayout(2, 2)) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
        };

        buttons.add(btnImg1);
        buttons.add(btnImg2);
        buttons.add(btnImg3);
        buttons.add(btnImg4);

        add(buttons);

        JButton play = new JButton("Play");
        add(play, BorderLayout.SOUTH);
        play.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        List<JButton> sequence = new ArrayList<>(Arrays.asList(new JButton[]{btnImg1, btnImg2, btnImg3, btnImg4}));
        Collections.shuffle(sequence);
        Timer timer = new Timer(1000, new ActionListener() {

            private JButton last;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (last != null) {
                    last.setBackground(null);
                }
                if (!sequence.isEmpty()) {
                    JButton btn = sequence.remove(0);
                    btn.setBackground(Color.RED);
                    last = btn;
                } else {
                    ((Timer)e.getSource()).stop();
                }
            }
        });
        timer.setInitialDelay(0);
        timer.start();
    }
}

这只是将所有按钮放入List,对列表进行随机播放,然后Timer删除List中的第一个按钮,直到所有按钮都“闪烁”。< / p>

现在这只是使用按钮backgroundColor,因此您需要创建一个类,允许您将JButton与“开”和“关”图像相关联,这些将是添加到ListTimer以与上面类似的方式执行