Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Need help with matching game

    Hey any chance of some help with this piece of code? I abstracted it from a larger program that I built to help me try with the smaller version and then implement the code into the larger version.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

    public class matchingGame extends JFrame implements ActionListener{



    //////////////creating image for buttons(before they are clicked)//////////////
    Icon buttonIcon = new ImageIcon("i_love_simpsons.jpg");

    int count = 0;
    int match = 0;
    int guess = 0;

    Object clicked1 = new Object();
    Object clicked2 = new Object();

    /////////////creating buttons with images/////////////
    JButton button1 = new JButton(buttonIcon);
    JButton button2 = new JButton(buttonIcon);
    JButton button3 = new JButton(buttonIcon);


    JLabel matches = new JLabel("Matches: "+ match);
    JLabel guesses = new JLabel("Guesses: "+ guess);



    /////////////creating button with "Reset" written on it/////////////////
    JButton reset = new JButton ("Reset");

    /////////////creating panels/////////////////
    JPanel buttonPanel = new JPanel();
    JPanel bottomPanel = new JPanel();


    //////////creating container and naming it contentPane///////////////
    Container contentPane = getContentPane();


    /////////////////mew method to display the game/////////////////////
    public matchingGame(){

    //////////////////name of window////////////
    super("*****Guessing Game*****");



    //////////////////adding action listenr to buttons////////////
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);


    reset.addActionListener(this);


    ////////////////setting layout of button panel to GridLayout////////////////
    buttonPanel.setLayout(new GridLayout(3,4));

    buttonPanel.add(button1);
    buttonPanel.add(button2);
    buttonPanel.add(button3);



    bottomPanel.add(matches);
    bottomPanel.add(guesses);
    bottomPanel.add(reset);


    contentPane.setLayout(new BorderLayout());


    contentPane.add(buttonPanel, BorderLayout.CENTER);
    contentPane.add(bottomPanel, BorderLayout.SOUTH);

    setSize(500, 500);

    setVisible(true);


    }

    public static void main(String[]args){

    matchingGame game = new matchingGame();

    }

    public void actionPerformed(ActionEvent e){


    count++;

    Icon bart = new ImageIcon("bart.jpg");
    Icon homer = new ImageIcon("homer.jpg");
    Icon marge = new ImageIcon("marge.jpg");




    if (e.getSource()==button1){
    button1.setIcon(bart);

    }
    else if (e.getSource()==button1){
    button1.setIcon(bart);
    }

    else if (e.getSource()==button2){
    button2.setIcon(homer);
    }

    else if (e.getSource()==button3){

    button3.setIcon(bart);
    }

    if (count==1){
    clicked1 = e.getSource();

    }
    else if (count ==2){
    clicked2 = e.getSource();
    }

    if (clicked1==clicked2)
    //it seems like it's not even entering this if statement//
    {
    match++; //this part doesn't work
    setEnabled(false); //this part doesn't work

    }



    if(e.getSource()== reset){

    button1.setIcon(buttonIcon);
    button2.setIcon(buttonIcon);
    button3.setIcon(buttonIcon);
    match=0;
    count=0;
    guess=0;

    }
    }
    }


    Ok, basically all that I have working is the JFrame diplaying JButtons with images and when the button is clicked another image appears instead....

    What the program is supposed to do is check that when the images are the same they stay facing upright, like in a matching game.... the original program has 12 buttons (6 different pairs of images).

    The main problem I'm having first is that it's doesn't seem to be comparing the images. Also the match++ isn't implementing.

    I've been pulling my hair out trying to sort this out.... any help will be nice.

  2. #2
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    Can you please put
    Java Code:
    code tags
    around your code, it will help a lot.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

  3. #3
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Hope this works :/

    Like this???

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class matchingGame extends JFrame implements ActionListener{
    
    
    
    //////////////creating image for buttons(before they are clicked)//////////////
    Icon buttonIcon = new ImageIcon("i_love_simpsons.jpg");
    
    int count = 0;
    int match = 0;
    int guess = 0;
    
    Object clicked1 = new Object();
    Object clicked2 = new Object();
    
    /////////////creating buttons with images/////////////
    JButton button1 = new JButton(buttonIcon);
    JButton button2 = new JButton(buttonIcon);
    JButton button3 = new JButton(buttonIcon);
    
    
    JLabel matches = new JLabel("Matches: "+ match);
    JLabel guesses = new JLabel("Guesses: "+ guess);
    
    
    
    /////////////creating button with "Reset" written on it/////////////////
    JButton reset = new JButton ("Reset");
    
    /////////////creating panels/////////////////
    JPanel buttonPanel = new JPanel();
    JPanel bottomPanel = new JPanel();
    
    
    //////////creating container and naming it contentPane///////////////
    Container contentPane = getContentPane();
    
    
    /////////////////mew method to display the game/////////////////////
    public matchingGame(){
    
    //////////////////name of window////////////
    super("*****Guessing Game*****");
    
    
    
    //////////////////adding action listenr to buttons////////////
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    
    
    reset.addActionListener(this);
    
    
    ////////////////setting layout of button panel to GridLayout////////////////
    buttonPanel.setLayout(new GridLayout(3,4));
    
    buttonPanel.add(button1);
    buttonPanel.add(button2);
    buttonPanel.add(button3);
    
    
    
    bottomPanel.add(matches);
    bottomPanel.add(guesses);
    bottomPanel.add(reset);
    
    
    contentPane.setLayout(new BorderLayout());
    
    
    contentPane.add(buttonPanel, BorderLayout.CENTER);
    contentPane.add(bottomPanel, BorderLayout.SOUTH);
    
    setSize(500, 500);
    
    setVisible(true);
    
    
    }
    
    public static void main(String[]args){
    
    matchingGame game = new matchingGame();
    
    }
    
    public void actionPerformed(ActionEvent e){
    
    
    count++;
    
    Icon bart = new ImageIcon("bart.jpg");
    Icon homer = new ImageIcon("homer.jpg");
    Icon marge = new ImageIcon("marge.jpg");
    
    
    
    
    if	 (e.getSource()==button1){
    button1.setIcon(bart);
    
    }
    else if (e.getSource()==button1){
    button1.setIcon(bart);
    }
    
    else if	(e.getSource()==button2){
    button2.setIcon(homer);
    }
    
    else if	(e.getSource()==button3){
    
    button3.setIcon(bart);
    }
    
    if (count==1){
    clicked1 = e.getSource();
    
    }
    else if (count ==2){
    clicked2 = e.getSource();
    }
    
    if (clicked1==clicked2)
    //it seems like it's not even entering this if statement//
    {
    match++; //this part doesn't work
    setEnabled(false); //this part doesn't work
    
    }
    
    
    
    if(e.getSource()== reset){
    
    button1.setIcon(buttonIcon);
    button2.setIcon(buttonIcon);
    button3.setIcon(buttonIcon);
    match=0;
    count=0;
    guess=0;
    
    }
    }
    }
    First time posting on a forum like this so didn't know what to do :P

Similar Threads

  1. Paranthesis Matching???
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 10-27-2009, 07:02 AM
  2. Picture Matching Game
    By schnappi in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2009, 02:38 PM
  3. matching and getting xml data
    By Juuno in forum Advanced Java
    Replies: 6
    Last Post: 04-26-2009, 07:25 PM
  4. Help with signature matching
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:21 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
  •