路径在javafx中拖动

时间:2014-12-18 05:01:53

标签: java javafx

我试图在javafx中为路径拖动创建一个事件,但是,使用setLayoutX/Y会在鼠标按下时将Path元素放在cursos下面,但拖动鼠标会有平滑拖动。我正在使用setTranslateX/Y方法,它不会将Path放在cursos下方,但拖动不平滑并使Path节点在拖动时向后和向前跳转。 Path被创建为一个自由对象。

我使用此代码创建路径:

EventHandler<MouseEvent> mouseEventHandler = new EventHandler<MouseEvent>() {
@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;
        }
    }
}

要从路径列表中拖动已创建的路径,请使用以下代码:

class PathEventHandler implements EventHandler<MouseEvent>{
//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");
        }
    }
    }
}

1 个答案:

答案 0 :(得分:1)

您必须将路径添加到群组,然后才能平滑拖动。 我不知道你为什么使用路径列表,但不需要进一步,因为我们有一个由对象扭曲的Path对象,你可以在那里对Path对象进行更改。如果由于某种原因仍然需要列表,则可以将Group对象收集到列表中,然后使用它来设置路径。

然后看看这个并做出这些改变

  if (e.getEventType() == MouseEvent.MOUSE_RELEASED) {
                    if (j == 1) {
                        paths.add(path);
                        root.getChildren().add(new DragablePath(path));
                        cachePath.add(path);
                         cacheType.add("Create");


                    }
                }

这是DragablePath类和DragContext类。您可以像使用“Path Event Handlers”一样使用“DragablePath事件处理程序”。

class DragablePath extends Group {

    private final Node node;

    DragablePath(Node node) {
        this.node = node;
        this.getChildren().add(node);
        final DragContext dragContext = new DragContext();
        this.addEventFilter(
                MouseEvent.ANY,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(final MouseEvent mouseEvent) {
                        mouseEvent.consume();

                    }
                });
       this.addEventFilter(
                MouseEvent.MOUSE_ENTERED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(final MouseEvent mouseEvent) {
                        node.setEffect(new DropShadow(20, Color.BLACK));

                    }
                });
        this.addEventFilter(
                MouseEvent.MOUSE_EXITED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(final MouseEvent mouseEvent) {
                        node.setEffect(null);

                    }
                });

        this.addEventFilter(
                MouseEvent.MOUSE_PRESSED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(final MouseEvent mouseEvent) {
                        dragContext.mouseAnchorX = mouseEvent.getX();
                        dragContext.mouseAnchorY = mouseEvent.getY();
                        dragContext.initialTranslateX = node.getTranslateX();
                        dragContext.initialTranslateY = node.getTranslateY();

                    }
                });
        this.addEventFilter(
                MouseEvent.MOUSE_DRAGGED,
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(final MouseEvent mouseEvent) {

                        node.setTranslateX(
                                dragContext.initialTranslateX
                                + mouseEvent.getX()
                                - dragContext.mouseAnchorX);
                        node.setTranslateY(
                                dragContext.initialTranslateY
                                + mouseEvent.getY()
                                - dragContext.mouseAnchorY);

                    }
                });
        this.addEventFilter(
                MouseEvent.MOUSE_RELEASED,
                new EventHandler<MouseEvent>() {

                    @Override
                    public void handle(final MouseEvent mouseEvent) {

                    }
                });

    }
}

private static final class DragContext {

    public double mouseAnchorX;
    public double mouseAnchorY;
    public double initialTranslateX;
    public double initialTranslateY;

}

**提示:您可以通过Shape的setSmooth(true)启用抗锯齿提示。