Hello Guys , I need Help
Hello Java Programmers ,
i need help in my application ...
i'm doing a memory game application and it's almost done , but i just
need help in how to return the buttons closed again if the firstclick
does not equal the second click....
and how to say that you won after finishing the game ...
thank you very much for reading and i beg for help , thank u
here's my code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class Memory extends JFrame implements ActionListener
{
ImageIcon img[ ]=new ImageIcon[12];
public Memory()
{
img[0]=new ImageIcon("sahara.jpg");
img[1]=new ImageIcon("elvispresley.jpg");
img[2]=new ImageIcon("adamo.jpg");
img[3]=new ImageIcon("paulanka2.jpg");
img[4]=new ImageIcon("mike brant.jpg");
img[5]=new ImageIcon("lara2cov.jpg");
img[6]=new ImageIcon("italian job.jpg");
img[7]=new ImageIcon("ildivo.jpg");
img[8]=new ImageIcon("guitar.jpg");
img[9]=new ImageIcon("joedolan.jpg");
img[10]=new ImageIcon("damasco.jpg");
img[11]=new ImageIcon("chia.jpg");
JButton button[]=new JButton[24];
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(4,2,5,5));
for(int i=0;i<button.length;i++){
button= new JButton(Integer.toString(i+1));
p1.add(button);
Container c=getContentPane();
c.add(p1);
}
setTitle("Memory Game");
setSize(700,700);
setVisible(true);
button[0].addActionListener(this);
button[11].addActionListener(this);
button[1].addActionListener(this);
button[7].addActionListener(this);
button[2].addActionListener(this);
button[5].addActionListener(this);
button[3].addActionListener(this);
button[6].addActionListener(this);
button[8].addActionListener(this);
button[10].addActionListener(this);
button[13].addActionListener(this);
button[19].addActionListener(this);
button[15].addActionListener(this);
button[21].addActionListener(this);
button[18].addActionListener(this);
button[20].addActionListener(this);
button[4].addActionListener(this);
button[9].addActionListener(this);
button[12].addActionListener(this);
button[14].addActionListener(this);
button[16].addActionListener(this);
button[22].addActionListener(this);
button[17].addActionListener(this);
button[23].addActionListener(this);
button[0].setActionCommand("0");
button[11].setActionCommand("0");
button[1].setActionCommand("1");
button[7].setActionCommand("1");
button[2].setActionCommand("2");
button[5].setActionCommand("2");
button[3].setActionCommand("3");
button[6].setActionCommand("3");
button[8].setActionCommand("4");
button[10].setActionCommand("4");
button[13].setActionCommand("5");
button[19].setActionCommand("5");
button[15].setActionCommand("6");
button[21].setActionCommand("6");
button[18].setActionCommand("7");
button[20].setActionCommand("7");
button[4].setActionCommand("8");
button[9].setActionCommand("8");
button[12].setActionCommand("9");
button[14].setActionCommand("9");
button[16].setActionCommand("10");
button[22].setActionCommand("10");
button[17].setActionCommand("11");
button[23].setActionCommand("11");
}
int counter = 0;
String firstClick;
String secondClick;
public void actionPerformed (ActionEvent e){
int x=Integer.parseInt(e.getActionCommand());
JButton o=(JButton)e.getSource();
o.setIcon(img[x]);
if(counter==0){
firstClick=e.getActionCommand();
counter ++;
}
if(counter==1){
secondClick=e.getActionCommand();
}
if(secondClick.equals(firstClick)){
}
else{
}
}
public static void main(String[ ]args){
new Memory();
}
}>

