gpt4 book ai didi

java - 在线程 "JavaFX Application Thread"中获取异常,并具有时间线关键帧的重复子项

转载 作者:行者123 更新时间:2023-12-01 09:57:25 24 4
gpt4 key购买 nike

我知道 Polyline sine = new Polyline(); 导致了错误,因为它试图为已经存在的东西创建一个新的类实例。如何清除每个关键帧的实例或在方法外部声明折线而不创建对角线及其创建的正弦波? My Full Code

public BallOnCurvePane() {
// Create an animation for moving the ball
animation = new Timeline(
new KeyFrame(Duration.millis(50), e -> moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
}

protected void moveBall() {
Polyline sine = new Polyline();
ObservableList<Double> list = sine.getPoints();
for (double x = -170.0; x <= 170.0; x++) {
list.add(x + 200.0);
list.add(80.0 - 50.0 * Math.sin((x / 100.0) * 2.0 * (double) Math.PI));
}
circle.setFill(ballColor);
getChildren().addAll(sine, circle);

pt.setRate(.25);
pt.setPath(sine);
pt.setNode(circle);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play();

}

我的完整错误

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = BallOnCurvePane@13424ecf
at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
at BallOnCurvePane.moveBall(BallOnCurvePane.java:68)
at BallOnCurvePane.lambda$new$0(BallOnCurvePane.java:25)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:239)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:180)
at javafx.animation.Timeline.impl_playTo(Timeline.java:176)
at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(InfiniteClipEnvelope.java:110)
at javafx.animation.Animation.impl_timePulse(Animation.java:1102)
at javafx.animation.Animation$1.lambda$timePulse$25(Animation.java:186)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.Animation$1.timePulse(Animation.java:185)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

最佳答案

线路

getChildren().addAll(sine, circle);

每次添加相同的实例。无需多次添加节点。此外,由于折线仅代表球应采取的路径,因此无需将其添加为子项(除非您想绘制路径并沿其移动圆)。

关于java - 在线程 "JavaFX Application Thread"中获取异常,并具有时间线关键帧的重复子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083634/

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