I have the following Problem.
I'm am a complete newbie in JavaFX and I am trying to code a Game of Life program.
I have a class "MainView" extending VBox. MainView has an ArrayList "playingField" of the class Field as an attribute, consisting of cells and also a draw() method, which draws rectangles on a Canvas, according to the states of the cells.
Inside of Field I have a transition() method, which changes the state of the cells, according to some defined transition rules.
This is working fine, and I can manually progress by clicking on a button on my primary stage. What I want though, is a possibility to press a button once, and then have indefinite steps of progresses -> applications of the transition()-method and redrawings of the canvas.
If I try to do it in a loop, it works just fine, but of course happens way to fast to recognize the single steps, but when I use Thread.sleep(), the whole application freezes.
I know I have to solve this with either a timeline or a transitions, but I can't really find sufficient informations online on how to inegrate the transition() and draw() methods in an infinite cycle in either one of them.
I hope following snippets will help to make myself more clear:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
MainView mainView = new MainView();
Scene scene = new Scene(mainView, 1000, 1032);
primaryStage.setScene(scene);
primaryStage.show();
mainView.draw();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
public MainView() extends VBox {
private Button stepButton;
private Canvas canvas;
private Field playingField;
this.stepButton = new Button("progress");
this.stepButton.setOnAction(ActionEvent -> {
playingField.fieldTransition();
draw();
});
public void draw() {
//drawing Rectangles that represent the cells of field on a canvas
}
}
}
Field class with the transitions() method:
public class Field implements TransitionRules {
public void fieldTransition(){
//method to change the state of the cells according to given rules.
}
}