JavaFX Canvas没有显示图纸

时间:2018-06-02 18:27:19

标签: java animation javafx

我首先想说的是,过去一天我已经广泛地研究了这个问题,而且我没有找到适合这种情况的任何东西。我正在尝试创建一个简单的Conway的生命游戏应用程序,我似乎在绘制到JavaFX Canvas时遇到了麻烦。一切似乎都是正确的配置(Canvas添加到布局容器,所述容器添加到一个场景,并且舞台已启动)但即使在尝试不同的方式封装代码后,Canvas仍然是空

这是我的代码:

public class Life extends Application implements Runnable {

    private Grid grid;
    private boolean running;
    private boolean paused;

    private int stepsPerMinute;

    @Override
    public void start(Stage stage) {

        this.grid = new Grid(60, 40);
        this.running = true;
        this.paused = false;
        this.stepsPerMinute = 60;

        StackPane root = new StackPane();
        root.getChildren().add(grid);

        Scene scene = new Scene(root, 1080, 720);
        stage.setTitle("Conway's Game of Life");
        stage.setScene(scene);
        stage.setResizable(false);

        stage.setOnCloseRequest(e -> stop());

        stage.show();

    }

    public void run() {

        while (running) {

            if (!paused) {

                try {
                    Thread.sleep(1000 / stepsPerMinute);
                } catch (InterruptedException e) { e.printStackTrace(); }

                grid.step();
                grid.draw();

            }

        }

    }

    @Override
    public void stop() {

        this.running = false;

        // TODO: Dump Stats at end

        System.exit(0);

    }

    public static void main(String[] args) { launch(args); }

    public class Grid extends Canvas {

        private final int WIDTH = 1080, HEIGHT = 720;

        private boolean[][] cellStates;
        private int rows, cols;
        private int stepNum;

        public Grid(int rows, int cols) {

            this.rows = rows;
            this.cols = cols;
            this.stepNum = 0;
            randomize();

            minWidth(WIDTH);
            maxWidth(WIDTH);
            prefWidth(WIDTH);
            minHeight(HEIGHT);
            maxHeight(HEIGHT);
            prefHeight(HEIGHT);

        }

        public Grid(boolean[][] cellStates) {
            this.cellStates = cellStates;
            this.rows = cellStates.length;
            this.cols = cellStates[0].length;
            this.stepNum = 0;
        }

        public void step() {

            boolean[][] updatedStates = new boolean[rows][cols];

            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++) {

                    int aliveNeighbors = countAliveNeighbors(i, j);

                    if (cellStates[i][j]) {
                        if (aliveNeighbors == 2 || aliveNeighbors == 3)
                            updatedStates[i][j] = true;
                    }
                    else if (aliveNeighbors == 3)
                        updatedStates[i][j] = true;

                }

            this.cellStates = updatedStates;
            stepNum++;

        }

        private int countAliveNeighbors(int r, int c) {

            int result = 0;

            for (int i = -1; i <= 1; i++)
                for (int j = -1; j <= 1; j++) {

                    if (cellStates[ (rows + r + i) % rows ][ (cols + c + j) % cols ])
                        result++;

                }

            return result;

        }

        public void randomize() {

            boolean[][] updatedStates = new boolean[rows][cols];

            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++) {

                    if (Math.random() < 0.08)
                        updatedStates[i][j] = true;


                }

            this.cellStates = updatedStates;

        }

        public void glider(){
            this.cellStates = new boolean[rows][cols];
            this.cellStates[1][2] = true;
            this.cellStates[2][3] = true;
            this.cellStates[3][1] = true;
            this.cellStates[3][2] = true;
            this.cellStates[3][3] = true;
        }

        public void draw() {

            GraphicsContext g = this.getGraphicsContext2D();

            int cellWidth = WIDTH / cols, cellHeight = HEIGHT / rows;

            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++) {

                    if (cellStates[i][j])
                        g.setFill(Color.color(0.0078, 0, 1));
                    else
                        g.setFill(Color.color(0.9961, 1, 0.8431));

                    g.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);

                    g.strokeLine(j * cellWidth, 0, j * cellWidth, HEIGHT);
                    g.strokeLine(0, i * cellHeight, WIDTH, i * cellHeight);

                }

        }

    }
}

为了简洁起见,我将导入的代码留下了。

谢谢!

0 个答案:

没有答案