ConcurrentModification从stage和ArrayList中删除元素时的异常

时间:2018-05-29 14:23:42

标签: java javafx arraylist concurrentmodification

我正在尝试使用JavaFX动画。我在屏幕上的随机位置产生矩形,它们随着我定义的球移动。每当一个矩形离开屏幕的左边时,我会从场景中删除它,并从包含它的ArrayList中删除它。这是我的代码:

package UI;

import javafx.animation.AnimationTimer;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Random;

public class UIController extends AnimationTimer {

@FXML
private Label fpsLbl;

@FXML
private Circle moveMeCircle;

@FXML
private AnchorPane root;

private ArrayList<Rectangle> rectangles = new ArrayList<>();

int fps = 0;

int deltaX = 5;
int deltaY = 0;

long start = 0;

@FXML
public void initialize() {
    super.start();
}

@Override
public void handle(long l) {
    if (start == 0) {
        start = l;
    } else {
        if (l >= start + 1000000000) {
            fpsLbl.setText("FPS: " + ((l - start) / 1000000000) * fps);
            System.out.println(fps);
            renderShape();
            if (fps < 59) {
                System.out.println(rectangles.size());
            }
            fps = 0;
            start = l;
        }
    }
    updateElements();
    fps++;
}

private void renderShape() {
    try{
    for (Rectangle rectangle : rectangles) {
        if (rectangle.getX()+rectangle.getWidth() < 0) {//this is the part 
            rectangles.remove(rectangle);               //that causes the
            root.getChildren().remove(rectangle);       //exception

        }
    }}
    catch(ConcurrentModificationException e){
        e.printStackTrace();
        }
    Random rand = new Random();
    int posX = rand.nextInt(1280);
    int posY = rand.nextInt(720);
    rectangles.add(new Rectangle(posX, posY, 100, 50));


    for (Rectangle rect : rectangles) {
        if (!root.getChildren().contains(rect)) {
            rect.setWidth(rand.nextInt(300 + 650));
            root.getChildren().add(rect);
        }
    }
}

private void updateElements() {
    if ((moveMeCircle.getCenterX() >= 1280 && deltaX > 0) || (moveMeCircle.getCenterX() <= 0 && deltaX < 0)) {
        deltaX = deltaX * -1;
    }
    if ((moveMeCircle.getCenterY() <= 0 && deltaY < 0) || (moveMeCircle.getCenterY() >= 720 && deltaY > 0)) {
        deltaY = deltaY * -1;
    }
    for (Rectangle rect : rectangles) {
        rect.setX(rect.getX() + deltaX);
        rect.setY(rect.getY() + deltaY);
    }
    moveMeCircle.setCenterX(moveMeCircle.getCenterX() + deltaX);
    moveMeCircle.setCenterY(moveMeCircle.getCenterY() + deltaY);
}
}

虽然它确实抛出了一个例外,它仍然可以直观地删除&#34;元素,所以我的问题是,我怎样才能避免出现这种错误,是否有更好的方法可以用来实现我想要的结果?

感谢。

这是完整的StackTrace:

java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at UI.UIController.renderShape(UIController.java:61)
at UI.UIController.handle(UIController.java:47)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.lambda$handle$484(AnimationTimer.java:57)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:357)
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)

以及FXML文件,虽然它几乎无关紧要,但我只是定义了Anchorpane和正在移动的球:

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.control.Label?>
<AnchorPane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" 
minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0"
        prefWidth="1280.0" scaleShape="false" style="-fx-background-color: 
        #a4a4bd;"stylesheets="@css/main.css" 
        xmlns="http://javafx.com/javafx/10" 
        xmlns:fx="http://javafx.com/fxml/1"fx:controller="UI.UIController">
    <children>
        <Label fx:id="fpsLbl" prefWidth="200" AnchorPane.leftAnchor="0" 
        style="-fx-text-fill: blueviolet; -fx-font:20px 'Agency FB'"/>
        <Circle radius="10" centerX="50" centerY="50" fx:id="moveMeCircle"/>
    </children>
</AnchorPane>

0 个答案:

没有答案