如何在JavaFX中旋转的矩形上相对“粘贴”形状?

时间:2016-05-10 15:44:46

标签: javafx rotation

正如你可以看到的那样,我可以通过红色锚点旋转我的矩形形状。 我希望所有锚点在旋转时(相当于当然之后)相对于矩形形状粘贴,而不是在第二个屏幕截图中看到的原始位置。

Not rotated shape

Rotated Shape

private Rectangle createDraggableRectangle(double x, double y, double width, double height) {

    final double handleRadius = 5;

    Rectangle rect = new Rectangle(x, y, width, height);

    // top left resize handle:
    Circle resizeHandleNW = new Circle(handleRadius, Color.GOLD);
    // bind to top left corner of Rectangle:
    resizeHandleNW.centerXProperty().bind(rect.xProperty());
    resizeHandleNW.centerYProperty().bind(rect.yProperty());

    // top right rotate handle:
    Circle rotateHandle = new Circle(handleRadius, Color.RED);
    // bind to top right corner of Rectangle:
    rotateHandle.centerXProperty().bind(rect.xProperty().add(rect.widthProperty()));
    rotateHandle.centerYProperty().bind(rect.yProperty());


    // force circles to live in same parent as rectangle:
    rect.parentProperty().addListener((obs, oldParent, newParent) -> {
        for (Circle c : Arrays.asList(resizeHandleNW, resizeHandleSE, resizeHandleLeft, resizeHandleRight, resizeHandleTop, resizeHandleBottom, rotateHandle, moveHandle)) {
            Pane currentParent = (Pane) c.getParent();
            if (currentParent != null) {
                currentParent.getChildren().remove(c);
            }
            ((Pane) newParent).getChildren().add(c);
        }
    });

    Wrapper<Point2D> mouseLocation = new Wrapper<>();

    setUpDragging(resizeHandleNW, mouseLocation);

    resizeHandleNW.setOnMouseDragged(event -> {
        if (mouseLocation.value != null) {
            double deltaX = event.getSceneX() - mouseLocation.value.getX();
            double deltaY = event.getSceneY() - mouseLocation.value.getY();
            double newX = rect.getX() + deltaX;
            if (newX >= handleRadius
                    && newX <= rect.getX() + rect.getWidth() - handleRadius) {
                rect.setX(newX);
                rect.setWidth(rect.getWidth() - deltaX);
            }
            double newY = rect.getY() + deltaY;
            if (newY >= handleRadius
                    && newY <= rect.getY() + rect.getHeight() - handleRadius) {
                rect.setY(newY);
                rect.setHeight(rect.getHeight() - deltaY);
            }
            mouseLocation.value = new Point2D(event.getSceneX(), event.getSceneY());
        }
    });

    rotateHandle.setOnMouseDragged(event -> {
        if (mouseLocation.value != null) {
            double deltaX = event.getSceneX() - mouseLocation.value.getX();
            double deltaY = event.getSceneY() - mouseLocation.value.getY();
            double radAngle = Math.atan2(deltaY, deltaX);
            double degAngle = radAngle * 180 / Math.PI;
            rect.setRotate(degAngle);
        }
    });
    return rect;
}

private void setUpDragging(Circle circle, Wrapper<Point2D> mouseLocation) {

    circle.setOnDragDetected(event -> {
        circle.getParent().setCursor(Cursor.CLOSED_HAND);
        mouseLocation.value = new Point2D(event.getSceneX(), event.getSceneY());
    });

    circle.setOnMouseReleased(event -> {
        circle.getParent().setCursor(Cursor.DEFAULT);
        mouseLocation.value = null;
    });
}

static class Wrapper<T> {

    T value;
}

0 个答案:

没有答案