0

I am trying to have it so once my mouse is pressed its gets fired once and only once. However, it keeps getting fired twice. I am not sure if this is because I am going through a for loop, but I want the mouse listener to catch what index of the array that I am clicking on. That is why I assign it to each index in the 2d array, I am not sure if theres a better way of doing this.

Code:

public boolean firstClick = false;
public boolean secondClick = false;
for (int i = 7; i >= 0; i--) {
        for (int j = 0; j < 8; j++) {
            int tempi = i;
            int tempj = j;
            pnlCells[i][j].add(getPieceObject(str[(7 - i)][j]), BorderLayout.CENTER);
            pnlCells[i][j].validate();
            pnlCells[i][j].addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {

                    try {
                        if (firstClick == false || secondClick == false) {


                                if (firstClick == false) {
                                    mouseX = tempj;
                                    mouseY = 7 - tempi;
                                     System.out.println("First You pressed" +  mouseX + ", " + mouseY);   
                                    firstClick = true;
                                    sourceColor = pnlCells[mouseX][mouseY].getForeground();
                                    pnlCells[mouseX][mouseY].setForeground(Color.yellow);
                                    pnlCells[mouseX][mouseY].repaint();
                                    pnlBoard.repaint();
                                    pnlMain.repaint();
                                } else if (secondClick == false) {
                                    newMouseX = tempj;
                                    newMouseY = 7 - tempi;
                                    System.out.println("Second You pressed" +  newMouseX + ", " + newMouseY);  
                                    secondClick = true;
                                }

                                if (firstClick == true && secondClick == true) {
                                    firstClick = false;
                                    secondClick = false;
                                    pnlCells[mouseX][mouseY].setForeground(sourceColor);
                                    pnlCells[mouseX][mouseY].repaint();
                                    pnlBoard.repaint();
                                    pnlMain.repaint();
                                    PlayerMove pM = turn(); //send turn to server
                                    objectOut.writeObject(pM); //send turn to server
                                    System.out.println(name + ": sent move to server");
                                    s.suspend();
                                }


                        }
                    } catch (IOException ex) {
                        Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            //System.out.println(j+", "+(i)+":"+v.str[(7-i)][j]);

        }
    }

Once I click the mouse once it always prints First You pressed xcoord1, ycoord1 Second You pressed xcoord2, ycoord2

user3339242
  • 631
  • 2
  • 15
  • 32
  • Where are `firstClick` and `secondClick` defined? – MadProgrammer Apr 10 '15 at 02:48
  • @MadProgrammer just added it to code – user3339242 Apr 10 '15 at 02:52
  • Currently, when you click on a cell `firstClick` will become true, the next cell (no matter which one) to be clicked will then react to `secondClick` been `false` - is that the behavior you want? – MadProgrammer Apr 10 '15 at 03:02
  • @MadProgrammer Yes. When i click on th the first location I want it to go through the first if statement on the second clicked location I want it to go in the else if statement – user3339242 Apr 10 '15 at 03:05
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Apr 10 '15 at 03:06
  • @MadProgrammer Ok, the main program is just a chess program. I want to click one piece and then click another location on the board so the piece can move in that location. – user3339242 Apr 10 '15 at 03:10

1 Answers1

0

I am assuming that Both firstClick and secondClick are false at the init. Then when we go in the loop, the firstClick will always fire and then you set it to true. In the next iteration, the secondClick will always fire because it is false and firstClick will not fire because it is true now.

Set Both firstClick and secondClick to true once any of these fire.

azmuhak
  • 964
  • 1
  • 11
  • 31
  • I am not sure I follow, I need to make sure I have two clicks and that on the second click it goes into that second click if statement. When do you suggest I set them both to true? – user3339242 Apr 10 '15 at 02:52
  • how about using a counter then? increment it to 1 on the firstClick and do not do anything apart from saving the values in some temp variables. Then increment it to 2 on the second click and use the temp and the new values, do your processing and send them to the server. Its not a perfect practice to make 7*8 mouse listeners. make one mouse listener and get the source information from the mouse event. – azmuhak Apr 10 '15 at 03:03
  • something like this perhaps ? : int s = (int)e.getSource() – azmuhak Apr 10 '15 at 03:06
  • Can you show me how I would go by only creating 1 mouse listener in my case? I was thinking about that but wasnt sure how to get the current indexes of my array. I will try a counter. – user3339242 Apr 10 '15 at 03:07