title: JavaFX animation does not work in a Stack Pane
body: <p>I am working on a class assignment, a pong game. 
  Below is the requirement.</p>
  
  <p>A rectangular paddle moves back and forth via mouse drag along the bottom of the pane;
  If the ball connects with the paddle, then it bounces at a 90-degree angle back into the pane space;</p>
  
  <p>The mouse and keyboard actions do not work properly, no response to mouse and keys.</p>
  
  <p>I can't figure out the problem. Any helps will be really appreciated.</p>
  
  <p>PongForOne.java</p>
  
  <pre><code>public class PongForOne extends Application {
      @Override
      public void start(Stage primaryStage) {
          BallPane ball = new BallPane();
          PaddlePane paddle = new PaddlePane();
          StackPane stkPane = new StackPane();
  
          stkPane.getChildren().addAll(paddle,ball);
  
          paddle.setOnMouseDragged(e -&gt; {
              PaddlePane.paddle.setX(e.getX());
          });
  
          paddle.setOnKeyPressed(e -&gt; {
              switch(e.getCode()){
                  case LEFT:  PaddlePane.paddle.setX(PaddlePane.paddle.getX() - 10);  break;
                  case RIGHT: PaddlePane.paddle.setX(PaddlePane.paddle.getX() + 10);  break;
                  default: break;
              }
          });
  
          Scene scene = new Scene(stkPane, 300, 400);
          primaryStage.setTitle("Hello World!");
          primaryStage.setScene(scene);
          primaryStage.show();
  
          ball.requestFocus();
          paddle.requestFocus();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>BallPane.java</p>
  
  <pre><code>public class BallPane extends Pane {
      private final double radius = 20;
      private double x = radius, y = radius;
      private double dx = 1, dy = 1;
      private int numBallHitsPaddle = 0;
      private int numBallMissPaddle = 0;
      private final Circle circle = new Circle(x,y, radius);
      private final Text text= new Text (x, y, Integer.toString(numBallHitsPaddle));
      private final Timeline animation;
  
      public BallPane(){
          circle.setFill(Color.GREEN);
          getChildren().addAll(circle, text);
          animation = new Timeline(new KeyFrame(Duration.millis(5), e -&gt; moveBall()));
          animation.setCycleCount(Timeline.INDEFINITE);
          animation.play();
      }
      public void play(){
          animation.play();
      }
      public void pause(){
          animation.pause();
      }
      public void increaseSpeed(){
          animation.setRate(animation.getRate() + 0.1);
      }
      public void decreaseSpeed(){
          animation.setRate(
              animation.getRate() &gt; 0 ? animation.getRate() - 0.1 : 0);
      }
      public void colorChange(){
          circle.setFill(Color.color(Math.random(), Math.random(), Math.random()));
      }
      public DoubleProperty rateProperty(){
          return animation.rateProperty();
      }
      public boolean ballHitsPaddle(){
          boolean status = false;
          if (x + radius &gt; PaddlePane.getPaddleX() &amp;&amp; x - radius &lt; PaddlePane.getPaddleX() + PaddlePane.getPaddleWidth() &amp;&amp; y + radius == PaddlePane.getPaddleY()){
               status = true;
          }
          return status;
      }
      /**
       * defines ball's movement
       * changes color every time when ball hits paddle and counts the number of hit
       */
      public void moveBall(){
          if (x &lt; radius || x &gt; getWidth() - radius){
              dx *= -1;
          } 
          if ( y &gt; getHeight() - radius){
              x = getWidth() / 2;
              y = 50;
              dx *= -1;
              numBallMissPaddle++;
          }
          if (y &lt; radius || ballHitsPaddle()){
              if(ballHitsPaddle()) {
                  text.setText(Integer.toString(++numBallHitsPaddle));
                  colorChange();
              }
              dy *= -1; 
          }
          x += dx;
          y += dy;
          circle.setCenterX(x);
          circle.setCenterY(y);
          text.setX(x - 4);
          text.setY(y + 3);
      }   
  }
  </code></pre>
  
  <p>PaddlePane.java</p>
  
  <pre><code>public class PaddlePane extends Pane{
      private static final double width = 40, hight = 5;
      public static Rectangle paddle = new Rectangle(width, hight);
  
  
      public PaddlePane(){
          paddle.xProperty().bind(widthProperty().divide(2).subtract(width / 2));
          paddle.yProperty().bind(heightProperty().subtract(100));
          getChildren().add(paddle);
      }
      public static double getPaddleX(){
          return paddle.getX();
      }
      public static double getPaddleY(){
          return paddle.getY();
      }
      public static double getPaddleWidth(){
          return paddle.getWidth();
      }
      public static double getPaddleHeight(){
          return paddle.getHeight();
      }
      public void colorChange(){
          paddle.setFill(Color.color(Math.random(), Math.random(), Math.random()));
      }
  }
  </code></pre>