停止形状重叠 - JavaFX

时间:2017-02-03 21:01:04

标签: javafx nodes collision-detection shapes overlapping

我有一个程序可以添加多个矩形,可以移动,调整大小,在场景中旋转。我想实现某种碰撞检测,这样如果碰撞,选定的形状就不会与现有形状重叠。

问题:

enter image description here

期望:

enter image description here

DraggingRectangleClass:

import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;

public class DraggingRectangle  {

public Group createDraggableRectangle(double x, double y, double width, double height) {
    final double handleRadius = 20 ;
    final double handleRadius2 = 30 ;

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

    // top left resize handle:
    Circle resizeHandleNW = new Circle(handleRadius, Color.RED);
    resizeHandleNW.setOpacity(0);
    // bind to top left cornerof Rectangle:
    resizeHandleNW.centerXProperty().bind(rect.xProperty());
    resizeHandleNW.centerYProperty().bind(rect.yProperty());
    resizeHandleNW.setStyle("-fx-cursor: NW_RESIZE; ");

    // bottom right resize handle:
    Circle resizeHandleSE = new Circle(handleRadius, Color.RED);
    resizeHandleSE.setOpacity(0);
    // bind to bottom right corner of Rectangle:
    resizeHandleSE.centerXProperty().bind(rect.xProperty().add(rect.widthProperty()));
    resizeHandleSE.centerYProperty().bind(rect.yProperty().add(rect.heightProperty()));
    resizeHandleSE.setStyle("-fx-cursor: SE_RESIZE; ");


    // move handle:
    Circle moveHandle = new Circle(handleRadius2,Color.RED);
    moveHandle.setOpacity(0);
    moveHandle.setStyle("-fx-cursor: CLOSED_HAND;");
    // bind to bottom center of Rectangle:
    moveHandle.centerXProperty().bind(rect.xProperty().add(rect.widthProperty().divide(2)));
    moveHandle.centerYProperty().bind(rect.yProperty().add(rect.heightProperty().divide(2)));


    Group group = new Group(rect, resizeHandleNW, resizeHandleSE, moveHandle);


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

    setUpDragging(resizeHandleNW, mouseLocation) ;
    setUpDragging(resizeHandleSE, mouseLocation) ;
    setUpDragging(moveHandle, 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());

        }
    });

    resizeHandleSE.setOnMouseDragged(event -> {
        if (mouseLocation.value != null) {
            double deltaX = event.getSceneX() - mouseLocation.value.getX();
            double deltaY = event.getSceneY() - mouseLocation.value.getY();
            double newMaxX = rect.getX() + rect.getWidth() + deltaX ;
            if (newMaxX >= rect.getX()
                    && newMaxX <= group.getParent().getBoundsInLocal().getWidth() - handleRadius) {
                rect.setWidth(rect.getWidth() + deltaX);
            }
            double newMaxY = rect.getY() + rect.getHeight() + deltaY ;
    if (newMaxY >= rect.getY()
            && newMaxY <= group.getParent().getBoundsInLocal().getHeight() - handleRadius) {
        rect.setHeight(rect.getHeight() + deltaY);
    }
    mouseLocation.value = new Point2D(event.getSceneX(), event.getSceneY());
}
});

    moveHandle.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 ;
            double newMaxX = newX + rect.getWidth();
            if (newX >= handleRadius
                    && newMaxX <=group.getParent().getBoundsInLocal().getWidth() - handleRadius) {
                rect.setX(newX);
            }
            double newY = rect.getY() + deltaY ;
            double newMaxY = newY + rect.getHeight();
            if (newY >= handleRadius
                    && newMaxY <=group.getParent().getBoundsInLocal().getHeight() - handleRadius) {
                rect.setY(newY);
            }
            mouseLocation.value = new Point2D(event.getSceneX(), event.getSceneY());
        }

    });


    return group ;

}

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 ; }


}

MethodToCreateRectangle:

 if (treeview.getSelectionModel().getSelectedItem() == green) {
                Group group = drag.createDraggableRectangle(200, 60, 180, 90);
                group.getChildren().get(0).setStyle(
                        "-fx-fill: green;");                             

                container2.getChildren().addAll(group);
                group.setOnMouseClicked(e -> onShapeSelected(e));
}

&#34; Group&#34;包含我的形状以及要移动和调整大小的手柄。 &#34;容器2&#34;是我的AnchorPane的fx:id。

由于

0 个答案:

没有答案