ArrayList中的java.util.ConcurrentModificationException;尝试在绘画时添加新的对象

时间:2015-12-21 20:56:59

标签: java arraylist paint

我做了一个小游戏。现在我想每10毫秒向玩家添加一些新粒子:

    TimerTask task2 = new TimerTask() {
        @Override
        public void run() {
            if(!_panel.drawing())
            new HeatParticle(_panel, _player);
        }
    };
    timer.schedule(task2, 10, 10);

粒子将自己添加到GamePanel:

public HeatParticle(GamePanel panel, GameCircle _player) {
    _panel = panel;
    _particle = new GameCircle(_player.x() + _r.nextInt(10)  - 5  + _player.size() / 2, _player.y() + _r.nextInt(10)  - 5 + _player.size() / 2, 5, "draw", Color.RED);

    _panel.add(_particle);
    startTimer();
}

GamePanel将所有Circles存储在ArrayList中:

private ArrayList<GameCircle> circles = new ArrayList<GameCircle>();
[...]
    public void add(GameCircle circle) {
    if (!_drawing) {
        circles.add(circle);
        circle.setIndex(circles.size() - 1);
        repaint();
    }
}

要绘制所有这些,它用于:

private void draw() {
_drawing = true;
 [...]
for (GameCircle circle : circles) {
        if (circle != null)
            if (circle.drawOrFill() != null)
                if (circle.drawOrFill().equals("fill") && circle.draw()) {
                    _canvas.setColor(circle.color());
                    _canvas.fillOval(circle.x(), circle.y(), circle.size(), circle.size());
                } else if (circle.drawOrFill().equals("draw") && circle.draw()) {
                    _canvas.setColor(circle.color());
                    _canvas.drawOval(circle.x(), circle.y(), circle.size(), circle.size());
                }

    }
_drawing = false;
}

但我得到一个例外:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at jakob.GameEngine2D.GamePanel.draw(GamePanel.java:95)
at jakob.GameEngine2D.GamePanel.paint(GamePanel.java:330)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我认为问题是新的Circles被添加到ArrayList,而它在for循环中,我试图解决这个问题(boolean _drawing),但它不起作用。你能救我吗? -Jakob

1 个答案:

答案 0 :(得分:1)

尝试并发列表,例如CopyOnWriteArrayList