我正在编写一个简单的贪吃蛇游戏。所以,我有一个更新线程,执行一个无限循环,更新所有游戏对象并重新绘制 JPanel。
// Run thread
public void run () {
try {
while (true) {
this.update();
this.getGamePanel().sceneShouldRepaint();
Thread.sleep(this.getFps());
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.OK_CANCEL_OPTION);
}
}
现在,问题是当我调用myJFrame.repaint()
它时,它可能正在另一个线程中执行。而当我绘画的时候,我显然必须得到物体的位置、速度等等……我知道异常来自paintComponent()
,因为它没有被捕获。如您所见,while 循环嵌入在 try-catch 中。
我能做些什么呢?
这是一个例外:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at ch.ilijatovilo.Scene.GameObjects.Creatures.Snake.Snake.render(Snake.java:52)
at ch.ilijatovilo.Scene.SceneHandling.Scene.renderSceneObjects(Scene.java:57)
at ch.ilijatovilo.Scene.SceneHandling.Scene.render(Scene.java:53)
at ch.ilijatovilo.Scene.SceneHandling.GamePanel.paintComponent(GamePanel.java:53)
at ch.ilijatovilo.Scene.SceneHandling.GamePanel.paint(GamePanel.java:30)
at javax.swing.JComponent._paintImmediately(JComponent.java:5106)
at javax.swing.JComponent.paintImmediately(JComponent.java:4890)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:812)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694)
at javax.swing.RepaintManager.access$700(RepaintManager.java:41)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1672)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:702)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:663)
at java.awt.EventQueue$2.run(EventQueue.java:661)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:672)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
谢谢你的帮助!
暗示
如果您尝试从 2 个不同的线程同时访问一个实例,则会引发 ConcurrentModificationException。至少我是这么听说的。
更多代码
// This is the render method in the Snake class at line 50.
// So 52 is the start of the for loop
public void render(Graphics g) {
// We delegate the drawing to the body parts
for (SnakeBodyPart sbp : this.getSnakeBodyParts()) {
sbp.render(g);
}
this.getSnakeHead().render(g);
}
// And here the for loop from the Scene class
protected void renderSceneObjects (Graphics g) {
for (SceneObject so : this.getSceneObjects()) {
so.render(g);
}
}