如何在javafx中获取Path中心的坐标?

时间:2014-10-30 20:35:12

标签: java javafx

我已获得Path元素,手动绘制并保存为Node。我无法确定如何获取整个节点的中心坐标,因此如果使用Path方法重新定位relocate()并稍后决定将其放回原位,则不会返回到它的位置。初始位置。我尝试过使用getLayoutX()getLayoutY()方法,但它会将我的节点从原始位置进一步向上移动。

我用于重新定位的代码

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_DRAGGED) {
        listX.add(paths.get(n).getLayoutX());
        listY.add(paths.get(n).getLayoutY());
        paths.get(n).relocate(me.getSceneX(), me.getSceneY());
        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)

根据您以前的question,以及您在此篇中几乎没有描述过的内容,我试图弄清楚您要完成的任务。

也许它离那里很远,但是这个简短但功能强大的代码应该足以让你理解如何处理路径创建(如第一个问题)和路径移动(对于实际问题),所有这些都在同一只鼠标中监听器。

private Path path;
private double x1, y1;

@Override
public void start(Stage primaryStage) {

    AnchorPane root = new AnchorPane();

    root.addEventHandler(MouseEvent.ANY, e -> {
        if(e.getTarget() instanceof Path){
            // Select existing path
            Path path1 = (Path)e.getTarget();

            if (e.getEventType() == MouseEvent.MOUSE_ENTERED_TARGET) {
                path1.setEffect(new DropShadow(20, Color.BLACK));
            } else if (e.getEventType() == MouseEvent.MOUSE_EXITED_TARGET) {
                path1.setEffect(null);
            } else if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
                x1=e.getX(); 
                y1=e.getY();
            } else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                // traslate path
                path1.setTranslateX(e.getX()-x1+path1.getTranslateX());
                path1.setTranslateY(e.getY()-y1+path1.getTranslateY());
                x1=e.getX(); 
                y1=e.getY();
            } else if (e.getButton()==MouseButton.SECONDARY) {
                // right-click over the path to move it to its original position
                path1.setTranslateX(0);
                path1.setTranslateY(0);
            }
        } else {
            // Generate new path
            if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
                path = new Path();
                path.setStroke(Color.BLACK);
                path.setStrokeWidth(10);
                path.getElements().add(new MoveTo(e.getX(), e.getY()));
                root.getChildren().add(path);
            } else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED || 
                e.getEventType() == MouseEvent.MOUSE_RELEASED) {
                path.getElements().add(new LineTo(e.getX(), e.getY()));
            }
        }
    });

    Scene scene = new Scene(root, 600, 400);

    primaryStage.setScene(scene);
    primaryStage.show();
}