title: Java/JavaFX Event Handler and setFill() trouble
body: <p>I was programming a game inspired by Conway's "Game of Life". </p>
  
  <p>Although I have the overall game logic figured out (but not coded), I am still having trouble with getting the fill colors of my rectangle objects to change once the player's first turn is over. When I run my program it skips over the requirement for player one's color (Color.BLUE) and goes straight to player two's color (Color.RED). </p>
  
  <p>Here is the code:</p>
  
  <pre><code>//William Fisher
  //July.11.2017
  package cellularautomatagame;
  
  import javafx.application.Application;
  import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.StackPane;
  import javafx.stage.Stage;
  import javafx.scene.*;
  import javafx.scene.paint.*;
  import javafx.scene.canvas.*;
  import javafx.scene.input.MouseEvent;
  import static javafx.scene.paint.Color.*;
  
  
  public class CellularAutomataGame extends Application {
  
  @Override
  public void start(Stage stage) {
  
     Group root = new Group();
     Scene s = new Scene(root, 300, 300, Color.BLACK);
  
     Canvas canvas = new Canvas(1280,720);
     GraphicsContext gc = canvas.getGraphicsContext2D();
  
     root.getChildren().add(canvas);
  
     stage.setScene(s);
     stage.show();
  
     gc.setFill(WHITE);
     gc.fillRect(0, 0, 5, 720);
     gc.fillRect(0, 0, 1280, 5);
     gc.fillRect(0, 715, 1280, 5);
     gc.fillRect(1275, 0, 5, 720);
  
  
      Player player1 = new Player();
      Player player2 = new Player();
  
  
      player1.playerFirstMove(root,canvas,Color.BLUE);
      player2.playerFirstMove(root,canvas,Color.RED);
  }
  
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      launch(args);
  }
  
  }
  
  //William Fisher
  // July.11.2017
  package cellularautomatagame;
  
  import javafx.event.EventHandler;
  import javafx.scene.Group;
  import javafx.scene.Scene;
  import javafx.scene.canvas.Canvas;
  import javafx.scene.canvas.GraphicsContext;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.paint.Color;
  import static javafx.scene.paint.Color.*;
  import javafx.scene.shape.Rectangle;
  
  public class Player {
      int firstMove = 0;
  
  public void playerFirstMove(Group root,Canvas canvas,Color color){
  
      canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler&lt;MouseEvent&gt;(){
         @Override
          public void handle (MouseEvent e){
              while(firstMove &lt; 1){
                  if(e.getClickCount() == 1){
                      Rectangle r = new Rectangle(e.getX(),e.getY(),5,5);
                      r.setFill(color);
                      root.getChildren().add(r);
                      firstMove++;
                  }
              }
         }
      });
      firstMove--;
  }
  }
  
  /** (07/11/2017)Current Problem: The first player is unable to make their first move. Only the 
   * second player is able to make a first move.
   * 
   * (07/16/2017)Current Problem: Same as previous problem, changed the code so that a rectangle
   * object would spawn upon mouse click event. Problem possibly has to do with "setFill()" function.
   */
  </code></pre>
  
  <p>On line 52 of the main JavaFX method where it shows player1's first turn, it should call the playerFirstMove method and allow a blue rectangle to spawn once the mouse is clicked, as shown in the playerFirstMove method starting on line 18 of the Player class. However when the mouse is clicked, one red rectangle is spawned instead of a blue one. It is as though the program skipped over player1.playerfirstMove(...) on line 52 and went straight to the player2.playerfirstMove(...) on line 53. I've tried for hours to fix this small problem, reading the JavaFX API and searching the internet. The program is doing what I want it to do (spawn only one rectangle per mouse click) but it seems as though it is skipping the instructions on line 52 (player1.playerfirstMove(...)).</p>
  
  <p>To me it seems as though there may be a bug involving the setFill() function?</p>
  
  <p>I would deeply appreciate any help.</p>
  
  <p>Thanks All!</p>