简体   繁体   English

如何比较两个 ImageIcon?

[英]How to Compare two ImageIcons?

Basically, i'm using 2 images for a board game type of thing, and i change it from time to time, So i need to be able to check if two has the same imageIcon.基本上,我将 2 张图像用于棋盘游戏类型,并且我不时更改它,因此我需要能够检查两个图像是否具有相同的 imageIcon。 For example if both uses "pirosfigura.png" from the resources folder.例如,如果两者都使用资源文件夹中的“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.";
    }

By the way the return value of the getIcon().toString() is javax.swing.ImageIcon@cd7e8021 which is refers to the memory place i guess(?) so it's random with every run and for every image therefore it's seems unusable.顺便说一下,getIcon().toString() 的返回值是 javax.swing.ImageIcon@cd7e8021,它指的是我猜测的内存位置(?),因此每次运行和每个图像都是随机的,因此它似乎无法使用。

One way you can achieve this, is to keep your own mapping of ImageIcon s to files, so that whenever you load an ImageIcon you store it in a Map as a key and its file or some symbolic name/enum as value.您可以实现这一点的一种方法是保留您自己的ImageIcon到文件的映射,以便每当您加载ImageIcon您都将它作为键存储在Map ,并将其文件或某些符号名称/枚举作为值存储。 This way when you want to compare imIc1 and imIc2 you would write something like:这样,当您想比较imIc1imIc2您可以编写如下内容:

if (map.get(imIc1).equals(map.get(imIc2)) { ... }

or (if you have descriptive string values )或(如果您有描述性字符串值)

if (map.get(imIc1).equals("NOT_WORKING_ICON") { ... }

or (if you are using enum values )或(如果您使用的是枚举值)

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 用于图像的文件路径。

Makes perfect sense.很有道理。 A JLabel displays an Icon . JLabel显示一个Icon

Why should a JLabel know or care about the file path?为什么 JLabel 应该知道或关心文件路径?

You could implement the Icon interface yourself and do the custom painting for the Icon .您可以自己实现Icon接口并为Icon进行自定义绘制。 So not all Icons will have a file path.所以并不是所有的图标都有一个文件路径。 Only an ImageIcon is created from a file.仅从文件创建 ImageIcon。

The property for the file name belongs to the ImageIcon .文件名的属性属于ImageIcon

By the way the return value of the getIcon().toString() is javax.swing.ImageIcon@cd7e8021顺便说一下 getIcon().toString() 的返回值是 javax.swing.ImageIcon@cd7e8021

Image piros=new ImageIcon(this.getClass().getResource("pirosfigura.png")).getImage(); 
celpont.setIcon(new ImageIcon(piros)); 

Look at the above code that you are using.查看您正在使用的上述代码。

You area creating an Icon from an Image, so the file information is lost.您从图像创建图标,因此文件信息丢失。

Instead you should just create the ImageIcon directly:相反,您应该直接创建 ImageIcon:

ImageIcon icon = new ImageIcon( this.getClass().getResource("pirosfigura.png") ); 
celpont.setIcon( icon ); 
System.out.println( celpont.getIcon() );

I believe the ImageIcon will then save the filename as the "description" for the ImageIcon.我相信 ImageIcon 然后会将文件名保存为 ImageIcon 的“描述”。 It appears the toString() will return the description.看来 toString() 将返回描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM