Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Matching Pairs game - something's not right?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Matching Pairs game - something's not right?

    Hi, this is a matching pairs game i'm making. basically if the user manages to find a matching pair they score a hit if not they score a miss. What i'm finding though, is that when i click on the cards (JToggleButtons), they wont change, only once i press the 'solve' button do they change on the click of a mouse. I can't figure it out for the life of me? Please could someone show me where im going wrong.. thanks!

     
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ItemListener;
    import java.awt.*;
    import java.awt.event.ItemEvent;
    import java.awt.event.*;
     
     
     
     
    public class MatchingPairs {
     
     
    	// Create images for toggle buttons
    	private ImageIcon backImg = new ImageIcon("back.jpg");
        private ImageIcon heartImg = new ImageIcon("heart.jpg");
        private ImageIcon diamondImg = new ImageIcon("diamond.jpg");
        private ImageIcon spadeImg = new ImageIcon("spade.jpg");
        private ImageIcon clubImg = new ImageIcon("club.jpg");
     
    	//Create JToggleButtons
        private JToggleButton heart = new JToggleButton(backImg);
        private JToggleButton heartDup = new JToggleButton(backImg);
        private JToggleButton diamond = new JToggleButton(backImg);
        private JToggleButton diamondDup = new JToggleButton(backImg);
        private JToggleButton spade = new JToggleButton(backImg);
        private JToggleButton spadeDup = new JToggleButton(backImg);
        private JToggleButton club = new JToggleButton(backImg);
        private JToggleButton clubDup = new JToggleButton(backImg); 
     
        //Create JButtons
        private JButton scramble = new JButton("Scramble");
        private JButton solve = new JButton("Solve");
     
        //Create JTextFields
        private JTextField hitValue = new JTextField(3);
        private JTextField missValue = new JTextField(3);
     
        //Create JLabels
        private JLabel hitLabel = new JLabel("Hits");
        private JLabel missLabel = new JLabel("Miss");
     
        // Variables
        private int hit;
        private int miss;
     
        //Window for GUI
        private JFrame window = new JFrame("Matching Pairs");
        private JPanel cardTable = new JPanel();
     
     
        // MatchingPairs() - constructor
        public MatchingPairs(){
        	// configure GUI
          	window.add(cardTable);
        	window.setSize(1400, 800);
        	window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	window.setBackground(Color.GREEN);
        	window.add(cardTable);
        	cardTable.setBackground(Color.GREEN);
     
     
     
           	hitValue.setEditable(false);
            hitValue.setFont(new Font("Arial",Font.BOLD,30));
            hitValue.setText(String.valueOf(hit));
     
     
        	missValue.setEditable(false);
            missValue.setFont(new Font("Arial",Font.BOLD,30));
            missValue.setText(String.valueOf(miss));
     
        	// add event listeners
            solve.addActionListener(new SolveButton());
            scramble.addActionListener(new ScrambleButton());
            heart.addItemListener(new Cards());
            heartDup.addItemListener(new Cards());
            diamond.addItemListener(new Cards());
            diamondDup.addItemListener(new Cards());
            spade.addItemListener(new Cards());
            spadeDup.addItemListener(new Cards());
            club.addItemListener(new Cards());
            clubDup.addItemListener(new Cards()); 
     
     
     
     
            cardTable.setLayout(new GridLayout(3, 6, 10, 10));
            cardTable.add(hitValue);
            hitValue.setPreferredSize(new Dimension(180, 300));
            cardTable.add(missValue);
            cardTable.add(scramble);
            cardTable.add(solve);
            cardTable.add(heart);
            cardTable.add(heartDup);
            cardTable.add(diamond);
            cardTable.add(diamondDup);
            cardTable.add(spade);
            cardTable.add(spadeDup);
            cardTable.add(club);
            cardTable.add(clubDup);
     
     
            //display GUI
            window.setVisible(true);
     
     
        }  
     
     
     
         // handle solve button press
        class SolveButton implements ActionListener{
        	public SolveButton(){
           	}
        	    public void actionPerformed(ActionEvent e){     // Switch ImageIcon to card faces
        	    	heart.setSelectedIcon(heartImg);
        	    	heartDup.setSelectedIcon(heartImg);
        	    	diamond.setSelectedIcon(diamondImg);
        	    	diamondDup.setSelectedIcon(diamondImg);
        	    	spade.setSelectedIcon(spadeImg);
        	    	spadeDup.setSelectedIcon(spadeImg);
        	    	club.setSelectedIcon(clubImg);
        	    	clubDup.setSelectedIcon(clubImg);
        	    }
        }
     
        // handle scramble button press (Does nothing at the moment)
        class ScrambleButton implements ActionListener {
        	public ScrambleButton(){
           	}
        }
     
     
        class Cards implements ItemListener{     // Flip card on click
            public Cards(){
           	}
                public void itemStateChange(ItemEvent e){
                	if (e.getStateChange() == ItemEvent.SELECTED){
                		heart.setSelectedIcon(heartImg);
                		heartDup.setSelectedIcon(heartImg);
                		diamond.setSelectedIcon(diamondImg);
            	    	diamondDup.setSelectedIcon(diamondImg);
            	    	spade.setSelectedIcon(spadeImg);
            	    	spadeDup.setSelectedIcon(spadeImg);
            	    	club.setSelectedIcon(clubImg);
            	    	clubDup.setSelectedIcon(clubImg);            		
                	}
               }
        }
     
        public static void main(String[] args) {
    		MatchingPairs gui = new MatchingPairs();
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Matching Pairs game - something's not right?

    Your code does not compile.

Similar Threads

  1. Need Help with Game
    By Shivam24 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 18th, 2011, 03:58 PM
  2. Help me with My Game.
    By jtvd78 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 13th, 2011, 04:14 PM
  3. [SOLVED] Pattern Matching to string problme
    By Kakashi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 16th, 2011, 09:45 AM
  4. Comparing two files and printing out matching words
    By sport10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2010, 09:10 PM
  5. What is the matching mutator method?
    By ssmith in forum Object Oriented Programming
    Replies: 1
    Last Post: November 3rd, 2009, 10:03 PM