I looked through almost every post on this subject, but most of them do not explain what to do correctly. To the question:
I created the javaFX application, a dice game, a human player against the computer, but at any time, playing a game, a human player should be able to click the "new game" button, and what needs to be done is to restart the game from the beginning.
I tried to start the stage again, but in javafx we cannot call the launch method twice.
1) Is there a way to implement this without restarting the entire application?
2) if not, how can I completely restart the application by pressing a button?
Main class
public class Main {
public static void main(String[] args) {
GameUI gameUI = new GameUI();
gameUI.launch(GameUI.class, args);
}
GameUI
( , . , , , . , .)
public class GameUI extends Application {
private Scene scene;
@Override
public void start(Stage primaryStage) throws Exception {
setLabelsPlyr(diesP);
setLabels(diesC);
btnThrow = new Button("Throw");
btnThrow.setPrefSize(70, 40);
btnThrow.setOnAction(e -> {
DieClass[] com = getNewDiceArrC();
lblDiceOneC.setGraphic(new ImageView(diesC[0].getDieImageC()));
DieClass[] playerAr = getNewDiceArrP();
lblDiceOnePlyr.setGraphic(new ImageView(diesP[0].getDieImageP()));
});
btnNewGame = new Button("New Game");
btnNewGame.setOnAction(e -> {
**
});
GridPane gridPane = new GridPane();
gridPane.add(lblComputer, 0, 0);
Scene scene = new Scene(gridPane, 1100, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("dice Game");
primaryStage.show();
}
public void setLabels(DieClass[] dies) {
for (int i=0; i < dies.length; i++) {
lblDiceOneC = new Label();
lblDiceOneC.setGraphic(new ImageView(dies[0].getDieImageC()));
++i;
break;
}
}
public void setLabelsPlyr(DieClass[] dies){
for (int i=0; i<dies.length; i++) {
lblDiceOnePlyr = new Label();
lblDiceOnePlyr.setGraphic(new ImageView(dies[0].getDieImageP()));
++i;
lblDiceTwoPlyr = new Label();
break;
}
}
p.s JavaFX Java.