0

I would first like to say that I have extensively looked into this over the past day and I haven't found anything that fits this particular case. I'm in the midst of trying to create a simply Conway's Game of Life Application, and I seem to be having trouble with drawing to a JavaFX Canvas. Everything seems to be configurerd correctly (Canvas added to a layout container, said container added to a scene, and the stage is started) but even after trying different ways of encapsultating the code the Canvas remains to be empty

Here is my code:

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

                }

        }

    }
}

I left the imports out of the code for sake of brevity.

Thank you!

fabian
  • 80,457
  • 12
  • 86
  • 114
zebra14420
  • 51
  • 1
  • 2
  • 7
  • Why does your `Application` class implement `Runnable`? What thread are you expecting its `run()` method to be invoked on? Where are you starting that thread? You should look at https://stackoverflow.com/questions/9966136/javafx-periodic-background-task – James_D Jun 02 '18 at 18:32
  • Thank you! Will try to implement this instead when I get the chance later today. – zebra14420 Jun 02 '18 at 18:35
  • 1
    You never set the size of your canvas. `minWidth`, `maxWidth`, `prefWidth` and the equivalents for height are used to calculate the size, not set it. Furthermore a `Canvas` does not resize by itself. You need to set the `height` and `width` properies using the appropriate setters or the `Canvas` constructor... – fabian Jun 02 '18 at 19:06

0 Answers0