Estou fazendo um jogo de memória e estou com a seguinte dúvida.
Estou usando botões para colocar as imagens, meu problema está na hora de comparar se as imagens são iguais ou não!
Eu to fazendo o seguinte, o cara clica no primeiro botão, aparece a imagem e é armazenada numa variável o nome do arquivo de imagem, quando ele clica no outro botão ele faz a mesma coisa pega o nome do arquivo e compara com o primeiro botão, se for igual o nome dos arquivos beleza a figura botão fica lá senão as imagens saem!!
Meu problema ta ai!
Do jeito que eu fiz não deu certo!!
Se alguem tiver outra ideia para poder comparar as imagens eu agradeço.
CÓDIGO LOGO A BAIXO
[color=darkred]package MEMORIASAM;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Botões extends JPanel implements ActionListener {
ImageIcon im1 = new ImageIcon(getClass().getResource("img1.gif"));
ImageIcon im2 = new ImageIcon(getClass().getResource("img2.gif"));
ImageIcon im3 = new ImageIcon(getClass().getResource("img3.gif"));
//b1.setContentAreaFilled(false); //tira cor de fundo do botão
//b1.setBorderPainted(false); //tira as bordas
JButton boton1 = new JButton("");
JButton boton2 = new JButton("");
public Botões() {
setLayout(null);
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) {
String temp1 = "", temp2 = "";
if (e.getSource() == boton1) {
boton1.setIcon(im1); // IMAGENS
temp1 = boton1.getIcon().toString().substring(
boton1.getIcon().toString().lastIndexOf("/"),
boton1.getIcon().toString().length()).replaceAll("/", "");
}
if (e.getSource() == boton2) {
boton2.setIcon(im1); //IMAGENS
temp2 = boton2.getIcon().toString().substring(
boton2.getIcon().toString().lastIndexOf("/"),
boton2.getIcon().toString().length()).replaceAll("/", "");
}
confereImagens(temp1, temp2);
}
public void confereImagens(String img1, String img2) {
if (img1.equals(img2)) {
boton1.setIcon(im1);
boton2.setIcon(im1);
} else {
boton1.setIcon(null);
boton2.setIcon(null);
}
}
public static void main(String args[]) {
JFrame frame = new JFrame("MEMORIA");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
frame.getContentPane().add(new Botões(), BorderLayout.CENTER);
frame.setSize(200, 150);
frame.setVisible(true);
}
}[/color]