Results 1 to 9 of 9
  1. #1
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    I tried this another code but on the third click, I guess it didn't compare the pictures. Well, I go try and try. But I would really appreciate if you can give me some advice about this. Thank you.


    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);
    }
    }
    }

    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 (sameImage()){
    }else {
    pictureButton[btnID[0]].setDisabledIcon(pictureIcon[i]);
    pictureButton[btnID[1]].setDisabledIcon(pictureIcon[i]);

    }

    }

    }
    }
    }
    public static void main(String args[]){
    GuessMe2 application = new GuessMe2();
    // Toolkit kit = Toolkit.getDefaultToolkit();
    // Image img = kit.getImage("any picture na maliit para icon sa taas");
    // application.setIconImage(img);
    application.setTitle("Guess Me");
    application.setSize(800,700);
    application.setVisible(true);
    application.setResizable(true); //para di nababago size

    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    I am sorry.. I always forget... I really am sorry.

  6. #6
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Hi,


    Java 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];
    	savepictureIcon[];
    
    	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);
    			}
    		}
    	}
    
    	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==1){
    					savepictureIcon[]=pictureButton[pictureIcon[i]];
    				
    				}
    					
    			}
    		}
    	}
    	public static void main(String args[]){
    		GuessMe2 application = new GuessMe2();
    	//	Toolkit kit = Toolkit.getDefaultToolkit();
    	//	Image img = kit.getImage("any picture na maliit para icon sa taas");
    	//	application.setIconImage(img); 
    		application.setTitle("Guess Me");
    		application.setSize(800,700);
    		application.setVisible(true);		
    		application.setResizable(true); //para di nababago size
    
    	}
    }

  7. #7
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Is this ok now?
    i get some hint by playing with the code. I have a question. On the part of the code:

    Java Code:
    for (int i=0; i<pictureButton.length; i++){
    			if(pictureButton[i]==e.getSource()){
    				this.pictureButton[i].setIcon(pictureIcon[i]);
    this is what i use to show the image when the button is clicked. I got an advice so use the counter so I have this one:

    Java Code:
    for (int i=0; i<pictureButton.length; i++){
    			if(pictureButton[i]==e.getSource()){
    				this.pictureButton[i].setIcon(pictureIcon[i]);
    				counter++;
    
    				if (counter==1){
    					savepictureIcon[]=pictureButton[pictureIcon[i]];
    				
    				}
    					
    			}
    		}
    But I realized what do I need that will contain the image of the first button clicked. the variable (savepictureIcon). so, i stopped there, thinking of how can i declare that kind of variable. The thing is, I would like to save the image to "savepictureIcon" then compare it with the picture on the second one. If they match, setEnabled(true) and if not, setEnabled(false). I keep on trying but it didn't work out. I don't receive any error but the buttons didn't change even if they are not of the same image.

    I how you can help in this. Thank you.

  8. #8
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    I hope you can help me in this. Thank you.

  9. #9
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Another question, is it possible to store the number of actionEvents? thank you.

Similar Threads

  1. Deck of Cards
    By khunmato in forum New To Java
    Replies: 13
    Last Post: 09-06-2009, 05:47 PM
  2. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  3. Rotating and flipping an image in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:01 PM
  4. Display random cards
    By carl in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 07:01 AM
  5. Help with Random cards
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-03-2007, 08:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •