flipping memory cards if they are not matched...
i have made a memory card game and so far, I have done, clicking the JButton and showing the picture up. But my problem is to check the images if they are the same or not. If they are the same, I know what to do,. But if not, I don't know how to hide the image or flip the Jbutton so it would hide the image. I hope you can help me in this. Here is my code..,
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static java.util.Collections.*;
import java.util.Random;
public class GuessMe2 extends JFrame implements ActionListener {
JPanel wholePanel, firstPanel, secondPanel;
JButton newButton, quitButton, scoreButton, levelButton, learnButton;
ImageIcon[] pictureIcon = new ImageIcon[12];
JButton[] pictureButton = new JButton[12];
int[] btnID = new int[2];
int[] btnValue = new int[2];
int[] picNo = new int[2];
ArrayList<Integer> imageList = new ArrayList<Integer>();
int counter = 0;
//mga declarations
public GuessMe2(){
Container c = getContentPane();
panel ();
firstPanelButtons ();
secondPanelButtons ();
c.add(wholePanel); //dapat i-add sa container yung panel para makita
}
public void panel(){
wholePanel = new JPanel();
firstPanel = new JPanel();
secondPanel = new JPanel();
wholePanel.setLayout(new BorderLayout ());
firstPanel.setLayout(new FlowLayout());
secondPanel.setLayout(new GridLayout(4,3));
wholePanel.add(firstPanel, BorderLayout.NORTH); //para sa taas
wholePanel.add(secondPanel, BorderLayout.CENTER); //center para i-fill up
}
public void firstPanelButtons (){
newButton = new JButton ("New Game");
newButton.addActionListener(this);
scoreButton = new JButton ("Score");
scoreButton.addActionListener(this);
levelButton = new JButton ("Level");
levelButton.addActionListener(this);
learnButton = new JButton("Instructions");
learnButton.addActionListener(this);
quitButton = new JButton ("Quit Game");
quitButton.addActionListener(this);
firstPanel.add(newButton);
firstPanel.add(learnButton);
firstPanel.add(levelButton);
firstPanel.add(scoreButton);
firstPanel.add(quitButton);
}
public void secondPanelButtons (){
initPictureIcons();
for(int i=0; i<pictureButton.length; i++){
pictureButton[i]=new JButton();
pictureButton[i].addActionListener(this);
secondPanel.add(pictureButton[i]);
}
//for(int i=0; i<pictureButton.length; i++){
// secondPanel.add(pictureButton[i]);
//}
}
public void initPictureIcons (){
pictureIcon[0]=new ImageIcon("images/U1.jpg");
pictureIcon[1]=new ImageIcon("images/U2.jpg");
pictureIcon[2]=new ImageIcon("images/U3.jpg");
pictureIcon[3]=new ImageIcon("images/U4.jpg");
pictureIcon[4]=new ImageIcon("images/U5.jpg");
pictureIcon[5]=new ImageIcon("images/U6.jpg");
pictureIcon[6]=new ImageIcon("images/U1.jpg");
pictureIcon[7]=new ImageIcon("images/U2.jpg");
pictureIcon[8]=new ImageIcon("images/U3.jpg");
pictureIcon[9]=new ImageIcon("images/U4.jpg");
pictureIcon[10]=new ImageIcon("images/U5.jpg");
pictureIcon[11]=new ImageIcon("images/U6.jpg");
}
public void setArrayListImage(){
for(int i=0; i<2; i++){
for(int ii=1; ii<(pictureButton.length/2)+1; ii++){
imageList.add(ii);
}
}
}
//i used this to check the images but disabling the button is not applicable to boolean so I really don't know what to do.
public boolean sameImage(){
if (btnValue[0]==btnValue[1]){
return true;
}
return false;
}
public void actionPerformed(ActionEvent e){
for (int i=0; i<pictureButton.length; i++){
if(pictureButton[i]==e.getSource()){
this.pictureButton[i].setIcon(pictureIcon[i]);
counter++;
if (counter==3){
if(picNo[1]==picNo[2]){
}else{
pictureButton[picNo[1]].setDisabledIcon(true);
pictureButton[picNo[2]].setDisabledIcon(true);
}
}
}
}
}
public static void main(String args[]){
GuessMe2 application = new GuessMe2();
application.setTitle("Guess Me");
application.setSize(800,700);
application.setVisible(true);
application.setResizable(true); //para di nababago size
}
}
Thank you