Estou desenvolvendo o jogo da memoria, estou com um problema para aparecer a segunda imagem.
Eu fiz o seguinte quando o cara clica no botão ele revela a imagem b.(setIcon(…gif)) eu conto o número de cliques, quando o número de clickes for igual a 2 ele pega e compara o nome do arquivo da imagem dos dois botões (temp1 e temp2), e faz a comparação.
Se for iguais beleza, mas se for diferente ele tem que mostrar as imagens e logo depois tem que apagar os icones do botão b.setIcon(null)
O problema e que quando clico no segundo botão ele não revela a imagem, ele ja compara e ja faz o b.setIcon(null).
Eu ja tentei colocar um Thread.sleep para antes de fazer o setIcon(null) ele mostrar a figura, mas não funciona.
Queria saber como faço para aparecer a figura no segundo click e só depois ele fazer o b.setIcon(null)
Agradeço se puderem me ajudar!! Valeu!!
Logo abaixo o código
[color=red]
package MEMORIASAM;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Botões extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
ImageIcon im1 = new ImageIcon(getClass().getResource("img1.gif"));
ImageIcon im2 = new ImageIcon(getClass().getResource("img2.gif"));
ImageIcon im3 = new ImageIcon(getClass().getResource("img3.gif"));
JButton boton1 = new JButton("");
JButton boton2 = new JButton("");
static int numClick;
static String temp1 = "", temp2 = "";
public Botões() {
getContentPane().add(boton1, BorderLayout.CENTER);
getContentPane().add(boton2, BorderLayout.NORTH);
setSize(200, 150);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boton1.setBounds(40, 15, 60, 60);
boton2.setBounds(110, 15, 60, 60);
boton1.addActionListener(this);
boton2.addActionListener(this);
add(boton1);
add(boton2);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boton1) {
boton1.setIcon(im1); // IMAGENS
temp1 =
boton1.getIcon().toString().substring(
boton1.getIcon().toString().lastIndexOf("/"),
boton1.getIcon().toString().length()).replaceAll("/", "");
numClick++;
}
if (e.getSource() == boton2) {
boton2.setIcon(im2); // IMAGENS
temp2 = boton2.getIcon().toString().substring(
boton2.getIcon().toString().lastIndexOf("/"),
boton2.getIcon().toString().length()).replaceAll("/", "");
numClick++;
}
System.out.println(numClick);
if (numClick == 2)
confereImagens();
}
public void temp() throws InterruptedException {
Thread.sleep(1000);
}
public void confereImagens() {
System.out.println("Entrou no método verifica imagens!");
System.out.println("IMAGEM 1: " + temp1 + "\nIMAGEM 2: " + temp2);
if (temp1.equals(temp2)) {
System.out.println("Imagens são iguais!");
boton1.setIcon(im1);
boton2.setIcon(im2);
}
else {
System.out.println("Imagens são diferentes!");
boton1.setIcon(im1);
boton2.setIcon(im2);
try {
temp();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boton1.setIcon(null);
boton2.setIcon(null);
}
numClick = 0;
}
////////////////////// MAIN ///////////////////////////////
public static void main(String args[]) {
new Botões();
}
}
[/color]