如何比较两个 ImageIcons?
How to Compare two ImageIcons?
基本上,我在棋盘游戏类型中使用了 2 张图片,而且我会不时更改它,
所以我需要能够检查两个是否具有相同的 imageIcon。
例如,如果两者都使用资源文件夹中的 "pirosfigura.png"。
public String malomcheck() {
String pirosicon=lblNewLabel.getIcon().toString();
String pirosfilenev = pirosicon.substring(pirosicon.lastIndexOf("/" ) + 1);
String iconfilenev = labelhely_1.getIcon().toString();
String filenev = iconfilenev.substring(iconfilenev.lastIndexOf("/" ) + 1);
if(filenev==pirosfilenev) {
lblJtkos.setText("piros malom.");
JOptionPane.showMessageDialog(null, "working");
return "lefutott";
}
return "notworking. very sad.";
}
顺便说一句,getIcon().toString() 的 return 值是 javax.swing.ImageIcon@cd7e8021
我猜这是指内存位置(?)所以它对每个 运行 和每个图像都是随机的,因此它似乎无法使用。
实现此目的的一种方法是保留您自己的 ImageIcon
到文件的映射,这样每当您加载 ImageIcon
时,您都可以将其存储在 Map
中一个键及其文件或一些符号 name/enum 作为值。这样,当您想比较 imIc1
和 imIc2
时,您可以这样写:
if (map.get(imIc1).equals(map.get(imIc2)) { ... }
或(如果您有描述性字符串值)
if (map.get(imIc1).equals("NOT_WORKING_ICON") { ... }
或(如果您使用的是枚举值)
if (map.get(imIc1) == NOT_WORKING_ICON ) { ... }
it's so weird for me that there is no method to get to the filepath the Jlabel is using for an image.
很有道理。 JLabel
显示 Icon
。
为什么 JLabel 应该知道或关心文件路径?
您可以自己实现 Icon
接口并为 Icon
进行自定义绘制。所以并不是所有的图标都会有一个文件路径。只有 ImageIcon 是从文件创建的。
文件名属性属于ImageIcon
.
By the way the return value of the getIcon().toString() is javax.swing.ImageIcon@cd7e8021
Image piros=new ImageIcon(this.getClass().getResource("pirosfigura.png")).getImage();
celpont.setIcon(new ImageIcon(piros));
查看您正在使用的上述代码。
您正在从图像创建图标,因此文件信息丢失。
相反,您应该直接创建 ImageIcon:
ImageIcon icon = new ImageIcon( this.getClass().getResource("pirosfigura.png") );
celpont.setIcon( icon );
System.out.println( celpont.getIcon() );
我相信 ImageIcon 会将文件名保存为 ImageIcon 的 "description"。看来 toString() 将 return 描述。
基本上,我在棋盘游戏类型中使用了 2 张图片,而且我会不时更改它, 所以我需要能够检查两个是否具有相同的 imageIcon。 例如,如果两者都使用资源文件夹中的 "pirosfigura.png"。
public String malomcheck() {
String pirosicon=lblNewLabel.getIcon().toString();
String pirosfilenev = pirosicon.substring(pirosicon.lastIndexOf("/" ) + 1);
String iconfilenev = labelhely_1.getIcon().toString();
String filenev = iconfilenev.substring(iconfilenev.lastIndexOf("/" ) + 1);
if(filenev==pirosfilenev) {
lblJtkos.setText("piros malom.");
JOptionPane.showMessageDialog(null, "working");
return "lefutott";
}
return "notworking. very sad.";
}
顺便说一句,getIcon().toString() 的 return 值是 javax.swing.ImageIcon@cd7e8021 我猜这是指内存位置(?)所以它对每个 运行 和每个图像都是随机的,因此它似乎无法使用。
实现此目的的一种方法是保留您自己的 ImageIcon
到文件的映射,这样每当您加载 ImageIcon
时,您都可以将其存储在 Map
中一个键及其文件或一些符号 name/enum 作为值。这样,当您想比较 imIc1
和 imIc2
时,您可以这样写:
if (map.get(imIc1).equals(map.get(imIc2)) { ... }
或(如果您有描述性字符串值)
if (map.get(imIc1).equals("NOT_WORKING_ICON") { ... }
或(如果您使用的是枚举值)
if (map.get(imIc1) == NOT_WORKING_ICON ) { ... }
it's so weird for me that there is no method to get to the filepath the Jlabel is using for an image.
很有道理。 JLabel
显示 Icon
。
为什么 JLabel 应该知道或关心文件路径?
您可以自己实现 Icon
接口并为 Icon
进行自定义绘制。所以并不是所有的图标都会有一个文件路径。只有 ImageIcon 是从文件创建的。
文件名属性属于ImageIcon
.
By the way the return value of the getIcon().toString() is javax.swing.ImageIcon@cd7e8021
Image piros=new ImageIcon(this.getClass().getResource("pirosfigura.png")).getImage();
celpont.setIcon(new ImageIcon(piros));
查看您正在使用的上述代码。
您正在从图像创建图标,因此文件信息丢失。
相反,您应该直接创建 ImageIcon:
ImageIcon icon = new ImageIcon( this.getClass().getResource("pirosfigura.png") );
celpont.setIcon( icon );
System.out.println( celpont.getIcon() );
我相信 ImageIcon 会将文件名保存为 ImageIcon 的 "description"。看来 toString() 将 return 描述。