java - Recursive/looping calculations of new generation in The Game of Life -


everyone.

next generation in game of life application (link example application) calculated correcly. game works expected, must press "next" each time want new generation. i'm having problem implementing "start"-button loop generations. (see link difference between "next" , "start".)

it's obvious need kind of loop inside actionlistener class. i've tried calling nextgen() inside actionlistener recursivly while private boolean true. program crashes. i've tried setting kind of wait, not matter.

it 10 iterations if place 10 lines of nextgen(); inside listener, i'm guessing need kind of wait here. (the problem beeing memory.)

hope can me out on this. :)

the next generation calculated way.

actionlistener class:

public class gameoflifelistener implements actionlistener {      // important: gameoflifegrid contains gameoflife collection!     private gameoflifegrid gameoflife;        public gameoflifelistener ( gameoflifegrid g )      {         this.gameoflife = g;      }      @override     public void actionperformed (actionevent e)      {         // 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 ( )      {         // next generation         gameoflife.getcollection().nextgen();          // repaint         gameoflife.repaint();     } } 

the actionlistener runs nextgen() on gameoflife object when button "next" pressed. how nextgen() method works not important, here along parts of gameoflife class

public class gameoflife extends cellcollection {     // temporary array new generation . must add next generations alive cells here.     // else calculations of current generation fail.     private cell[][] nextgen = null;      public void nextgen ( )      {         // create new array holding next generation         preparenextcollection();          // iterate whole grid         ( int row = 0; row < super.rows; row++ )          {             ( int col = 0; col < super.cols; col++ )              {                  ruleone(row, col);             }         }          // set new collection superclass.          // super class holds collection drawn         super.setcollection(nextgen);     }       private void ruleone ( int row, int col )      {         // calculations not important. works expected.     }      private void preparenextcollection ( )      {         this.nextgen = new cell[rows][cols];     } 

this selected parts of gameoflifegrid class. draws grid , 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 code      @override     public void paintcomponent ( graphics g )     {         super.paintcomponent(g);         drawcells(g);     }      private void drawcells ( graphics g )      {         ( int row = 0; row < rows; row++ )          {             ( 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 timer object. timer send events each 100 ms.         // events caught actionlistener returned         // nextgenlistener(). voila!          timer = new timer(100, nextgenlistener());          // start sending events caught!         timer.start();     }      // added method! actionlistener catch events sent timer.     private actionlistener nextgenlistener ()      {         return new actionlistener()         {             @override             public void actionperformed(actionevent e)              {                 // next generation                 collection.nextgen();                  // repaint                 repaint();             }         };     } 

the issue when try loop thread.sleep() in actionperformed method, blocking event dispatch thread. nothing can repainted until release control of thread, repaint requests spool , fire off @ once when actionperformed method finishes.

the simple solution use javax.swing.timer fire events @ regular interval, along these lines:

new timer(5000, new actionlistener(){   public void actionperformed(actionevent e) {     panel.repaint();   } }).start(); 

Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -