ImageIcon in java

I am working on matching a game with photos, I think I can reset imageIcon to find out the name conteiner lable, and then reset the tag icon when ismatch returns false in the isMatch method.

write the following code on each label, reset only work on the second label. What should I do?

public ImageIcon firstChoice; public ImageIcon SecoundChoice; public boolean isSelected = false; public boolean isMatch = true; public boolean ismatch(ImageIcon firstChoice, ImageIcon secoundChoce) { if (firstChoice.getImage() == secoundChoce.getImage()) { JOptionPane.showMessageDialog(null, " wowo you got it ^^"); isMatch = true; } else { JOptionPane.showMessageDialog(null, " notmatced"); isMatch = false; } return isMatch; } // label Mouse Clicked private void label1MouseClicked(java.awt.event.MouseEvent evt) { label1.setIcon(new ImageIcon("G:/Games/icons/File Server Asia.png")); if (isSelected == true) { ImageIcon icon1 = (ImageIcon) label1.getIcon(); firstChoice = icon1; if (SecoundChoice != null && firstChoice != null) { } boolean match = ismatch(firstChoice, SecoundChoice); if (isMatch == false) { label1.setIcon(null); firstChoice = SecoundChoice = null; } } else { if (SecoundChoice == null) { ImageIcon icon1 = (ImageIcon) label1.getIcon(); SecoundChoice = icon1; isSelected = true; } if (isMatch == false) { label1.setIcon(null); } } } 
+4
source share
1 answer

I suggest you not pass ImageIcons to your ismatch(...) method, but rather pass two JLabels that contain ImageIcons. Then inside the method you can extract the ImageIcons and compare them as before, but more importantly, you have a link to the JLabels in which the icons are stored, and you can set them to a background or blank icon.

 // "second" is mispelled public boolean ismatch(JLabel firstChoiceLabel, JLabel secoundChoceLabel) { ImageIcon firstChoice = firstChoiceLabel.getIcon(); ImageIcon secoundChoice = secoundChoiceLabel.getIcon(); if (firstChoice.getImage() == secoundChoce.getImage()) { JOptionPane.showMessageDialog(null, " wowo you got it ^^"); isMatch = true; } else { JOptionPane.showMessageDialog(null, " notmatced"); isMatch = false; // here set Icon to null or to background icon. firstChoiceLabel.setIcon(null); secoundChoiceLabel.setIcon(null); } return isMatch; } 
+2
source