Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:05:19+00:00 2026-05-30T23:05:19+00:00

everyone. Next generation in my Game Of Life application (Link is example application) is

  • 0

everyone.

Next generation in my Game Of Life application (Link is example application) is calculated correcly. Game works as expected, but you must press “Next” each time you want a new generation. I’m having problem implementing a “Start”-button to loop generations. (See link for difference between “next” and “start”.)

It’s obvious I need some kind of loop inside the ActionListener class. I’ve tried calling nextGen() inside the actionListener recursivly while a private boolean is true. The program crashes. I’ve also tried setting some kind of wait, but it does not matter.

It really DOES do 10 iterations if I place 10 lines of nextGen(); inside the listener, so I’m guessing I need some kind of wait here. (The problem beeing memory.)

Hope you can help me out on this. 🙂

The next generation is calculated this way.

ActionListener class:

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();
    }
}

The actionListener runs nextGen() on the GameOfLife object when button “next” is pressed. How the nextGen() method works is not important, but here it along with some parts of the GameOfLife class

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];
    }

This is selected parts of the GameOfLifeGrid class. It draws the grid and the alive cells (Cell array).

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:05:21+00:00Added an answer on May 30, 2026 at 11:05 pm

    The issue is that when you try to loop with Thread.sleep() in the actionPerformed method, you are blocking the Event Dispatch Thread. Nothing can get repainted until you release control of the thread, so your repaint requests spool up and fire off all at once when your actionPerformed method finishes.

    The simple solution is to use a javax.swing.Timer to fire events at a regular interval, something along these lines:

    new Timer(5000, new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        panel.repaint();
      }
    }).start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

everyone. I'm trying to give a text link a background image but I'm not
Everyone (at least everyone who uses a compiled language) has faced compilation errors but
Good day everyone! I am trying to understand how buffer overflow works. Right now,
I'm using will_paginate in a facebook application and when i click on the next
Hey everyone I am currently getting the next variable based on if a file
I would like the next update of our C# application to be the last
I'm not sure but I believe that Python is -next to Objective-C- somewhat natural
Greetings, everyone. I consider myself to be an intermediate developer, but, to be candid,
I have a Phone interview coming up next with with a company which works
We have a .net service with simple commands (play, pause, next track, for example).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.