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 to check if it is correct.

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 am trying to compare the names of the source image with the names of the destination buttons of the grid which the user is trying to place the pieces to.

x.getName() doesn't work as x is an array. So I tried to implement setAction for each button to getName, but I couldn't quite understand how to use it =/

Any help is appreciated.

    private JButton[] button = new JButton[9];
    private JButton[] x = 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(image));
        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++){
        x[b] = new JButton();
        id2 += Integer.toString(++cc2);
        x[b].setName(id2); 
    // for each button, setAction to getName 
        x[0].setAction(new ButtonAction(x.getName())); // error here
    }

    // 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 mouseMoved(MouseEvent m){
                if (m.getComponent().getName().equals(x.getName())) {  // error here

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

Because x is an array you need to supply an index to tell Java which element of the array you are talking about, eg x[b].getName()

Hmm.. Is it possible to getName() the button in which the mouse is hovered upon ?

If I set it to a fixed button like x[1].getName() , the logic seems to be wrong..

Your moouseMoved method has a MouseEvent as parameter. MouseEvent has a getComponent() method that returns the conponent (your JButton), so you can use getName on that component

The getComponent() method is only able to return the source JButton, not the destination JButton right ?

In my code - if (m.getComponent().getName().equals(x.getName())) , m.getcomponent() refers to the JButton that was clicked when the image is dragged over.

When I dragged it over and placed the Image from the source JButton to the new JButton, I can't use getName() of the new JButton.

YOu could use the mouseEntered listener method - I haven't tried it, but I would expect that gives you the right component.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.