i'm creating conway's game of life simulation , i'm running problem can't seem figure out. can't seem jpanel
update on each iteration. have feeling simple i'm overlooking appreciated. first frame gets rendered after that, panel not repaint.
here code:
gameoflife.java
import javax.swing.jframe; public class gameoflife { private board currentboard; private board nextboard; private int generation = 0; private jframe currentframe; public gameoflife() { currentboard = new board(200, 100); nextboard = new board(currentboard.getxsize(), currentboard.getysize()); currentboard.populate(); } public void step() { generation++; system.out.println(generation); nextboard.clear(); (int = 0; < currentboard.getxsize(); i++) { (int j = 0; j < currentboard.getysize(); j++) { if(currentboard.getat(i, j) == true && (currentboard.numneighbors(i, j) == 2 || currentboard.numneighbors(i, j) == 3)) { nextboard.setalive(i, j); } else if (currentboard.getat(i, j) == false && currentboard.numneighbors(i, j) == 3){ nextboard.setalive(i, j); } } } //system.out.println(nextboard.tostring()); //system.out.println(currentboard.tostring()); currentboard = nextboard.copy(); currentboard.repaint(); } public board getboard() { return currentboard; } public void setframe(board panel) { currentframe.setcontentpane(panel); } public static void main(string[] args) throws interruptedexception { gameoflife game = new gameoflife(); jframe frame = new jframe(); game.currentframe = frame; frame.setcontentpane(game.getboard()); frame.setsize(game.getboard().getwidth(), game.getboard().getheight()); frame.setvisible(true); for(int = 0; < 10; i++) { game.step(); thread.sleep(5); } } }
board.java
import java.awt.graphics; import java.awt.graphics2d; import java.util.arraylist; import javax.swing.jframe; import javax.swing.jpanel; import java.util.random; /* * board game of life */ @suppresswarnings("serial") public class board extends jpanel{ private boolean[][] board; private int xsize; private int ysize; private final int cell_size = 5; private arraylist<string> alive; private random rand = new random(); private final double spawn_chance = .4; public board(int xsize, int ysize) { this.xsize = xsize; this.ysize = ysize; board = new boolean[xsize][ysize]; alive = new arraylist<string>(); } public void paint(graphics g) { super.paint(g); graphics2d g2d = (graphics2d)g; (int = 0; < xsize; i++) { (int j = 0; j < ysize; j++) { if(board[i][j]) { g2d.fillrect(cell_size*i, cell_size*j, cell_size, cell_size); } else { g2d.drawrect(cell_size*i, cell_size*j, cell_size, cell_size); } } } } public void clear() { (int = 0; < xsize; i++) { (int j = 0; j < ysize; j++) { board[i][j] = false; } } } public void populate() { (int = 0; < xsize; i++) { (int j = 0; j < ysize; j++) { if(rand.nextdouble() < spawn_chance) { board[i][j] = true; alive.add(i+","+j); } } } } public void setalive(int x, int y) { board[x][y] = true; alive.add(x+","+y); } public int numneighbors(int row, int col) { int neighbors = 0; (int x = math.max(0,row-1); x < math.min(row+2,xsize); x++) { (int y = math.max(0,col-1); y < math.min(col+2,ysize); y++) { if (board[x][y]) { neighbors ++; } } } //system.out.println("num neighbors " + row + "," + col + ": " + neighbors); return neighbors; } public board copy() { board newboard = new board(xsize, ysize); for(int = 0; < board.length; i++) for(int j = 0; j < board[0].length; j++) { if(this.getat(i, j)) { newboard.setalive(i, j); } } return newboard; } public int getwidth() { return xsize*cell_size; } public int getheight() { return ysize*cell_size; } public int getxsize() { return xsize; } public int getysize() { return ysize; } public boolean getat(int x, int y) { return board[x][y]; } public string tostring() { string toreturn = ""; (int = 0; < xsize; i++) { (int j = 0; j < ysize; j++) { if(board[i][j]) { toreturn += "x"; } else { toreturn += "o"; } } toreturn += "\n"; } return toreturn; } }
if take @ board#copy
can see creating brand new copy/instance of board
...
public board copy() { board newboard = new board(xsize, ysize); (int = 0; < board.length; i++) { (int j = 0; j < board[0].length; j++) { if (this.getat(i, j)) { newboard.setalive(i, j); } } } return newboard; }
but never added screen, can never displayed.
don't create "copy" of board, instead, update information board
suppose display.