0

我正在编写一个简单的贪吃蛇游戏。所以,我有一个更新线程,执行一个无限循环,更新所有游戏对象并重新绘制 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);
    }
} 
4

3 回答 3

2

很可能您正在更改需要在该库似乎不允许的另一个线程中呈现的细节(我假设大多数 GUI 库不允许这样做)

当您更改 GUI 组件或您将渲染的东西时,您需要调用SwingUtils.invokeLater()以便任务将在 GUI 事件线程中执行,而不是在另一个线程中同时执行。

于 2012-11-22T15:08:29.173 回答
1

那么 CopyOnWriteArrayList 就是一个答案。当我有时间时,我会寻找另一个解决方案......

awt - 基于应用程序的另一个解决方案应该是

EventQueue.invokeLater(new Runnable() {...});
于 2012-11-23T07:35:31.777 回答
0

如果您在两个线程中调整列表,您会看到您提到的问题。如果您找不到“Comodification”,您可以依靠 java.util.concurrent 库。

http://docs.oracle.com/javase/tutorial/essential/concurrency/collections.html

于 2012-11-22T15:09:30.273 回答