在具有时间轴关键帧的线程“JavaFX应用程序线程”重复子项中获取异常

时间:2016-05-07 01:42:41

标签: java javafx

我理解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)

1 个答案:

答案 0 :(得分:1)

该行

getChildren().addAll(sine, circle);

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