Results 1 to 5 of 5
  1. #1
    nubshat is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Comparing ImageIcons

    I am trying to make a matching game in java where the user flips over two images the program either disables those buttons or if the user gets it wrong the program resets the buttons.
    I have most of the program working I am just having issues with comparing the images.

    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 javax.swing.ImageIcon.*;
    
    public class MatchingGame extends JFrame implements ActionListener {
        private JButton exitButton, replayButton;
        private JButton[] gameButton = new JButton[36];
        private ImageIcon[] ImageArray;
        private ImageIcon[] images2;
        private int counter = 0;
        private int[] buttonID = new int[2];
        private ImageIcon[] buttonValue = new ImageIcon[2];
    	
        public MatchingGame() {
        	setLayout(new BorderLayout());
        	init();
            panel();
            setImages();
            setTitle("MathingGame");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(900, 900);
            setVisible(true);
        }
        
        public void init() {
            for (int i = 0; i < gameButton.length; i++) {
                gameButton[i] = new JButton();
                gameButton[i].addActionListener(this);
            }
            exitButton = new JButton("Exit");
            exitButton.addActionListener(this);
            replayButton = new JButton("Replay");
            replayButton.addActionListener(this);
        }
        
        public void panel() {
        	
        	Panel gamePanel = new Panel();
        	gamePanel.setLayout(new GridLayout(6, 6));
        	
        	for (int i = 0; i < gameButton.length; i++) {
            	gamePanel.add(gameButton[i]);
        	}
    
        	Panel buttonPanel = new Panel();
        	buttonPanel.add(replayButton);
        	buttonPanel.add(exitButton);
        	buttonPanel.setLayout(new GridLayout(1, 0));
     
        	add(gamePanel, BorderLayout.CENTER);
        	add(buttonPanel, BorderLayout.SOUTH);
     
        }
        
        public void setImages(){
    
        ImageIcon Aquaman = new ImageIcon ("Aquaman.jpg");
        ImageIcon Batman = new ImageIcon ("Batman.jpg");
        ImageIcon CaptainAmerica = new ImageIcon ("CaptainAmerica.jpg");
        ImageIcon Deadpool = new ImageIcon ("Deadpool.jpg");
        ImageIcon FantasticFour = new ImageIcon ("FantasticFour.jpg");
        ImageIcon Flash = new ImageIcon ("Flash.jpg");
        ImageIcon GreenArrow = new ImageIcon ("GreenArrow.jpg");
        ImageIcon GreenLantern = new ImageIcon ("GreenLantern.jpg");
        ImageIcon Hawkeye = new ImageIcon ("Hawkeye.jpg");
        ImageIcon Ironman = new ImageIcon ("Ironman.jpg");
        ImageIcon Punisher = new ImageIcon ("Punisher.jpg");
        ImageIcon RedTornado = new ImageIcon ("RedTornado.jpg");
        ImageIcon Robin = new ImageIcon ("Robin.jpg");
        ImageIcon Spiderman = new ImageIcon ("Spiderman.jpg");
        ImageIcon Superman = new ImageIcon ("Superman.jpg");
        ImageIcon TheAvengers = new ImageIcon ("TheAvengers.jpg");
        ImageIcon Thor = new ImageIcon ("Thor.jpg");
        ImageIcon Wonderwoman = new ImageIcon ("Wonderwoman.jpg");
        
        ImageIcon[] images = new ImageIcon[36];
     
        images[0] = Aquaman;
        images[1] = Batman;
        images[2] = CaptainAmerica;
        images[3] = Deadpool;
        images[4] = FantasticFour;
        images[5] = Flash;
        images[6] = GreenArrow;
        images[7] = GreenLantern;
        images[8] = Hawkeye;
        images[9] = Ironman;
        images[10] = Punisher;
        images[11] = RedTornado;
        images[12] = Robin;
        images[13] = Spiderman;
        images[14] = Superman;
        images[15] = TheAvengers;
        images[16] = Thor;
        images[17] = Wonderwoman;
        images[18] = Aquaman;
        images[19] = Batman;
        images[20] = CaptainAmerica;
        images[21] = Deadpool;
        images[22] = FantasticFour;
        images[23] = Flash;
        images[24] = GreenArrow;
        images[25] = GreenLantern;
        images[26] = Hawkeye;
        images[27] = Ironman;
        images[28] = Punisher;
        images[29] = RedTornado;
        images[30] = Robin;
        images[31] = Spiderman;
        images[32] = Superman;
        images[33] = TheAvengers;
        images[34] = Thor;
        images[35] = Wonderwoman;
         
        java.util.List myList = Arrays.asList (images);
        Collections.shuffle(myList);
        images2 = new ImageIcon [myList.size()];
        myList.toArray(images2);
        }
    
    
        public boolean sameValues() {
            if (buttonValue[0] == buttonValue[1]) {
                return true;
            }
            return false;
        }
        
        public void actionPerformed(ActionEvent e) {
        	for (int i = 0; i < gameButton.length; i++) {
                if (gameButton[i] == e.getSource()) {
                	gameButton[i].setIcon(images2[i]);
                    gameButton[i].setEnabled(false);
                    counter++;
                    if (counter == 3) {
                        if (sameValues()) {
                            gameButton[buttonID[0]].setEnabled(false);
                            gameButton[buttonID[1]].setEnabled(false);
                        } else {
                            gameButton[buttonID[0]].setEnabled(true);
                            gameButton[buttonID[0]].setIcon(null);
                            gameButton[buttonID[1]].setEnabled(true);
                            gameButton[buttonID[1]].setIcon(null);
                        }
                        counter = 1;
                    }
                    
                    if (counter == 1) {
                        buttonID[0] = i;
                        buttonValue[1]= images2[i];
                    }
                    
                    if (counter == 2) {
                        buttonID[1] = i;
                        buttonValue[1]= images2[i];
                    }
                }
        	}
    		if (exitButton == e.getSource()) {
    		    System.exit(0);
    		}
    		if (replayButton == e.getSource()) {
    			setImages();
    		}
        }
        
        public static void main (String[] args){
        	new MatchingGame();
        }
        
    }
    What I have right now is not working. I have also tried:
    Java Code:
        
    public boolean sameValues() {
        	if (buttonValue[0].toString().equals(buttonValue[1].toString())){
        		return true
            }
            return false;
        }
    Please help me.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Comparing ImageIcons

    How about comparing the references to the icons shown by the two buttons? JButton has a method that returns a reference to the icon it is showing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    nubshat is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: Comparing ImageIcons

    Quote Originally Posted by Norm View Post
    How about comparing the references to the icons shown by the two buttons? JButton has a method that returns a reference to the icon it is showing.
    How would I do that?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Comparing ImageIcons

    Get the references to the icons for each button and use the == operator to see if they are the same.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    nubshat is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: Comparing ImageIcons

    Quote Originally Posted by Norm View Post
    Get the references to the icons for each button and use the == operator to see if they are the same.
    I got it working thanks alot

Similar Threads

  1. Replies: 3
    Last Post: 01-31-2012, 12:01 AM
  2. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 01:40 AM
  3. Replies: 1
    Last Post: 03-09-2009, 03:18 AM
  4. Problems with ImageIcons
    By The_L in forum AWT / Swing
    Replies: 2
    Last Post: 10-24-2008, 02:04 AM
  5. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 09:40 AM

Posting Permissions

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