swing - Conway's Game of Life Visualizing Java -



swing - Conway's Game of Life Visualizing Java -

i'm trying write conway's game of life in java eclipse. algorithm works have problem in visualizing. can draw little little rectangles live in array did not work @ animations. here code. how can visual ? thanks.

public class conwaysgameoflife extends jpanel { private int[][] grid; private static final random rnd = new random(); public conwaysgameoflife(int width, int height) { this.grid = new int[width][height]; setupgrid(); } private void setupgrid() { (int[] row : grid) { (int j = 0; j < row.length; j++) { if (rnd.nextdouble() < 0.92) continue; row[j] = rnd.nextint(2); } } } public void updategrid() { (int = 0; < grid.length; i++) { (int j = 0; j < grid[i].length; j++) { countanddecidedeadoralive(i, j); } } } private void countanddecidedeadoralive(int rowindex, int columnindex) { int counter = 0; (int = 0; < grid.length; i++) { (int j = 0; j < grid[i].length; j++) { counter = 0; if ((i > 0 && j > 0) && grid[i - 1][j - 1] == '1') counter++; if (i > 0 && grid[i - 1][j] == '1') counter++; if ((i > 0 && j < grid[i].length - 1) && grid[i - 1][j + 1] == '1') counter++; if (j > 0 && grid[i][j - 1] == '1') counter++; if (j < grid[i].length - 1 && grid[i][j + 1] == '1') counter++; if ((j > 0 && < grid.length - 1) && grid[i + 1][j - 1] == '1') counter++; if (i < grid.length - 1 && grid[i + 1][j] == '1') counter++; if ((i < grid.length - 1 && j < grid[i].length - 1) && grid[i + 1][j + 1] == '1') counter++; if (grid[i][j] == '1') { if (counter > 3 || counter < 2) grid[i][j] = '0'; } else { if (counter == 3) grid[i][j] = '1'; } } } } public dimension getpreferredsize() { homecoming new dimension(grid.length * 4, grid[0].length * 4); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); color gcolor = g.getcolor(); (int = 0; < grid.length; i++) { (int j = 0; j < grid[i].length; j++) { if (grid[i][j] == 1) { g.setcolor(color.black); g.fillrect(j * 4, * 4, 4, 4); } } } g.setcolor(gcolor); } public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("bir row giriniz"); int row = input.nextint(); system.out.println("bir column giriniz"); int column = input.nextint(); final conwaysgameoflife c = new conwaysgameoflife(row, column); jframe frame = new jframe(); frame.getcontentpane().add(c); frame.pack(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationbyplatform(true); frame.setvisible(true); new timer(10, new actionlistener() { public void actionperformed(actionevent e) { c.updategrid(); c.repaint(); } }).start(); } }

the problem comparing int values char '1' or '0' in algorithm.

initializing int values:

private void setupgrid() { (int[] row : grid) { (int j = 0; j < row.length; j++) { if (rnd.nextdouble() < 0.92) continue; row[j] = rnd.nextint(2); } } }

comparing char values:

if (grid[i][j] == '1') { if (counter > 3 || counter < 2) grid[i][j] = '0'; } else { if (counter == 3) grid[i][j] = '1'; }

java swing conways-game-of-life

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -