gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:41:16 25 4
gpt4 key购买 nike

我做了一个小游戏。现在我想每 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 在 for 循环内部被添加到 ArrayList 中,我尝试修复此问题( boolean _drawing),但它不起作用。你可以帮我吗?-雅各布

最佳答案

尝试并发列表,例如 CopyOnWriteArrayList

关于ArrayList 中的 java.util.ConcurrentModificationException;尝试在绘画时添加新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34404189/

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