我正在使用javaFX开发生命游戏的GUI。我试图在一秒钟的延迟后在画布上绘制每一代,但我的代码不会这样做。发生的是它获得随机零生成,等待一秒,生成第一代,然后等待另一秒生成下一代,依此类推,直到达到确定的代数。在完成整个过程之后,它只绘制了最后一代。我尝试了一些没有运气的实验,现在我没有想法。如何在每代计算后绘制程序?
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class gui extends Application {
/////////////////////GUI SETUP PART/////////////////////////////////////////////
public int SquareSize = 16;
public int oldgrid [][];
public int newgrid [][];
public int ngens=5;
public int width = 10;
public int height = 10;
public int currentgen = 0;
public Group root;
public void start(Stage primaryStage) {
primaryStage.setTitle("Game of Life");
root = new Group();
oldgrid = new int [height][width];
newgrid = new int [height][width];
randomize();
Canvas canvas = new Canvas(500, 500);
GraphicsContext gc = canvas.getGraphicsContext2D();
run(gc);
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public void draw(GraphicsContext gc)
{
for(int y =0 ; y < getY() ; y++ )
{
for (int x = 0; x < getX() ; x++)
{
if(oldgrid[y][x] == 0)
{
gc.setFill(Color.WHITE);
gc.fillRect(x*SquareSize, y*SquareSize, SquareSize, SquareSize);
}
else if(oldgrid[y][x] == 1)
{
gc.setFill(Color.BLACK);
gc.fillRect(x*SquareSize, y*SquareSize, SquareSize, SquareSize);
}
}
}
}
////////////////////game of life rules part////////////////////////
public int getcurrentgen() {
return currentgen;
}
public int getX() {
return oldgrid[0].length;
}
public int getY() {
return oldgrid.length;
}
int Neighbours(int x, int y) //calculating neighbours
{
int neighbours = 0;
if (x > 0 && oldgrid[y][x - 1] == 1) {
neighbours++;
}
if (x < getX() - 1 && oldgrid[y][x + 1] == 1) {
neighbours++;
}
if (y < getY() - 1 && x > 0 && oldgrid[y + 1][x - 1] == 1) {
neighbours++;
}
if (y < getY() - 1 && x < getX() - 1 && oldgrid[y + 1][x + 1] == 1) {
neighbours++;
}
if (y < getY() - 1 && oldgrid[y + 1][x] == 1) {
neighbours++;
}
if (y > 0 && x > 0 && oldgrid[y - 1][x - 1] == 1) {
neighbours++;
}
if (y > 0 && x < getX() - 1 && oldgrid[y - 1][x + 1] == 1) {
neighbours++;
}
if (y > 0 && oldgrid[y - 1][x] == 1) {
neighbours++;
}
return neighbours;
}
public void setup() // applying the rules
{
for (int y = 0; y < getY(); y++) {
for (int x = 0; x < getX(); x++) {
if (oldgrid[y][x] == 1) {
if (Neighbours(x, y) < 2) {
newgrid[y][x] = 0;
}
if (Neighbours(x, y) > 3) {
newgrid[y][x] = 0;
}
if (Neighbours(x, y) == 3 || Neighbours(x, y) == 2) {
newgrid[y][x] = 1;
}
}
if (oldgrid[y][x] == 0) {
if (Neighbours(x, y) == 3) {
newgrid[y][x] = 1;
}
}
}
}
}
public void run(GraphicsContext gc)
{
draw(gc);
while(currentgen < ngens)
{
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
setup();
for (int y = 0; y < getY(); y++) {
for (int x = 0; x < getX(); x++) {
oldgrid[y][x] = newgrid[y][x];
}
}
currentgen++;
draw(gc);
}
}
public void randomize() {
// randomizing dead(0) and live(1) cells
for (int y = 0; y < getY(); y++) {
for (int x = 0; x < getX(); x++) {
double tmp = Math.random();
if (tmp < 0.5) {
oldgrid[y][x] = 0;
} else {
oldgrid[y][x] = 1;
}
}
}
}
}