0

我用Java写了一个简单的游戏。这是主要代码:

public class MainPanel extends JPanel {
    private Player player = new Player(100, 100, 3, 3);
    private Point2D targetPoint = new Point2D.Float(130, 350); //Pos on begin
    private ArrayList<Beam> beams = new ArrayList<Beam>();

    public MainPanel() {
        setPreferredSize(new Dimension(300, 400));

        addMouseMotionListener(new MouseMotionHandler());
        //Add shortcuts
        makeShortcut("player.BM1", "F1", new SetBeamModeAction(1));
        makeShortcut("player.BM2", "F2", new SetBeamModeAction(2));

        //Start threads
        Thread t = new Thread(new PlayerMoveRunnable());
        t.start();
        Thread t2 = new Thread(new PlayerShootRunnable());
        t2.start();
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.setColor(Color.BLACK);
        g2.fillRect(0, 0, 300, 400);
        //Draw player
        g2.drawImage(player.getImage(), (int)player.getX(), (int)player.getY(), null);
        //Draw beams
        for (Beam beam : beams) {
            g2.drawImage(beam.getImage(), (int)beam.getX(), (int)beam.getY(), null);
        }
    }

    //Thread running all the time
    private class PlayerMoveRunnable implements Runnable {
        public void run() {
            try {
                while (true) {
                    player.moveToPoint(targetPoint);
                    repaint();
                    Thread.sleep(15);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //Thread working all the time
    private class PlayerShootRunnable implements Runnable {
        public void run() {
            try {
                while (true) {
                    //Choose which beam to shoot (depends on set mode)
                    Thread t;
                    switch (player.getBeamMode()) {
                        case 1:
                            t = new Thread(new BeamMoveRunnable(new Beam1(player.getX()+18, player.getY(), 0, -15)));
                            break;
                        case 2:
                            t = new Thread(new BeamMoveRunnable(new Beam2(player.getX()+18, player.getY(), 0, -30)));
                            break;
                        default:
                            t = null;
                            break;
                    }
                    t.start();
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class BeamMoveRunnable implements Runnable {
        private Beam beam;

        public BeamMoveRunnable(Beam beam) {
            this.beam = beam;
        }

        public void run() {
            Beam beam = this.beam;
            beams.add(beam);
            try {
                while (true) {
                    if (beam.getY() <= 0) {
                        beams.remove(beam);
                        break;
                    }
                    beam.move();
                    repaint();
                    Thread.sleep(20);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

[它不是完整的代码。我剪了几行肯定不会引起问题]

我收到这样的错误:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at spacecommander.MainPanel.paintComponent(MainPanel.java:53)
    at javax.swing.JComponent.paint(JComponent.java:1054)

等等...

问题出在哪里?我知道 ConcurrentModificationException 是什么意思,但我不知道问题出在哪里。也许我应该进行一些同步。如果是,请显示在哪里

4

3 回答 3

1

在迭代光束之前,paintComponent创建集合的副本并使用副本进行迭代。

于 2013-01-08T15:27:22.187 回答
1

您正在修改另一个线程中的梁列表,例如BeamMoveRunnable,当您迭代集合时。

为每个对象创建一个线程非常浪费且难以管理。

我建议你有一个线程定期调用每个梁上的方法来移动。我建议您在绘制梁时不要添加或删除梁,这样您就不必同步对集合的访问。

于 2013-01-08T15:15:07.213 回答
0

beams您正在修改线程BeamMoveRunnable(包装此对象的线程)中的数组列表,Runnable同时在方法中对其进行迭代paintComponent。这就是异常的原因。

于 2013-01-08T15:13:12.093 回答