JavaFX Canvas 不显示绘图

JavaFX Canvas not showing drawings

提问人:zebra14420 提问时间:6/3/2018 最后编辑:fabianzebra14420 更新时间:6/3/2018 访问量:326

问:

我首先想说的是,在过去的一天里,我已经对此进行了广泛的研究,但我没有找到任何适合这种特殊情况的东西。我正在尝试创建一个简单的 Conway 的 Game of Life 应用程序,并且似乎在绘制到 JavaFX Canvas 时遇到了麻烦。一切似乎都已正确配置(添加到布局容器中,所述容器添加到场景中,并且舞台开始),但即使在尝试了不同的封装代码的方法之后,仍然是空的CanvasCanvas

这是我的代码:

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);

                }

        }

    }
}

为了简洁起见,我将导入排除在代码之外。

谢谢!

Java 动画 JavaFX

评论

0赞 James_D 6/3/2018
为什么你的类实现?您希望在哪个线程上调用其方法?你从哪里开始这个线程?你应该看看 stackoverflow.com/questions/9966136/......ApplicationRunnablerun()
0赞 zebra14420 6/3/2018
谢谢!当我今天晚些时候有机会时,将尝试实现这一点。
1赞 fabian 6/3/2018
您从不设置画布的大小。、 和高度的等效值用于计算大小,而不是设置大小。此外,a 本身不会调整大小。您需要使用适当的 setter 或构造函数来设置 and 属性...minWidthmaxWidthprefWidthCanvasheightwidthCanvas

答: 暂无答案