6 Replies - 257 Views - Last Post: 21 January 2013 - 10:48 PM Rate Topic: -----

#1 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 08-September 12

MouseEvents not working.

Posted 21 January 2013 - 06:33 AM

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;
    }
        
}


Is This A Good Question/Topic? 0
  • +

Replies To: MouseEvents not working.

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1374
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: MouseEvents not working.

Posted 21 January 2013 - 06:45 AM

I bet the following will never be true
if((m.getSource() == waa)){


The source will never be the array waa, instead it will be one of the buttons in the array

I don't understand why you want to check on the source anyway, can't you just leave it out :) ?
Was This Post Helpful? 1
  • +
  • -

#3 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 08-September 12

Re: MouseEvents not working.

Posted 21 January 2013 - 08:00 AM

View PostCasiOo, on 21 January 2013 - 06:45 AM, said:

I bet the following will never be true
if((m.getSource() == waa)){


The source will never be the array waa, instead it will be one of the buttons in the array

I don't understand why you want to check on the source anyway, can't you just leave it out :)/> ?


Oh yeah, I blindly copied the code from ActionEvent =/ My bad.

The validation still doesn't run.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1374
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: MouseEvents not working.

Posted 21 January 2013 - 09:14 AM

It makes no sense to check if the JButton elements are equal to the String elements
if (Arrays.equals(waa,ids)) { //This will most likely not work ^^/>


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: MouseEvents not working.

Posted 21 January 2013 - 09:44 AM

And why using MouseListener over a JButton ? JButton are just JLabel with MouseListener on them that change a bit shape when clicked so:

Use JLabel with MouseListener
or
JButton with ActionListener
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: MouseEvents not working.

Posted 21 January 2013 - 10:05 AM

It looks like you're using parallel arrays. You should really avoid those in favor of a more OO solution.

See my tutorial Moving Away From Parallel Arrays.
Was This Post Helpful? 0
  • +
  • -

#7 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 08-September 12

Re: MouseEvents not working.

Posted 21 January 2013 - 10:48 PM

View PostCasiOo, on 21 January 2013 - 09:14 AM, said:

It makes no sense to check if the JButton elements are equal to the String elements
if (Arrays.equals(waa,ids)) { //This will most likely not work ^^/>/>


I did that as a comparision because since the creation of buttons using waa array will result in waa[0] for the 1st button, waa[1] for the 2nd and so forth. ( Am I wrong ? ) Next, ids array is supposedly "attached" to each puzzle piece as an identifier..

For example, if the image (eg: with an ids[1] identifier) is dragged over to the JButton of waa[1] , this means that there's a match.

View Postmacosxnerd101, on 21 January 2013 - 10:05 AM, said:

It looks like you're using parallel arrays. You should really avoid those in favor of a more OO solution.

See my tutorial Moving Away From Parallel Arrays.


Will take a look at it now :)

View Postpbl, on 21 January 2013 - 09:44 AM, said:

And why using MouseListener over a JButton ? JButton are just JLabel with MouseListener on them that change a bit shape when clicked so:

Use JLabel with MouseListener
or
JButton with ActionListener


Oh noted. But ActionListener requires the user to click on the button for the event to be triggered right ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1