JavaFX-形状删除但锚点不删除

信仰者12

在我的程序中,我有两个可拖动和可调整大小的矩形。我添加了一个功能,使我可以在两者之间进行选择,然后根据选择和单击按钮删除其中之一。

删除之前: 在此处输入图片说明

删除后:

在此处输入图片说明

代码:

//Fields

private DraggingRectangle drag = new DraggingRectangle();
private List<Node> selectedShapes = new ArrayList<>();
@FXML
private AnchorPane container, container2;

//CreatingTheRectangle

if (treeview.getSelectionModel().getSelectedItem() == blackrect) {
  Rectangle rect = drag.createDraggableRectangle(200, 60, 200, 60);
  GraphicsContext gc = canvas.getGraphicsContext2D();            
  rect.setFill(Color.BLACK);
  container2.getChildren().addAll(rect);
  rect.setOnMouseClicked(e -> onShapeSelected(e));
}

//SelectingMethod

public void onShapeSelected(MouseEvent e) {
    Shape shape = (Shape) e.getSource();
    shape.setStyle("-fx-effect: dropshadow(three-pass-box, red, 10, 10, 0,           0);");
    if (!selectedShapes.contains(shape)) {
        selectedShapes.add(shape);
        System.out.println("Shape Selected:" + shape);
    } else {
        shape.setStyle("-fx-effect: null");
        selectedShapes.remove(shape);
    }

//DeletingMethod

 public void deleteButton(ActionEvent e) {
    deletebutton.setOnAction(a ->   container2.getChildren().removeAll(selectedShapes));
}

DraggableRectangle类:

public class DraggingRectangle  {

public Rectangle 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);
    // bind to top left corner of Rectangle:
    resizeHandleNW.centerXProperty().bind(rect.xProperty());
    resizeHandleNW.centerYProperty().bind(rect.yProperty());

    // bottom right resize handle:
    Circle resizeHandleSE = new Circle(handleRadius, Color.RED);
    // bind to bottom right corner of Rectangle:
    resizeHandleSE.centerXProperty().bind(rect.xProperty().add(rect.widthProperty()));
    resizeHandleSE.centerYProperty().bind(rect.yProperty().add(rect.heightProperty()));

    // move handle:
    Circle moveHandle = new Circle(handleRadius2,Color.RED);
    moveHandle.setOpacity(0.0);
    // 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)));

    // force circles to live in same parent as rectangle:
    rect.parentProperty().addListener((obs, oldParent, newParent) -> {
        for (Circle c : Arrays.asList(resizeHandleNW, resizeHandleSE, 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) ;
    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 <= rect.getParent().getBoundsInLocal().getWidth() - handleRadius) {
                rect.setWidth(rect.getWidth() + deltaX);
            }
            double newMaxY = rect.getY() + rect.getHeight() + deltaY ;
            if (newMaxY >= rect.getY()
                    && newMaxY <= rect.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 <= rect.getParent().getBoundsInLocal().getWidth() - handleRadius) {
                rect.setX(newX);
            }
            double newY = rect.getY() + deltaY ;
            double newMaxY = newY + rect.getHeight();
            if (newY >= handleRadius
                    && newMaxY <= rect.getParent().getBoundsInLocal().getHeight() - handleRadius) {
                rect.setY(newY);
            }
            mouseLocation.value = new Point2D(event.getSceneX(), event.getSceneY());
        }

    });

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


}

我意识到它也与我要添加矩形的列表有关,但是无法弄清楚如何将锚点与之一起添加。谢谢你

例外:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = AnchorPane[id=container2]
at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:329)
at com.sun.javafx.collections.VetoableListDecorator.remove(VetoableListDecorator.java:221)
at Model.DraggingRectangle.lambda$createDraggableRectangle$0(DraggingRectangle.java:43)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.Node.setParent(Node.java:720)
at javafx.scene.Parent$2.onProposedChange(Parent.java:497)
at com.sun.javafx.collections.VetoableListDecorator.removeFromList(VetoableListDecorator.java:149)
at com.sun.javafx.collections.VetoableListDecorator.removeAll(VetoableListDecorator.java:264)
at Controller.NewLayoutController.lambda$deleteButton$9(NewLayoutController.java:261)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

多边形

 public ObservableList<Anchor> createAnchorsPoly(Polygon polygon, final   ObservableList<Double> points) {}

polygon.parentProperty().addListener((obs, oldParent, newParent) -> {
            Platform.runLater(() -> {
                for (ObservableList<Double> a : Arrays.asList(points)) {
                    Pane currentParent = (Pane)oldParent;
                    if (currentParent != null) {
                        currentParent.getChildren().remove(a);
                    }
                    if (newParent != null) {
                        ((Pane)newParent).getChildren().add(a); <<Error!
                    }
                }
            });
詹姆斯_D

代替

rect.parentProperty().addListener((obs, oldParent, newParent) -> {
    for (Circle c : Arrays.asList(resizeHandleNW, resizeHandleSE, moveHandle)) {
        Pane currentParent = (Pane)c.getParent();
        if (currentParent != null) {
            currentParent.getChildren().remove(c);
        }
        ((Pane)newParent).getChildren().add(c);
    }
});

rect.parentProperty().addListener((obs, oldParent, newParent) -> {
    Platform.runLater(() -> {
        for (Circle c : Arrays.asList(resizeHandleNW, resizeHandleSE, moveHandle)) {
            Pane currentParent = (Pane)oldParent;
            if (currentParent != null) {
                currentParent.getChildren().remove(c);
            }
            if (newParent != null) {
                ((Pane)newParent).getChildren().add(c);
            }
        }
    });
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从JavaFX输入字段中删除蓝框

来自分类Dev

从JavaFX输入字段中删除蓝框

来自分类Dev

JavaFX ToolTip删除页面角

来自分类Dev

删除JavaFX 2 LineChart图例项目

来自分类Dev

如何使用CSS在JavaFX2中设置表格单元格的样式(不删除悬停/选择等)

来自分类Dev

如何删除JavaFx TableView行

来自分类Dev

JavaFX TitledPane:删除标题

来自分类Dev

JavaFX。无法删除TabPane右侧的多余空间

来自分类Dev

JavaFX TableView删除问题

来自分类Dev

JavaFX ListView选择-如何删除选择边框

来自分类Dev

Javafx从htmleditor删除节点

来自分类Dev

删除JavaFX按钮填充?

来自分类Dev

使用Delete键删除JavaFX表行

来自分类Dev

从Webengine JavaFX删除特定的Cookie

来自分类Dev

删除JavaFX menuButton上的箭头

来自分类Dev

JavaFX从AnchorPane删除对象

来自分类Dev

从JavaFX标签中删除填充/边距

来自分类Dev

如何从表列javafx中删除行

来自分类Dev

从网址中删除锚点

来自分类Dev

JavaFx:删除项目时出错

来自分类Dev

JavaFX的从TableView中删除

来自分类Dev

如何使用CSS在JavaFX2中设置表格单元格的样式(不删除悬停/选择等)

来自分类Dev

JavaFX TitledPane:删除标题

来自分类Dev

Javafx从htmleditor删除节点

来自分类Dev

JavaFX Imageview图像删除

来自分类Dev

添加和删除锚点属性

来自分类Dev

从网址中删除锚点

来自分类Dev

删除 JavaFx 微调箭头

来自分类Dev

JavaFX | 改变线锚?