Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较两个图像图标?_Java_Swing_Compare_Imageicon - Fatal编程技术网

Java 比较两个图像图标?

Java 比较两个图像图标?,java,swing,compare,imageicon,Java,Swing,Compare,Imageicon,我在下棋时遇到了一个问题。我看不出两个图像图标是否相同。我有四个红色、蓝色、绿色和黄色棋子的数组,我的想法是看看玩家点击的棋子是否与他们颜色数组中的任何棋子匹配。但是,如果我说like if(colorIcon.equals(clickedIcon))则返回false。我知道这是因为.equals()引用了引用,我在内存中创建了新的空间。有什么方法可以比较两个图像图标吗?谢谢你的阅读 您可以随时执行以下操作: public class MyImageIcon extends ImageIcon{

我在下棋时遇到了一个问题。我看不出两个图像图标是否相同。我有四个红色、蓝色、绿色和黄色棋子的数组,我的想法是看看玩家点击的棋子是否与他们颜色数组中的任何棋子匹配。但是,如果我说like if(colorIcon.equals(clickedIcon))则返回false。我知道这是因为.equals()引用了引用,我在内存中创建了新的空间。有什么方法可以比较两个图像图标吗?谢谢你的阅读

您可以随时执行以下操作:

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;
   }
}
并使用该类而不是原始图像图标

而不是:

ImageIcon myImage = new ImageIcon(imgURL, description);
你应该:

MyImageIcon myImage = new MyImageIcon (imgURL, description, "RED");
您可以随时执行以下操作:

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;
   }
}
并使用该类而不是原始图像图标

而不是:

ImageIcon myImage = new ImageIcon(imgURL, description);
你应该:

MyImageIcon myImage = new MyImageIcon (imgURL, description, "RED");

.equals()
不引用相同的内存引用。这是一种比较对象的方法;比较引用的是
==

.equals()
不引用相同的内存引用。这是一种比较对象的方法;比较引用的是
=

非常简单:

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
}

其实很简单:

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
}

您不应该比较图像图标。你有代表国际象棋的课吗?如果是这样,你应该比较棋子的类型。如果您发现自己需要比较图像图标来解决问题,我认为代码中存在设计问题。您不应该比较图像图标。你有代表国际象棋的课吗?如果是这样,你应该比较棋子的类型。如果您发现自己需要比较图像图标来解决问题,我认为代码中存在设计问题。ImageIcon类继承但未实现
对象中的
equals
方法<根据java文档,code>Object.equals
是:“类对象的equals方法在对象上实现了最有区别的等价关系;也就是说,对于任何非空引用值x和y,当且仅当x和y引用同一对象时(x==y的值为true),此方法才返回true。”,
equals
=
对于
ImageIcon
类来说是完全相同的谢谢这就是我的意思。equalImageIcon类从
对象继承但不实现
equals
方法<根据java文档,code>Object.equals
是:“类对象的equals方法在对象上实现了最有区别的等价关系;也就是说,对于任何非空引用值x和y,当且仅当x和y引用同一对象时(x==y的值为true),此方法才返回true。”,
equals
=
对于
ImageIcon
类来说是完全相同的谢谢你这就是我的意思。equals谢谢你的帮助我并没有这样做,但我创建了自己的JLabel类并使用它。这不应该使用
String.equals()
而不是
String==String
?感谢您的帮助,我并没有这样做,但我创建了自己的JLabel类并使用了它。它不应该使用
String.equals()
而不是
String==String