É o seguinte…
eu estou tentando fazer um tipo um campo minado e pra isso eu criei um vetor de tamanho 9 que contem os 9 botoes que eu preciso.
quando o usuario clica no botao( que tem um ImageIcon nele, uma imagem de ‘agua’ ;D), o botao muda sua imagem ou para um ImageIcon com uma carinha feliz
ou para uma bomba. se o cara clicar tres vezes em bomba perde, se clicar 6 vezes em carinha feliz ganha. no basico eh isso.
[code]Random random = new Random();
JButton[] vetor = new JButton[9];
vetor[0] = new JButton(agua);
vetor[1] = new JButton(agua);
vetor[2] = new JButton(agua);
vetor[3] = new JButton(agua);
vetor[4] = new JButton(agua);
vetor[5] = new JButton(agua);
vetor[6] = new JButton(agua);
vetor[7] = new JButton(agua);
vetor[8] = new JButton(agua);
int x=0;
for (int i=0; i<3; i++){
/* escolhe tres numeros aleatoriamente e nessas posiçoes do vetor ele adiciona um ActionListener de bomba */
while(true){
x = random.nextInt(9);
if (x != vetor2[0])
if (x != vetor2[1])
if (x != vetor2[2]) break;
}
/* o 'true' representa bomba */
vetor2[i] = x;
vetor[x].addActionListener(new Action(true,vetor[x]));
frame.pack();
}
/* nas outras posiçoes ele adiciona um ActionListener de carinha Feliz ;D */
for (int i=(vetor.length - 3); i > 0; i--){
System.out.println(vetor.length);
if (i != vetor2[0])
if (i != vetor2[1])
if (i != vetor2[2])
vetor[i].addActionListener(new Action(false,vetor[x]));
}[/code]
e nao tá dando um bug doido aqui.
a classe do ActionListener eh essa:
[code]private class Action implements ActionListener{
private boolean bomb;
private JButton botao;
public Action(boolean bomb, JButton o){
this.bomb = bomb;
this.botao = o;
}
public void actionPerformed(ActionEvent arg0) {
if (bomb){
botao.setIcon(pequena);
flag++;
/*flag controla se o cara acertou os 3 botoes com bombas ;D se sim ele perdeu :/*/
if(flag == 3){
JFrame frame_aux = new JFrame("Game Over :/");
String s = "\n GAME OVER :/ ";
Font font = new Font("Verdana", Font.PLAIN, 20);
JTextArea info = new JTextArea(100,100);
info.setFont(font);
info.setText(s);
info.setForeground(Color.RED);
info.setEditable(false);
JPanel panel = new JPanel();
JLabel label = new JLabel(bomba);
panel.setLayout(new GridLayout(1,2,0,0));
panel.add(label);
panel.add(info);
panel.setVisible(true);
frame_aux.add(panel);
frame_aux.setSize(715,100);
frame_aux.setResizable(false);
frame_aux.setVisible(true);
frame_aux.setDefaultCloseOperation(frame_aux.EXIT_ON_CLOSE);
}
}
else{
botao.setIcon(sorria);
flag2++;
/*flag2 controla se o cara acertou os 6 botoes sem bombas ;D se sim ele ganhou ;D*/
if(flag2 == 6){
JFrame frame_aux = new JFrame("Game Over :D");
String s = "\n GANHOOOU :D ";
Font font = new Font("Verdana", Font.PLAIN, 20);
JTextArea info = new JTextArea(100,100);
info.setFont(font);
info.setText(s);
info.setForeground(Color.ORANGE);
info.setEditable(false);
JPanel panel = new JPanel();
JLabel label = new JLabel(bomba);
panel.setLayout(new GridLayout(1,2,0,0));
panel.add(label);
panel.add(info);
panel.setVisible(true);
frame_aux.add(panel);
frame_aux.setSize(715,100);
frame_aux.setResizable(false);
frame_aux.setVisible(true);
frame_aux.setDefaultCloseOperation(frame_aux.EXIT_ON_CLOSE);
}
}
}
}[/code]