gpt4 book ai didi

javafx - 在 JavaFX 中重置场景图时出现问题

转载 作者:行者123 更新时间:2023-12-05 05:47:45 27 4
gpt4 key购买 nike

我遇到了快速变化的大型场景图的问题。

在响应事件时,当我清除场景图 (getChildren().clear) 时,有时会出现此异常:

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: class javafx.scene.Scene cannot be cast to class javafx.scene.Node (javafx.scene.Scene and javafx.scene.Node are in module javafx.graphics of loader 'app')
at javafx.graphics/javafx.scene.Scene$MouseHandler.handleNodeRemoval(Scene.java:3709)
at javafx.graphics/javafx.scene.Scene.generateMouseExited(Scene.java:3581)
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:593)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.clear(VetoableListDecorator.java:294)
at bit.fxzoomer/bit.fxzoomer.SectorPane12.populateMap(SectorPane12.java:72)
at bit.fxzoomer/bit.fxzoomer.SectorPane12.lambda$addListeners$1(SectorPane12.java:247)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:106)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.base/javafx.beans.property.ObjectProperty.setValue(ObjectProperty.java:72)
at bit.fxzoomer/bit.fxzoomer.SectorPane12.setViewport(SectorPane12.java:62)
at bit.fxzoomer/bit.fxzoomer.App.lambda$start$0(App.java:31)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:106)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.base/javafx.beans.property.ObjectProperty.setValue(ObjectProperty.java:72)
at bit.fxzoomer/bit.fxzoomer.PanZoomPane.setViewport(PanZoomPane.java:99)
at bit.fxzoomer/bit.fxzoomer.PanZoomPane.lambda$new$3(PanZoomPane.java:82)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.binding.ObjectBinding.invalidate(ObjectBinding.java:170)
at javafx.base/com.sun.javafx.binding.BindingHelperObserver.invalidated(BindingHelperObserver.java:52)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:136)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.graphics/javafx.scene.Node$LazyBoundsProperty.invalidate(Node.java:9785)
at javafx.graphics/javafx.scene.Node$MiscProperties.invalidateBoundsInLocal(Node.java:6876)
at javafx.graphics/javafx.scene.Node.invalidateBoundsInLocal(Node.java:3469)
at javafx.graphics/javafx.scene.Node.localBoundsChanged(Node.java:4041)
at javafx.graphics/javafx.scene.Node.doGeomChanged(Node.java:4028)
at javafx.graphics/javafx.scene.Node$1.doGeomChanged(Node.java:461)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.geomChangedImpl(NodeHelper.java:184)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.geomChanged(NodeHelper.java:137)
at javafx.graphics/javafx.scene.Parent.childBoundsChanged(Parent.java:1872)
at javafx.graphics/javafx.scene.Node.notifyParentOfBoundsChange(Node.java:4099)
at javafx.graphics/javafx.scene.Node.transformedBoundsChanged(Node.java:4060)
at javafx.graphics/javafx.scene.Node.doTransformsChanged(Node.java:5003)
at javafx.graphics/javafx.scene.Node$1.doTransformsChanged(Node.java:444)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.transformsChangedImpl(NodeHelper.java:170)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.transformsChanged(NodeHelper.java:119)
at javafx.graphics/javafx.scene.transform.Transform.transformChanged(Transform.java:2109)
at javafx.graphics/javafx.scene.transform.Affine$AffineAtomicChange.end(Affine.java:5778)
at javafx.graphics/javafx.scene.transform.Affine.appendTranslation(Affine.java:2038)
at javafx.graphics/javafx.scene.transform.Translate.appendTo(Translate.java:539)
at javafx.graphics/javafx.scene.transform.Affine.append(Affine.java:1502)
at bit.fxzoomer/bit.fxzoomer.PanZoomPane.translate(PanZoomPane.java:123)
at bit.fxzoomer/bit.fxzoomer.PanZoomPane.lambda$new$1(PanZoomPane.java:58)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)

它从那个异常开始,然后我得到这些的级联:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "javafx.scene.Scene.isDepthBuffer()" because the return value of "javafx.scene.Node.getScene()" is null
at javafx.graphics/com.sun.javafx.scene.input.PickResultChooser.processOffer(PickResultChooser.java:185)
at javafx.graphics/com.sun.javafx.scene.input.PickResultChooser.offer(PickResultChooser.java:143)
at javafx.graphics/javafx.scene.Node.doComputeIntersects(Node.java:5263)
at javafx.graphics/javafx.scene.Node$1.doComputeIntersects(Node.java:456)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeIntersectsImpl(NodeHelper.java:180)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeIntersects(NodeHelper.java:133)
at javafx.graphics/javafx.scene.Node.intersects(Node.java:5234)
at javafx.graphics/javafx.scene.Node.doPickNodeLocal(Node.java:5171)
at javafx.graphics/javafx.scene.Node$1.doPickNodeLocal(Node.java:450)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocalImpl(NodeHelper.java:175)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(NodeHelper.java:128)
at javafx.graphics/javafx.scene.Node.pickNode(Node.java:5203)
at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Parent.java:805)
at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Parent.java:136)
at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(ParentHelper.java:113)
at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Region.java:3160)
at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Region.java:184)
at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(RegionHelper.java:104)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(NodeHelper.java:128)
at javafx.graphics/javafx.scene.Node.pickNode(Node.java:5203)
at javafx.graphics/javafx.scene.Parent.pickChildrenNode(Parent.java:805)
at javafx.graphics/javafx.scene.Parent$1.pickChildrenNode(Parent.java:136)
at javafx.graphics/com.sun.javafx.scene.ParentHelper.pickChildrenNode(ParentHelper.java:113)
at javafx.graphics/javafx.scene.layout.Region.doPickNodeLocal(Region.java:3160)
at javafx.graphics/javafx.scene.layout.Region$1.doPickNodeLocal(Region.java:184)
at javafx.graphics/com.sun.javafx.scene.layout.RegionHelper.pickNodeLocalImpl(RegionHelper.java:104)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.pickNodeLocal(NodeHelper.java:128)
at javafx.graphics/javafx.scene.Node.pickNode(Node.java:5203)
at javafx.graphics/javafx.scene.Scene$MouseHandler.pickNode(Scene.java:4005)
at javafx.graphics/javafx.scene.Scene.pick(Scene.java:2029)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3815)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)

具体来说,我正在平移场景图(请在此处查看答案 Get Viewport of translated and scaled node )。每次视口(viewport)改变时,我都会重做场景图。

场景图相当大,有 20-30,000 个节点。

当事情很忙时,当我快速拖动场景,点击很多,不经常,我会得到这些异常。

这是单线程的,所以我认为我没有看到同步问题,但也许我正在与重新显示线程作斗争。显然有些事情在错误的时间发生。

有时它会从这些错误中恢复(它会发出很多无关紧要的噪音),有时它似乎破坏了场景并且不再起作用。

我曾尝试通过计时器延迟更新,该计时器在拖动时不断刷新,仅在拖动停止后触发(通过 Platform.runLater),但即使这样也可能触发此异常。我将其全部放回主线程以排除同步问题。

这是非常零星的。我不知道什么在打什么。

是什么原因造成的,我该如何预防?

编辑:这是一个失败的独立示例。它甚至没有大的场景图。只需运行该程序,捕获左下角并来回调整大小,它就会失败。

使用 JDK 11 和 JFX 17.0.2 测试。

我尝试了一个大型场景图(25,000 个元素),简单地绑定(bind)到一个调整大小事件,并且无法让它失败。这很容易失败。

package bit.fxtest2;

import javafx.application.Application;
import javafx.beans.binding.Binding;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Affine;
import javafx.scene.transform.Transform;
import javafx.stage.Stage;

public class TransformTest9 extends Application {

@Override
public void start(Stage primaryStage) throws Exception {

BigGridPane gridPane = new BigGridPane();
PanZoomPane pzPane = new PanZoomPane(gridPane);
pzPane.getViewportProperty().addListener((ov, t, t1) -> {
gridPane.setViewPort(t1);
});

Scene scene = new Scene(pzPane, 250, 250);
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}

class BigGridPane extends Region {

ObjectProperty<Bounds> viewPortProperty = new SimpleObjectProperty<>();

public BigGridPane() {
viewPortProperty.addListener((ov, t, t1) -> {
populate();
});
}

public void setViewPort(Bounds viewPort) {
viewPortProperty.setValue(viewPort);
}

public void populate() {
ObservableList<Node> children = getChildren();
children.clear();
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
Rectangle r = new Rectangle(i * 20, j * 20, 20, 20);
r.setFill(Color.WHITE);
r.setStroke(Color.BLACK);
children.add(r);
}
}
System.out.println(children.size());
}
}

class PanZoomPane extends Region {

private final Node content;

private final Rectangle clip;

private Affine transform;

private Point2D mouseDown;

private static final double SCALE = 1.01; // zoom factor per pixel scrolled

Binding<Bounds> viewportBinding;
ObjectProperty<Bounds> viewportProperty = new SimpleObjectProperty<>();

public PanZoomPane(Node content) {
Background background = new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY));
setBackground(background);
this.content = content;
getChildren().add(content);
clip = new Rectangle();
setClip(clip);
transform = Affine.affine(1, 0, 0, 1, 0, 0);
content.getTransforms().setAll(transform);

content.setOnMousePressed(event -> mouseDown = new Point2D(event.getX(), event.getY()));
content.setOnMouseDragged(event -> {
double deltaX = event.getX() - mouseDown.getX();
double deltaY = event.getY() - mouseDown.getY();
translate(deltaX, deltaY);
});
content.setOnScroll(event -> {
double pivotX = event.getX();
double pivotY = event.getY();
double scale = Math.pow(SCALE, event.getDeltaY());
scale(pivotX, pivotY, scale);
});

viewportBinding = new ObjectBinding<>() {
{
bind(
localToSceneTransformProperty(),
boundsInLocalProperty(),
content.localToSceneTransformProperty()
);
}

@Override
protected Bounds computeValue() {
return content.sceneToLocal(localToScene(getBoundsInLocal()));
}
};

viewportBinding.addListener((obs, oldViewport, newViewport) -> setViewport(newViewport));

}

public ObjectProperty<Bounds> getViewportProperty() {
return viewportProperty;
}

public void setViewportProperty(ObjectProperty<Bounds> viewportProperty) {
this.viewportProperty = viewportProperty;
}

public Bounds getViewport() {
return viewportProperty.getValue();
}

public void setViewport(Bounds bounds) {
viewportProperty.setValue(bounds);
}

public Node getContent() {
return content;
}

@Override
protected void layoutChildren() {
clip.setWidth(getWidth());
clip.setHeight(getHeight());
}

public void scale(double pivotX, double pivotY, double scale) {
Affine t = transform.clone();
t.append(Transform.scale(scale, scale, pivotX, pivotY));
}

public void translate(double x, double y) {
transform.append(Transform.translate(x, y));
}
}
}

编辑:

我想我有一个解决方案。我已经在下面发布了。

最佳答案

我想这就是答案。

编辑:不,不是。仍然有问题。也许我每次都会销毁并重新创建 Pane - 似乎过分了。

@kleopatra 提到了在 `PanZoomPane 中调用 super.layoutChildren。虽然这适用于测试,但不适用于我的代码。

@jewelsea 向我指出了这个 post它有一个重要的假设:

On the third hand, we expect people to set up scene graphs atinitialization time and modify nodes in-place, instead of doing scenegraph surgery.

这显然不是我在做什么。我不仅在进行场景图形手术,而且可以说是彻底的屠宰。

我之前从@James_D 那里看到过关于如何在 layoutChildren 中而不是事件处理程序中更好地完成某些工作的提及。所以也许 layoutChildren 是我们应该寻找的地方。

PanZoomPane.layoutChildren 被调用时,它不可避免地会调用我的自定义 Pane 的 layoutChildren

PanZoomPane 调整大小时,调用 layoutChildren。但是当它被平移或缩放时,它不会。为了解决这个问题,在 scaletranslate 方法中,我调整了 Pane 的高度:

double height = getHeight();
setHeight(height + 1);
setHeight(height);

这足以触发 layoutChildren 调用。显然 layoutChildren 是可以接受场景图手术的地方。因此,我将所有场景图代码移至 layoutChildren

可以说我应该将 tickle 移到我的内容 Pane 中,而不是从 PanZoomPane 强制它。

但它的核心是我猜场景图手术在事件处理程序中是不受欢迎的。我们应该只使用现有场景图的属性。

我不知道是否有更好的解决方案,但就目前而言,它似乎有效并且似乎有点道理。

关于javafx - 在 JavaFX 中重置场景图时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70920125/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com