Olá pessoal ! Estou tentando fazer um jogo da memória o mais simples possível e gostaria que vocês me dessem dicas de como fazê-lo (em relação a pontuação do jogador, por exemplo…). Consegui, porém, com a ajuda do fórum (;D) fazer essa pequena parte abaixo. O problema é que quando aciono o setIcon em mais de um botão para mudar a imagem ao clicar ele dá erro:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Tabuleiro extends JFrame implements ActionListener{
private JButton[][] pecas;
JPanel painelTabuleiro;
ImageIcon imagem1 = new ImageIcon("C:/Users/LARISSA/JAVA WORKSPACE/Figuras Jogo/fig1.jpg");
ImageIcon imagem2 = new ImageIcon("G:/LARISSA/Linguagem de Programação II/Figuras Jogo/fig2.jpg");
ImageIcon imagem3 = new ImageIcon("G:/LARISSA/Linguagem de Programação II/Figuras Jogo/fig3.jpg");
ImageIcon imagem4 = new ImageIcon("G:/LARISSA/Linguagem de Programação II/Figuras Jogo/fig4.jpg");
Tabuleiro(){
super("Jogo da Memoria");
pecas= new JButton[2][2];
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1000,600);
painelTabuleiro = new JPanel(new GridLayout(2,2));
this.add(painelTabuleiro);
ImageIcon imagemPadrao = new ImageIcon("G:/LARISSA/Linguagem de Programação II/Figuras Jogo/logotipo2.jpg");
for(int i=0; i<pecas.length; i++){
for(int j=0;j<pecas.length; j++){
pecas[i][j] = new JButton(imagemPadrao);
painelTabuleiro.add(pecas[i][j]);
pecas[i][j].addActionListener(this);
}
}
this.setVisible(true);
pack();
}
public static void main(String[] args) {
Tabuleiro t1 = new Tabuleiro();
}
@Override
public void actionPerformed(ActionEvent arg) {
if(arg.getSource() == this.pecas[1][1]){
pecas[1][1].setIcon(imagem2);
}
//é aqui que está o problema, mais de um getSource ele dá erro :/
if(arg.getSource() == this.pecas[1][2]){
pecas[1][2].setIcon(imagem1);
}
}
E esse é o erro:
[color=red]Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 2
at Tabuleiro.actionPerformed(Tabuleiro.java:93)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)[/color]
Agradeço desde já !
