0

This is a followup question from my previous question. I am trying to solve the issue in the previous question. link

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 moves the mouse cursor to a specific button coordinate on the other 3x3 grid as the user drags the pieces from the original 3x3 grid.

Edited : Edited code is marked. Flex seems to have a method which can detect the object which is under the cursor, does java have it ? Link ^ getSource() solves the above issue.

However, when I tried system.out.println to see when does the button run the getName(), it appears that it doesn't run when I drag the pieces to the button; to activate getName(), I must further hover over the button after dragging the image pieces to the button =/ .

private int mouseX,mouseY;
private JButton[] button = new JButton[9];
private JButton[] userarea = new JButton[9];

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

// create 9 buttons for the user to place the puzzle pieces to
for(int b=0; b<9; b++){
         userarea[b] = new JButton();
         id2 += Integer.toString(++cc2);
         // setName for each button (as unique id)
         userarea[b].setName(id2); 
         TransferHandler transfer1 = new TransferHandler("icon");
         userarea[b].setTransferHandler(transfer1);
    //<--- EDITED CODE ------>
            ChangeListener clistener = new ChangeListener(){                 
               public void stateChanged(ChangeEvent ce) {

                     JButton source = (JButton)ce.getSource();
                     ButtonModel mod = source.getModel(); 
                     if (mod.isRollover()){
                     System.out.println(source.getName());
                     }
                     else{

                 }
             }
         };
   //<--- EDITED CODE ------>
}


public void nameOfDestButton() {

            // not sure how to do this
            if(mouseCoordinate == getBounds of one of the buttons){
                userarea[appropriate button number].getName();
            }
}

public void mouseMoved(MouseEvent m){
        mouseX=m.getX();
        mouseY=m.getY();

        nameOfDestButton();

        // not sure how to do this. Obtaining a return value of getName
        // in the nameOfDestButton() method ?
        if (m.getComponent().getName().equals(nameOfDestButton) {  

        }
        else{
             JOptionPane.showMessageDialog(null,"Wrong! Try Again.");
        }
    }         
Community
  • 1
  • 1
iridescent
  • 305
  • 4
  • 14
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Why are you trying to determine the bounds of components? That is rarely necessary. – Andrew Thompson Jan 23 '13 at 03:22
  • 1) I have tried my best in posted only the relevant code. I will see if more code reductions is possible :) 2) This is to get the coordinates of each button. The purpose is for the mouse cursor coordinate to compare with the coordinate of each button, to determine which button the mouse cursor is currently hovering over. – iridescent Jan 23 '13 at 03:28
  • You might want to accept an answer on that previous question you linked. – Perception Jan 23 '13 at 03:31
  • 3
    Don't use getBounds which is much too low level and prone to error. Why not instead add a ChangeListener to your buttons' models and listen for hover (`isRollOver()` is true)? – Hovercraft Full Of Eels Jan 23 '13 at 03:40
  • It sounds like you need to translate the mouse point from a child component to it's parent. Basically, I think you need to look at [`SwingUtilities.convertPoint`](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#convertPoint%28java.awt.Component,%20int,%20int,%20java.awt.Component%29) – MadProgrammer Jan 23 '13 at 03:41
  • *"I have tried my best in posted only the relevant code."* An SSCCE requires more than just relevant code (as if you could tell, short of a solution, what is relevant). The code needs to be compilable. *"I will see if more code reductions is possible"* The document which you seem to have ignored has several suggestions. E.G. the code is currently dealing with 10 `Rectangle` instances. Try and do this with *just **2!*** Replace a 3x3 grid with 2x2 for 5 less of whatever it requires. etc. – Andrew Thompson Jan 23 '13 at 03:48
  • @HovercraftFullOfEels - Oh I will try out your suggestion. It sound much less complicated. – iridescent Jan 23 '13 at 03:54
  • @AndrewThompson - Ahh okay, noted =/ . My bad.. Will do a full read up of the SSCCE document you drawn up asap. – iridescent Jan 23 '13 at 03:54
  • @HovercraftFullOfEels - Hmm I realised `isRollOver` does not have the ability to know which exact button it rolled over. – iridescent Jan 23 '13 at 05:21
  • 1
    The change listener's ChangeEvent object's `getsource()` method will return a reference to the listened to's model. You should be able to figure out how to code this. – Hovercraft Full Of Eels Jan 23 '13 at 22:29
  • Okay thanks, got it figured out, but somehow the getName() works only after a further hover after dragging the image pieces to a button. It doesn't work immediately when the user drags the image pieces above a button. – iridescent Jan 24 '13 at 13:13

0 Answers0