title: How to not let my image move out of scene
body: <p>Here is my following code: </p>
  
  <pre><code>    public void start(Stage primaryStage) throws Exception {
      Pane root = new Pane();
      Scene scene = new Scene(root, 500, 500, Color.RED);
  
      ImageView dice = new ImageView(new Image(getClass().getResourceAsStream("dice.jpeg")));
      dice.setX(0);
      dice.setY(300);
      root.getChildren().add(dice);
  
      scene.setOnKeyPressed(e -&gt; {
          if (dice.getX() &gt;= 0 &amp;&amp;  dice.getX() &lt;= 500 ) {
              switch (e.getCode()) {
  
                  case RIGHT:
                      dice.setX(dice.getX() + KEYBOARD_MOVEMENT_DELTA);
                      break;
  
                  case LEFT:
                      dice.setX(dice.getX() - KEYBOARD_MOVEMENT_DELTA);
                      break;
               }
             }
      });
  
      primaryStage.setScene(scene);
      primaryStage.show();
  }
  </code></pre>
  
  <p>In my code, my image <code>dice</code> can move left and right, but I don't want it to go outside the scene. I want it to not move once it reaches to end of the scene in both left and right. I tried doing it with <code>if</code> statement, but it doesn't work. Is there any way I can stop my image <code>dice</code>to not move out of scene? Any help is appreciated! </p>