1

I ran into an issue while I'm making a 4 player chess game. I am unable to see if two ImageIcons are the same. I have four arrays for the red, blue, green, and yellow pieces and my idea was to see if what piece the player clicked on matched any of the pieces in their color array. However if I say like if(colorIcon.equals(clickedIcon)) it returns false. I know that is because .equals() refers to the reference and I'm making new space in the memory. So is there any way I can compare two ImageIcons? Thanks for reaading!

1
  • 5
    You should not be comparing the ImageIcons. Do you have a class representing the chess piece? If so, you should compare the type of chess piece instead. If you find yourself needing to compare the image icon to solve the problem, I think there is a design problem in your code.
    – gigadot
    Nov 29, 2011 at 11:06

4 Answers 4

1

You can always do:

public class MyImageIcon extends ImageIcon{
   String imageColor;
   // Getters and setters...  
   // Appropriate constructor here.
   MyImageIcon(Image image, String description, String color){
       super(image, description);
       imageColor = color;
   }
   @Override
   public bool equals(Object other){
      MyImageIcon otherImage = (MyImageIcon) other;
      if (other == null) return false;
      return imageColor == other.imageColor;
   }
}

And use this class instead of a raw ImageIcon

Instead of having:

ImageIcon myImage = new ImageIcon(imgURL, description);

you would have:

MyImageIcon myImage = new MyImageIcon (imgURL, description, "RED");
2
  • Thanks for the help I didn't exactly do something like this but I created my own JLabel class and worked with that.
    – D347th
    Nov 29, 2011 at 20:35
  • Shouldn't that use String.equals() instead of String == String?
    – Bowi
    Mar 3, 2017 at 8:13
0

.equals() doesn't refer to the same memory reference. It's a method that compares the objects; it's == that compares the references.

2
  • 4
    ImageIcon class inherits but not implement the equalsmethod from Object. Object.equals according to java doc is: "The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true)." Hence, equals and == are exactly the same for the ImageIcon class
    – GETah
    Nov 29, 2011 at 17:56
  • Thank you that was what I meant by .equals
    – D347th
    Nov 29, 2011 at 20:34
0

Its really simple as this:

ImageIcon i=new ImageIcon("getClass().getResource("image.jpg");//this is the image you want to compare to jLabel's icon
if(jLabel1.getIcon().equals(i){
  //...do something
}
0

I did this:

String Icon1=Button1.getIcon().toString();
String Icon2=Button2.getIcon().toString();

if(Icon1.equals(Icon2)){
   System.out.println("Yes");
}else{
   System.out.println("No");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.