锚点不遵循多边形 - JavaFX

Anchor Points Do Not Follow Polygon - JavaFX

目前我有一个可以根据需要调整大小和形状的多边形。问题是当我移动多边形时,锚点保持固定在它们的位置并且不随多边形移动,我真的很困惑,希望有人能帮助或指导我。

public Polygon createStartingFloorPlan(ActionEvent event) throws IOException {
    Polygon fp = new Polygon();
    ObjectProperty<Point2D> mousePosition = new SimpleObjectProperty<>();
    fp.getPoints().setAll(
            150d, 50d,
            450d, 50d,
            750d, 50d,
            750d, 350d,
            750d, 650d,
            450d, 650d,
            150d, 650d,
            150d, 350d

    );

    fp.setOnMousePressed(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            mousePosition.set(new Point2D(event.getSceneX(), event.getSceneY()));
        }
    });

    fp.setOnMouseDragged(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            double deltaX = event.getSceneX() - mousePosition.get().getX();
            double deltaY = event.getSceneY() - mousePosition.get().getY();
            fp.setLayoutX(fp.getLayoutX()+deltaX);
            fp.setLayoutY(fp.getLayoutY()+deltaY);
            mousePosition.set(new Point2D(event.getSceneX(), event.getSceneY()));
            createControlAnchorsFor(fp.getPoints());
        }
    });

    fp.setStroke(Color.FORESTGREEN);
    fp.setStrokeWidth(4);
    fp.setStrokeLineCap(StrokeLineCap.ROUND);
    fp.setFill(Color.CORNSILK.deriveColor(0, 1.2, 1, 0.6));
    container.getChildren().add(fp);
    container.getChildren().addAll(createControlAnchorsFor(fp.getPoints()));
    return fp;
}


private ObservableList<Anchor> createControlAnchorsFor(final ObservableList<Double> points) {
    ObservableList<Anchor> anchors = FXCollections.observableArrayList();

    for (int i = 0; i < points.size(); i += 2) {
        final int idx = i;

        DoubleProperty xProperty = new SimpleDoubleProperty(points.get(i));
        DoubleProperty yProperty = new SimpleDoubleProperty(points.get(i + 1));

        xProperty.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> ov, Number oldX, Number x) {
                points.set(idx, (double) x);
            }
        });

        yProperty.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> ov, Number oldY, Number y) {
                points.set(idx + 1, (double) y);
            }
        });
        anchors.add(new Anchor(Color.GOLD, xProperty, yProperty));

    }

    return anchors;
}

// a draggable anchor displayed around a point.
class Anchor extends Circle {
    private final DoubleProperty x, y;

    Anchor(Color color, DoubleProperty x, DoubleProperty y) {
        super(x.get(), y.get(), 10);
        setFill(color.deriveColor(1, 1, 1, 0.5));
        setStroke(color);
        setStrokeWidth(2);
        setStrokeType(StrokeType.OUTSIDE);

        this.x = x;
        this.y = y;

        x.bind(centerXProperty());
        y.bind(centerYProperty());
        enableDrag();
    }

    // make a node movable by dragging it around with the mouse.
    private void enableDrag() {
        final Delta dragDelta = new Delta();
        setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                // record a delta distance for the drag and drop operation.
                dragDelta.x = getCenterX() - mouseEvent.getX();
                dragDelta.y = getCenterY() - mouseEvent.getY();
                getScene().setCursor(Cursor.MOVE);
            }
        });
        setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                getScene().setCursor(Cursor.HAND);
            }
        });
        setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                double newX = mouseEvent.getX() + dragDelta.x;
                if (newX > 0 && newX < getScene().getWidth()) {
                    setCenterX(newX);
                }
                double newY = mouseEvent.getY() + dragDelta.y;
                if (newY > 0 && newY < getScene().getHeight()) {
                    setCenterY(newY);
                }
            }
        });
        setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (!mouseEvent.isPrimaryButtonDown()) {
                    getScene().setCursor(Cursor.HAND);
                }
            }
        });
        setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (!mouseEvent.isPrimaryButtonDown()) {
                    getScene().setCursor(Cursor.DEFAULT);
                }
            }
        });
    }

    // records relative x and y co-ordinates.
    private class Delta {
        double x, y;
    }

}

问题

绘制 Polygon 点的坐标是 (layoutX + pointX, layoutY + pointY) 假设没有对节点应用变换。

您永远不会调整 AnchorlayoutXlayoutY 属性。要解决此问题,您可以将它们绑定到 Polygon:

的属性
private ObservableList<Anchor> createControlAnchorsFor(Polygon polygon, final ObservableList<Double> points) {
    ...

    Anchor anchor = new Anchor(Color.GOLD, xProperty, yProperty);

    anchor.layoutXProperty().bind(polygon.layoutXProperty());
    anchor.layoutYProperty().bind(polygon.layoutYProperty());

    anchors.add(anchor);

    ...

}

此外,我建议使用自定义 DoubleProperty class 而不是对侦听器使用 SimpleDoublePropertys,因为这样可以使代码更具可读性并减少每个坐标一个对象:

private static class ListWriteDoubleProperty extends SimpleDoubleProperty {
    private final ObservableList<Double> list;
    private final int index;

    public ListWriteDoubleProperty(ObservableList<Double> list, int index) {
        super(list.get(index));
        this.list = list;
        this.index = index;
    }

    @Override
    protected void invalidated() {
        list.set(index, get());
    }

}
DoubleProperty xProperty = new ListWriteDoubleProperty(points, i);
DoubleProperty yProperty = new ListWriteDoubleProperty(points, i + 1);
// no more listeners/final index copy required...