简体   繁体   中英

Comparing components via setName() .

I am coding an image puzzle game and one part of the code is to compare the pieces the user has selected to the pieces of the correct image.

Each image piece is already added to a JButton as an ImageIcon.

An identifier is required to differentiate each image piece apart and also for comparision.

I am setting a setName() for each JButton created as the identifier.

The comparison starts when the user releases the mouse after he drags the puzzle pieces from the original 3x3 grid where the shuffled pieces are to the other 3x grid for matching.

I have problems removing the error from the comparison if statement.

I got the comparison idea from this SO thread - link

    private JButton[] button = new JButton[9];
    private JButton[] waa = new JButton[9];

    private String id;
    private int cc;
    private String id2;
    private int cc2;

    // setName for each of the 9 buttons in the original 3x3 grid being created 
    // which stores the shuffled puzzle pieces
    for(int a=0; a<9; a++){
        button[a] = new JButton(new ImageIcon());
        id += Integer.toString(++cc);
        button[a].setName(id); 
    }

    // setName for each of the 9 buttons in the other 3x3 grid  
    // where the images will be dragged to by the user
        for(int b=0; b<9; b++){
        waa[b] = new JButton();
        id2 += Integer.toString(++cc2);
        waa[b].setName(id2); 
    }

    // check if puzzle pieces are matched in the correct place
    // compare name of original 'button' array button with the name of 'waa' array buttons 
        button[a].addMouseListener(new MouseAdapter(){

            public void mouseReleased(MouseEvent m){
                if(m.getbutton().getName().equals (waa.getName())){

                    }
                    else{
                         JOptionPane.showMessageDialog(null,"Wrong! Try Again.");
                    }
            }
        }

In your mouseReleased event m.getButton() is returning the mouse button that was clicked. You'll want to do something more like this that will get you closer:

if (m.getComponent().getName().equals(waa.getName())) {

m.getComponent() returns the Component object (your JButton ) that the event was fired from. From there you can do the comparison with the getName approach you are using.

There's an additional issue in that your waa variable is an array. I'm not sure how you want to compare them, whether running through the arrays and making sure the index and names match, but that's an additional issue you need to look into.

JButton uses an ActionListener to trigger notifications back to your program to indicate when it's been triggered. This allows the button to respond to different types of event, including the mouse, keyboard and program triggers.

As apart of the action API, you can supply an action command for each button. See JButton#setActionCommand

Basically you would integrate it in a simular way to your mouse listener...

public void actio Performed(ActionEvent evt) {
    if (command.equals(evt.getActionCommand()) {...}
}

Depending on your requirements, it might even be easier to use the Action API

The problem you are actually having is waa is an array, therefore, it doesn't have a getName method. I'm also unclear as to why you have two arrays of buttons?

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