简体   繁体   中英

ImageIcon in java

i am working on matching photos game, i think i may be able to reset imageIcon by know the name of conteiner lable then reset the label icon when ismatch return false in the isMatch method.

the write the following code in each label, the reset work only in 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);

                }

            }

}

I suggest that you don't pass ImageIcons into your ismatch(...) method but rather pass in the two JLabels that hold the ImageIcons. Then inside the method you can extract the ImageIcons and compare them, same as before, but more importantly, you have a reference to the JLabels that hold the icons, and you can then set them to the background or null 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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