我在製作4人棋牌遊戲時遇到了問題。我無法看到兩個ImageIcons是否相同。我爲紅色,藍色,綠色和黃色作品提供了四個陣列,我的想法是查看玩家點擊的顏色是否與他們的顏色陣列中的任何作品匹配。然而,如果我說如果(colorIcon.equals(clickedIcon))它返回false。我知道那是因爲.equals()引用了引用,並且我正在內存中創建新的空間。那麼有什麼辦法可以比較兩個ImageIcons?感謝您的回覆!比較兩個圖像圖標?
1
A
回答
1
你總是可以做到:
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
而是具有:
ImageIcon myImage = new ImageIcon(imgURL, description);
你會:
MyImageIcon myImage = new MyImageIcon (imgURL, description, "RED");
0
0
它很簡單,因爲這:
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
}
相關問題
- 1. 比較圖像的兩個座標值
- 2. OSX:比較兩個圖像
- 3. 比較兩個圖像android
- 4. 兩幅圖像的圖像比較
- 5. 比較兩幅圖像
- 6. 將兩個圖像與ImageMagick比較
- 7. 在JavaScript中比較兩個圖像
- 8. 比較Android中的兩個圖像
- 9. 比較兩個圖像問題
- 10. 使用OpenCV比較兩個圖像
- 11. 如何比較兩個圖像uisng vb.net?
- 12. 比較兩個圖像是否相同
- 13. 比較低質量的兩個圖像
- 14. ios比較兩個圖像/路徑
- 15. 比較IOS中的兩個圖像8
- 16. 標誌的圖像比較
- 17. 比較兩個圖片
- 18. 比較兩個視圖
- 19. 創建位圖來比較兩個圖像像素
- 20. Python:用戶界面圖像擦除,比較兩個圖像
- 21. 比較圖像
- 22. 圖像比較
- 23. 比較PHP中的兩幅圖像
- 24. 直觀地比較兩幅圖像
- 25. 位圖圖像比較
- 26. 使用BioAPI轉換和比較兩個指紋位圖圖像
- 27. [Android]比較兩個圖像/繪圖在Android的記憶遊戲?
- 28. 如何比較圖像的兩個直方圖?
- 29. 使用直方圖比較兩個圖像
- 30. SIFT-圖像比較
你不應該比較ImageIcons。你有代表棋子的班級嗎?如果是這樣,你應該比較一下棋子的類型。如果您發現自己需要比較圖像圖標來解決問題,我認爲您的代碼中存在設計問題。 – gigadot