title: Path dragging in javafx
body: <p>im trying to create an event for path dragging in javafx, however, using <code>setLayoutX/Y</code> will drop the <code>Path</code> element below the cursos at mouse pressed but will have a smooth dragging on mouse dragged. I am using <code>setTranslateX/Y</code> method and it does not drop <code>Path</code> below the cursos, however dragging is not smooth and makes <code>Path</code> node jump backwards and forward on dragging. <code>Path</code> is created as a freedraw object.</p>
  
  <p>I create a Path using this code:</p>
  
  <pre><code>EventHandler&lt;MouseEvent&gt; mouseEventHandler = new EventHandler&lt;MouseEvent&gt;() {
  @Override
  public void handle (MouseEvent e) {
      if (i == 0) {
      if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
          j = 1;
          path = new Path();
          path.setStroke(color);
          path.setStrokeWidth(10);
          root.getChildren().add(path);
          path.getElements().add(new MoveTo(e.getX(), e.getY()));
      }
      if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) {
          if (j == 1) {
          path.getElements().add(new LineTo(e.getX(), e.getY()));
          }
      }
      if (e.getEventType() == MouseEvent.MOUSE_RELEASED) {
          if (j == 1) {
          path.getElements().add(new LineTo(e.getX(), e.getY()));
          path.setOnMouseEntered(new PathEventHandler(paths.size()));
          path.setOnMouseExited(new PathEventHandler(paths.size()));
          path.setOnMousePressed(new PathEventHandler(paths.size()));
          path.setOnMouseDragged(new PathEventHandler(paths.size()));
          path.setOnMouseReleased(new PathEventHandler(paths.size()));
          path.setOnMouseClicked(new PathEventHandler(paths.size()));
          paths.add(path);
          cachePath.add(path);
          cacheType.add("Create");
          j = 0;
          }
      }
  }
  </code></pre>
  
  <p>For dragging created Path from paths list i use this code:</p>
  
  <pre><code>class PathEventHandler implements EventHandler&lt;MouseEvent&gt;{
  //element number in paths
  public int n;
  public PathEventHandler(int n){
      this.n=n;
  }
  @Override
  public void handle(MouseEvent me) {
      if (me.getEventType() == MouseEvent.MOUSE_ENTERED) {
          paths.get(n).setEffect(new DropShadow(20, Color.BLACK));
      }
      if (me.getEventType() == MouseEvent.MOUSE_EXITED) {
          paths.get(n).setEffect(null);
      }
      if (i == 2) {
      if (me.getEventType() == MouseEvent.MOUSE_PRESSED) {
          x = me.getX();
          y = me.getY();
      } 
      if (me.getEventType() == MouseEvent.MOUSE_DRAGGED) {
          paths.get(n).setTranslateX(me.getX() - x + paths.get(n).getTranslateX());
          paths.get(n).setTranslateY(me.getY() - y + paths.get(n).getTranslateY()); 
          /*paths.get(n).setTranslateX(me.getX());
          paths.get(n).setTranslateY(me.getY()); */
          listX.add(paths.get(n).getTranslateX());
          listY.add(paths.get(n).getTranslateY());
          x = me.getX();
          y = me.getY();
      }
      if (me.getEventType() == MouseEvent.MOUSE_RELEASED) {
          cachePath.add(paths.get(n));
          cacheType.add("Relocate");
      }
      if (me.getEventType() == MouseEvent.MOUSE_CLICKED) {
          if (me.getButton() == MouseButton.SECONDARY) {
          root.getChildren().remove(paths.get(n));
          cachePath.add(paths.get(n));
          cacheType.add("Remove");
          }
      }
      }
  }
  </code></pre>