Results 1 to 5 of 5
  1. #1
    delongj is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Help with obj1.equals(obj2)

    I have an issue I simply cannot figure out. I think another set of eyes will help. I am pulling a series of .png files into an array, then displaying two of the images into a Jframe. These two Jframes are then compared on a button click to see if the images are the same. I can't seem to get the code right for my action listener to compare ie. if(pic1.equals(pic2)). Any helo would be great! Thanks in advance!

    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    /*
    * @author jmdelong
    */
    public class samediff extends JFrame implements ActionListener
    {
    JLabel lblOutput = new JLabel();
    JPanel pnlControl = new JPanel();
    JButton btnSame = new JButton ("");
    JButton btnDiff = new JButton ("");
    JLabel pic1 = new JLabel("");
    JLabel pic2 = new JLabel("");
    ImageIcon[] imageArr = new ImageIcon[10];

    public static void main(String[] args)
    {
    new samediff();
    }

    public samediff()
    {
    try
    {
    for(int i=0; i<10; i++)
    {

    imageArr[i] = new ImageIcon("images/image" + i + ".png");
    }

    String SamePath = "images/same.png"; //image courtesy of iconlet.com
    File same = new File(SamePath);
    BufferedImage SameImage = ImageIO.read(same);

    String DiffPath = "images/diff.png"; //image courtesy of iconlet.com
    File diff = new File(DiffPath);
    BufferedImage DiffImage = ImageIO.read(diff);

    btnSame = new JButton(new ImageIcon(SameImage));
    btnDiff = new JButton(new ImageIcon(DiffImage));

    this.setTitle("Same Different Game");
    this.setLayout(new GridLayout(0,2));

    this.setSize(1000,900);
    this.setVisible(true);

    pic1 = new JLabel("", imageArr[0], JLabel.CENTER);
    pic2 = new JLabel("", imageArr[0], JLabel.CENTER);

    this.add(pic1);
    this.add(pic2);
    this.add(btnSame);
    this.add(btnDiff);

    btnSame.addActionListener(this);
    btnDiff.addActionListener(this);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    }
    catch(Exception f)
    {
    System.out.println(f);
    }
    }
    public void actionPerformed (ActionEvent e)
    {
    if(e.getSource() == btnSame)
    {
    System.out.println("I've been clicked");
    if(pic1.equals(pic2))
    {
    System.out.println("Correct!");
    }
    else
    System.out.println("Wrong");
    }
    if(e.getSource() == btnDiff)
    {
    System.out.println("Different button");
    }
    }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    JLabel doesn`t override the equals method of Object, so you compare only the references!
    BufferedImage doesn`t override equals too, so one solution would be, to iterate over the image by yourself and compare the pixels e.g. with the method getRGB(x, y) of the BufferedImages

  3. #3
    delongj is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    This may be a dumb question, but shouldn't I only need to compare the references? The file names will still be the same. Correct?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    But you have different objects !!!

    What exactly do you want to compare? The images(pixel?) The filenames? Then you could use the methods of the File class! getName() returns a string, that string you could compare with the equals method with the the other filename!
    as an example, something like:
    same.getName().equals(diff.getName())

    But i think that wouldn`t make sense in relation to your code?!

    Sorry i`m not sure what you really want :(

  5. #5
    delongj is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    eRaaaa, sorry, I was just looking back through what I sent you and I can see where it would be confusing. I updated a few things in my code. What will happen is pic1 and pic2 will display random pictures from array imageArr[]. They if statement at the bottom will compare these to see if either the array number or the name of the image file (ie. image1.png, image2.png...etc) are the same. Does that make more sense?
    Here is my updated code as of now.

    Java Code:
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import java.util.*;
    /*
     * @author jmdelong
     */
    public class samediff extends JFrame implements ActionListener
    {
        JButton btnSame = new JButton ("");
        JButton btnDiff = new JButton ("");
        JLabel pic1 = new JLabel("", JLabel.CENTER);
        JLabel pic2 = new JLabel("", JLabel.CENTER);
        JTextArea level = new JTextArea("");
        ImageIcon[] imageArr = new ImageIcon[10];
        int correct;
        int counter;
        int rand;
        Random generator = new Random();
    
        public static void main(String[] args)
        {
            new samediff();
        }
    
        public samediff()
        {
            try
            {
                for(int i=0; i<10; i++)
                {
    
                    imageArr[i] = new ImageIcon("images/image" + i + ".png");
                }
                 
                String SamePath = "images/same.png"; //image courtesy of iconlet.com
                File same = new File(SamePath);
                BufferedImage SameImage = ImageIO.read(same);
    
                String DiffPath = "images/diff.png"; //image courtesy of iconlet.com
                File diff = new File(DiffPath);
                BufferedImage DiffImage = ImageIO.read(diff);
    
                String laf = UIManager.getCrossPlatformLookAndFeelClassName();
                try
                {
                    UIManager.setLookAndFeel(laf);
                }
                catch(Exception e)
                {
                    System.err.println("Error loading L&F: "+e.getMessage());
                }
    
                btnSame = new JButton(new ImageIcon(SameImage));
                btnSame.setBackground(Color.WHITE);
                btnSame.setOpaque(true);
    
                btnDiff = new JButton(new ImageIcon(DiffImage));
                btnDiff.setBackground(Color.WHITE);
                btnDiff.setOpaque(true);
                
                this.setTitle("Same Different Game");
                this.setLayout(new GridLayout(0,2));
                this.setBackground(Color.WHITE);
                
                this.setSize(1000,900);
                this.setVisible(true);
    
                rand = generator.nextInt(10);
                pic1.setIcon(imageArr[rand]);
    
                rand = generator.nextInt(10);
                pic2.setIcon(imageArr[rand]);
                
                this.add(pic1);
                this.add(pic2);
                this.add(btnSame);
                this.add(btnDiff);
    
                btnSame.addActionListener(this);
                btnDiff.addActionListener(this);
    
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
            catch(Exception f)
            {
                System.out.println(f);
            }
        }
        public void actionPerformed (ActionEvent e)
        {
            counter++;
            if(counter == 5)
            {
                this.remove(pic1);
                this.remove(pic2);
                this.remove(btnSame);
                this.remove(btnDiff);
                this.repaint();
    
                this.
                level.setText("Thanks for playing The Same Different Game! You got " + correct +" out of 10 correct! Great Job!");
                this.add(level, JTextArea.LEFT_ALIGNMENT);
            }
            if(e.getSource() == btnSame)
            {
                System.out.println("I've been clicked");
                if(pic1.equals(pic2))
                {
                    correct++;
                    System.out.println("Correct!");
                }
                else
                {
                    System.out.println("Wrong");
                }
                rand = generator.nextInt(10);
                pic1.setIcon(imageArr[rand]);
                //pic1 = new JLabel("", imageArr[rand], JLabel.CENTER);
                rand = generator.nextInt(10);
                pic2.setIcon(imageArr[rand]);
                //pic2 = new JLabel("", imageArr[rand], JLabel.CENTER);
            }
            if(e.getSource() == btnDiff)
            {
                System.out.println("Different button");
    
                if(pic1.equals(pic2))
                {
    
                    System.out.println("Wrong");
                }
                else
                {   correct++;
                    System.out.println("Correct");
                }
                rand = generator.nextInt(10);
                pic1.setIcon(imageArr[rand]);
                //pic1 = new JLabel("", imageArr[rand], JLabel.CENTER);
                rand = generator.nextInt(10);
                pic2.setIcon(imageArr[rand]);
                //pic2 = new JLabel("", imageArr[rand], JLabel.CENTER);
            }
        }
    }

Similar Threads

  1. c always equals a*b
    By imorio in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 02:32 PM
  2. == and equals()
    By arefeh in forum New To Java
    Replies: 13
    Last Post: 01-05-2010, 04:56 PM
  3. == is same as .equals()??
    By DrMath in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 04:57 AM
  4. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 10:26 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 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
  •