title: Panning on a canvas in javafx
body: <p>I am trying to create a canvas with javafx 2 in which the user can pan and zoom. For static content my solution works, but as soon as the content gets updated while the user is panning, the mouse events stop working until the mouse button is released and pressed again.</p>
  
  <p>The following is a minimal example which demonstrates the problem. If you click in a white area you can pan around, but if you start the panning with a click on the red rectangle it gets interrupted when the content is updated.</p>
  
  <pre><code>class Test extends StackPane
  {
      private Timer timer = new Timer();
      private Rectangle rect;
      private double pressedX, pressedY;
  
      public Test()
      {
          setMinSize(600, 600);
          setStyle("-fx-border-color: blue;");
  
          timer.schedule(new TimerTask()
          {
              @Override
              public void run()
              {
                  Platform.runLater(new Runnable()
                  {
                      @Override
                      public void run()
                      {
                          if (rect != null)
                              getChildren().remove(rect);
  
                          rect = new Rectangle(10, 10, 200, 200);
                          rect.setFill(Color.RED);
                          getChildren().add(rect);
                      }
                  });
              }
          }, 0, 100);
  
          setOnMousePressed(new EventHandler&lt;MouseEvent&gt;()
          {
              public void handle(MouseEvent event)
              {
                  pressedX = event.getX();
                  pressedY = event.getY();
              }
          });
  
          setOnMouseDragged(new EventHandler&lt;MouseEvent&gt;()
          {
              public void handle(MouseEvent event)
              {
                  setTranslateX(getTranslateX() + event.getX() - pressedX);
                  setTranslateY(getTranslateY() + event.getY() - pressedY);
  
                  event.consume();
              }
          });
      }
  }
  
  public class TestApp extends Application
  {
      public static void main(String[] args)
      {
          launch(args);
      }
  
      @Override
      public void start(Stage primaryStage)
      {
          Scene scene = new Scene(new Test());
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  }
  </code></pre>
  
  <p>I'm on Windows 8 64 bit with JDJ7u7.</p>