“生命游戏”中新生代的递归/循环计算

时间:2012-03-07 01:01:56

标签: java recursion multidimensional-array mouseevent composition

大家。

我的Game Of Life应用程序(链接是示例应用程序)中的下一代是正确计算的。游戏按预期工作,但每次想要新一代时都必须按“下一步”。我在实现“开始”按钮以循环生成时遇到问题。 (参见“next”和“start”之间差异的链接。)

很明显,我需要在ActionListener类中使用某种循环。我已经尝试在actionListener内部recursivly中调用nextGen(),而private boolean为true。程序崩溃了。我也试过设置某种等待,但没关系。

如果我放置10行nextGen(),它确实会进行10次迭代;在听众里面,所以我猜我需要在这里等待。 (问题在于记忆。)

希望你能帮我解决这个问题。 :)

下一代以这种方式计算。

ActionListener类:

public class GameOfLifeListener implements ActionListener
{

    // IMPORTANT: GameOfLifeGrid contains the GameOfLife collection!
    private GameOfLifeGrid gameOfLife;



    public GameOfLifeListener ( GameOfLifeGrid g ) 
    {
        this.gameOfLife = g;
     }

    @Override
    public void actionPerformed (ActionEvent e) 
    {
        // Get actionCommand
        String ac = e.getActionCommand();

        if ( ac.equals("next") ) 
        {
             // Method calculates next generation
             nextGen();
        }
        if ( ac.equals("start") ) 
        {
             // ADDED CODE: See class GameOfLifeGrid in bottom.
             gameOfLife.start();
        }
    }

    private void nextGen ( ) 
    {
        // Get Next generation
        gameOfLife.getCollection().nextGen();

        // Repaint
        gameOfLife.repaint();
    }
}

当按下“next”按钮时,actionListener在GameOfLife对象上运行nextGen()。 nextGen()方法的工作原理并不重要,但这里它与GameOfLife类的某些部分一起

public class GameOfLife extends CellCollection
{
    // Temporary array for new generation . We must add next generations alive cells HERE.
    // Else the calculations of the current generation will fail.
    private Cell[][] nextGen = null;

    public void nextGen ( ) 
    {
        // Create the new Array holding next generation
        prepareNextCollection();

        // Iterate the whole grid
        for ( int row = 0; row < super.rows; row++ ) 
        {
            for ( int col = 0; col < super.cols; col++ ) 
            {
                 ruleOne(row, col);
            }
        }

        // Set the new collection to superClass. 
        // Super class holds the collection that will be drawn
        super.setCollection(nextGen);
    }


    private void ruleOne ( int row, int col ) 
    {
        // Calculations not important. It works like expected.
    }

    private void prepareNextCollection ( ) 
    {
        this.nextGen = new Cell[rows][cols];
    }

这是GameOfLifeGrid类的选定部分。它绘制网格和活细胞(细胞阵列)。

public class GameOfLifeGrid extends Grid
{

    private GameOfLife collection = null;

    // ADDED MEMBERS: Timer, int
    private Timer timer; 
    private int updateEachMilliSec = 100; // Used in program. Not in this code

    @Override
    public void paintComponent ( Graphics g )
    {
        super.paintComponent(g);
        drawCells(g);
    }

    private void drawCells ( Graphics g ) 
    {
        for ( int row = 0; row < rows; row++ ) 
        {
            for ( int col = 0; col < cols; col++ )
            {
                if ( ! collection.isEmptyPos(row, col) ) 
                {
                    g.fillRect(super.calcX(col), super.calcY(row), cellSize, cellSize);
                }
            }
        } 
    }

    // ADDED METHOD!
    public void start() 
    {
        // Create a timer object. The timer will send events each 100 ms.
        // The events will be caught by the ActionListener returned from
        // nextGenlistener(). VOILA!

        timer = new Timer(100, nextGenlistener());

        // Start sending events to be caught!
        timer.start();
    }

    // ADDED METHOD! The ActionListener who will catch the events sent by timer.
    private ActionListener nextGenlistener () 
    {
        return new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                // Get next generation
                collection.nextGen();

                // Repaint
                repaint();
            }
        };
    }

1 个答案:

答案 0 :(得分:1)

问题在于,当您尝试在actionPerformed方法中使用Thread.sleep()循环时,您将阻止Event Dispatch Thread。在释放对线程的控制权之前,没有任何东西可以重新绘制,因此当您的actionPerformed方法完成时,您的重绘请求将立即关闭并立即关闭。

简单的解决方案是使用javax.swing.Timer定期触发事件,这些内容如下:

new Timer(5000, new ActionListener(){
  public void actionPerformed(ActionEvent e) {
    panel.repaint();
  }
}).start();