Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

MouseEvents not working.

$
0
0
I am creating a 3x3 rectangular jigsaw puzzle game.

I am trying to verify that the user dragged puzzle pieces are correct based on the original image.

There are two 3x3 button GridLayouts, the first one is where the shuffled pieces are at, the second is where the user drags the pieces to.

The attached code only shows the 2nd 3x3 button GridLayout.

The idea behind what I coded so far is to have a unique identifier for each puzzle piece. This is done via a creation of another array ids counting from 1 to 9 when each subimage is created.

Subsequently, since each JButton on the 3x3 GridLayout are created using arrays and have "identifiers" for each of them like waa[0] , waa[1] etc, I am comparing the arrays of both the 3x3 GridLayout and the ids array to validate if the user placed the pieces in the correct areas.

I coded it as such, but nothing happened when I dragged over the pieces.

private JButton[] button = new JButton[9];
private JButton[] waa = new JButton[9];
private String[] ids = new String[button.length];
private String id;
private int cc;

// method setupImage() adds the subimages to the buttons
// variable wi is the already divided subimages taken from another method that does the dividing 
private void setupImage(int a, Image wi){
	button[a].setIcon(new ImageIcon( wi ));
	// variable id is the unique identifier for each puzzle piece image
	id += Integer.toString(++cc);
	ids[a] = id;
}

// Creation of 3x3 GridLayout buttons in which puzzles pieces are to be dragged over
for(int b=0; b<9; b++){
	waa[b] = new JButton();
	TransferHandler transfer1 = new TransferHandler("icon");
	waa[b].setTransferHandler(transfer1);

		waa[b].addMouseListener(new MouseAdapter(){
		         public void mouseReleased(MouseEvent m){
		            if((m.getSource() == waa)){
		                  checkPuzzle();
		                        if(puzzleSolver){

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

// check if user moved puzzle pieces match correctly
public boolean checkPuzzle(){
	// compare array ids (unique id for each piece) 
	// against array waa (3x3 buttons of the area for pieces to be dragged to)
    if (Arrays.equals(waa,ids)){
        puzzleSolver = true;
        return puzzleSolver;
    }
    else{
         puzzleSolver = false;
        return puzzleSolver;
    }
        
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>