From 6d6cc5b0139b4cc6cf986a6d4658bcf1a657a807 Mon Sep 17 00:00:00 2001 From: "antoine.perederii" Date: Fri, 8 Dec 2023 13:19:06 +0100 Subject: [PATCH 1/2] update javafx's tp with beautiful graphics --- .../tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml | 33 ++++++++++--------- 2A/Java/tp/JavaFX/tpjeude/src/data/Stub.java | 13 ++++++++ .../JavaFX/tpjeude/src/launcher/Launch.java | 3 -- .../tp/JavaFX/tpjeude/src/view/Fenetre.java | 28 +++++++++------- 4 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 2A/Java/tp/JavaFX/tpjeude/src/data/Stub.java diff --git a/2A/Java/tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml b/2A/Java/tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml index c9dd117..61dce2f 100644 --- a/2A/Java/tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml +++ b/2A/Java/tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml @@ -1,23 +1,24 @@ - - + + - - - - -
- -
- - - + + + + +
+ +
+ + +
+ diff --git a/2A/Java/tp/JavaFX/tpjeude/src/data/Stub.java b/2A/Java/tp/JavaFX/tpjeude/src/data/Stub.java new file mode 100644 index 0000000..ad6afdd --- /dev/null +++ b/2A/Java/tp/JavaFX/tpjeude/src/data/Stub.java @@ -0,0 +1,13 @@ +package data; + +import model.Game; +import model.Player; + +public class Stub { + public Game loadPlayer() { + Game game = new Game(); + game.addPlayer(new Player("Cedric")); + game.addPlayer(new Player("Jérome")); + return game; + } +} diff --git a/2A/Java/tp/JavaFX/tpjeude/src/launcher/Launch.java b/2A/Java/tp/JavaFX/tpjeude/src/launcher/Launch.java index c49c037..f105861 100644 --- a/2A/Java/tp/JavaFX/tpjeude/src/launcher/Launch.java +++ b/2A/Java/tp/JavaFX/tpjeude/src/launcher/Launch.java @@ -5,10 +5,7 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; -import view.Fenetre; - import java.io.IOException; -import java.util.Objects; public class Launch extends Application { Parent root = FXMLLoader.load(getClass().getResource("/fxml/Fenetre.fxml")); diff --git a/2A/Java/tp/JavaFX/tpjeude/src/view/Fenetre.java b/2A/Java/tp/JavaFX/tpjeude/src/view/Fenetre.java index 8680ad3..3e6821b 100644 --- a/2A/Java/tp/JavaFX/tpjeude/src/view/Fenetre.java +++ b/2A/Java/tp/JavaFX/tpjeude/src/view/Fenetre.java @@ -1,5 +1,6 @@ package view; +import data.Stub; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.image.Image; @@ -27,17 +28,7 @@ public class Fenetre { @FXML private ImageView diceImage; private boolean hasRolled = false; - private Game game; - private final Player p1 = new Player("Cedrice"); - private final Player p2 = new Player("Jerome"); - - public void initialize() { - game = new Game(); - game.addPlayer(p1); - game.addPlayer(p2); - updateUI(); - } - + private final Game game = new Stub().loadPlayer(); @FXML private void onButtonHoldClicked() { @@ -85,7 +76,22 @@ public class Fenetre { Image image = new Image(getClass().getResourceAsStream(imagePath)); diceImage.setImage(image); } + + // Vérifier si un joueur a gagné + if (currentPlayer.getTotalScore() >= Game.SCORE_TO_WIN) { + topText.setText(currentPlayer.getName() + " wins the game!"); + topText.setStyle("-fx-fill: #e74c3c;"); // Définir la couleur du texte en rouge + buttonRoll.setDisable(true); + buttonHold.setDisable(true); + } else { + // Si le jeu n'est pas terminé, utilisez la couleur par défaut ou une autre couleur de votre choix + topText.setStyle("-fx-fill: black;"); + } } + public void initialize() { +// topText.textProperty().bind(game.getCurrentPlayer()); + updateUI(); + } } From b5a5d619d4b31787efa3ccd7b644eb3674205beb Mon Sep 17 00:00:00 2001 From: "antoine.perederii" Date: Fri, 8 Dec 2023 16:05:26 +0100 Subject: [PATCH 2/2] update JavaFX's tp --- .../tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml | 36 +++++---- .../JavaFX/tpjeude/src/launcher/Launch.java | 10 +-- .../tp/JavaFX/tpjeude/src/model/Player.java | 46 +++++------ .../tp/JavaFX/tpjeude/src/view/Fenetre.java | 81 ++++++++++--------- 4 files changed, 88 insertions(+), 85 deletions(-) diff --git a/2A/Java/tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml b/2A/Java/tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml index 61dce2f..bdd3cbd 100644 --- a/2A/Java/tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml +++ b/2A/Java/tp/JavaFX/tpjeude/res/fxml/Fenetre.fxml @@ -1,24 +1,32 @@ + - - - -
- -
- - - + + + +
+ +
+ + +
- diff --git a/2A/Java/tp/JavaFX/tpjeude/src/launcher/Launch.java b/2A/Java/tp/JavaFX/tpjeude/src/launcher/Launch.java index f105861..81cb1c7 100644 --- a/2A/Java/tp/JavaFX/tpjeude/src/launcher/Launch.java +++ b/2A/Java/tp/JavaFX/tpjeude/src/launcher/Launch.java @@ -5,17 +5,15 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; -import java.io.IOException; public class Launch extends Application { - Parent root = FXMLLoader.load(getClass().getResource("/fxml/Fenetre.fxml")); - - public Launch() throws IOException { - } @Override public void start(Stage primaryStage) throws Exception { - primaryStage.setScene(new Scene(root)); + FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Fenetre.fxml")); + Parent root = loader.load(); + + primaryStage.setScene(new Scene(root, 700, 500)); primaryStage.show(); } } \ No newline at end of file diff --git a/2A/Java/tp/JavaFX/tpjeude/src/model/Player.java b/2A/Java/tp/JavaFX/tpjeude/src/model/Player.java index 715bb9b..add8843 100644 --- a/2A/Java/tp/JavaFX/tpjeude/src/model/Player.java +++ b/2A/Java/tp/JavaFX/tpjeude/src/model/Player.java @@ -1,12 +1,26 @@ package model; +import javafx.beans.property.IntegerProperty; +import javafx.beans.property.SimpleIntegerProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; + public class Player { private static final String BASE_PLAYER_NAME_DEFAULT = "Player "; private static int num = 0; - - private String name; - private int totalScore; - private int currentScore; + private StringProperty name = new SimpleStringProperty(); + public String getName() { return name.get(); } + public StringProperty nameProperty() { return name; } + private void setName(String name) { this.name.set(name); } + + private IntegerProperty totalScore = new SimpleIntegerProperty(); + public Integer getTotalScore() { return totalScore.get(); } + public IntegerProperty totalScoreProperty() { return totalScore; } + public void setTotalScore(Integer totalScore) { this.totalScore.set(totalScore); } + private IntegerProperty currentScore = new SimpleIntegerProperty(); + public Integer getCurrentScore() { return currentScore.get(); } + public IntegerProperty currentScoreProperty() { return currentScore; } + public void setCurrentScore(Integer currentScore) { this.currentScore.set(currentScore); } public Player(String name) { setName(name); @@ -16,30 +30,6 @@ public class Player { this(BASE_PLAYER_NAME_DEFAULT + ++num); } - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getTotalScore() { - return totalScore; - } - - public void setTotalScore(int score) { - totalScore = score; - } - - public int getCurrentScore() { - return currentScore; - } - - public void setCurrentScore(int score) { - currentScore = score; - } - @Override public String toString() { return name + " (" + totalScore + ") has currently " + currentScore; diff --git a/2A/Java/tp/JavaFX/tpjeude/src/view/Fenetre.java b/2A/Java/tp/JavaFX/tpjeude/src/view/Fenetre.java index 3e6821b..cdedfe6 100644 --- a/2A/Java/tp/JavaFX/tpjeude/src/view/Fenetre.java +++ b/2A/Java/tp/JavaFX/tpjeude/src/view/Fenetre.java @@ -3,13 +3,14 @@ package view; import data.Stub; import javafx.fxml.FXML; import javafx.scene.control.Button; +import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.text.Text; import model.Game; import model.Player; -import java.util.Objects; +import java.util.List; public class Fenetre { @@ -24,6 +25,10 @@ public class Fenetre { @FXML private Text bottomText; + @FXML + private TextField joueur1; + @FXML + private TextField joueur2; @FXML private ImageView diceImage; @@ -32,66 +37,68 @@ public class Fenetre { @FXML private void onButtonHoldClicked() { + joueur1.setDisable(true); + joueur2.setDisable(true); +// hasRolled = true; game.passToNextPlayer(); - hasRolled = true; updateUI(); } @FXML private void onButtonRollClicked() { - hasRolled = true; + joueur1.setDisable(true); + joueur2.setDisable(true); +// hasRolled = true; game.rollDice(); - - int diceValue = 0; - try { - diceValue = game.getDiceValue(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - String imagePath = "/dice/" + diceValue + ".png"; - Image image = new Image(getClass().getResourceAsStream(imagePath)); - diceImage.setImage(image); - updateUI(); - - // Si le dé affiche la valeur 1, passez au joueur suivant et mettre à jour l'interface graphique. - if (diceValue == 1) { - topText.setText("Le joueur précedent à perdue\n" + game.getCurrentPlayer().getName() + ", press R to roll the dice or H to hold your score."); - imagePath = "/dice/0.png"; - image = new Image(getClass().getResourceAsStream(imagePath)); - diceImage.setImage(image); - hasRolled = false; - } } private void updateUI() { Player currentPlayer = game.getCurrentPlayer(); - topText.setText(currentPlayer.getName() + ", press R to roll the dice or H to hold your score."); - bottomText.setText(currentPlayer.getName() + "(" + currentPlayer.getTotalScore() + ")" + " has currently " + currentPlayer.getCurrentScore()); - - if (!hasRolled) { - String imagePath = "/dice/0.png"; - Image image = new Image(getClass().getResourceAsStream(imagePath)); - diceImage.setImage(image); + int diceValue = 0; + try { +// if(hasRolled) { + diceValue = game.getDiceValue(); +// } + } catch (IllegalAccessException e) { + topText.setStyle("-fx-fill: black;"); + topText.textProperty().unbind(); + topText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(", press R to roll the dice or H to hold your score.")); + bottomText.textProperty().unbind(); + bottomText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(" (").concat(game.getCurrentPlayer().totalScoreProperty()).concat(") has currently ").concat(game.getCurrentPlayer().currentScoreProperty())); +// e.printStackTrace(); + return; } - - // Vérifier si un joueur a gagné - if (currentPlayer.getTotalScore() >= Game.SCORE_TO_WIN) { - topText.setText(currentPlayer.getName() + " wins the game!"); - topText.setStyle("-fx-fill: #e74c3c;"); // Définir la couleur du texte en rouge + if (currentPlayer.getTotalScore() == Game.LOSE_DICE_VALUE) { + game.passToNextPlayer(); + topText.textProperty().unbind(); + topText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(" à perdue\n").concat(game.getCurrentPlayer().nameProperty()).concat(", press R to roll the dice or H to hold your score.")); +// hasRolled = false; + } else if (currentPlayer.getTotalScore() >= Game.SCORE_TO_WIN) { + topText.textProperty().unbind(); + topText.textProperty().bind(currentPlayer.nameProperty().concat(" wins the game")); + topText.setStyle("-fx-fill: #e74c3c;"); buttonRoll.setDisable(true); buttonHold.setDisable(true); } else { - // Si le jeu n'est pas terminé, utilisez la couleur par défaut ou une autre couleur de votre choix topText.setStyle("-fx-fill: black;"); + topText.textProperty().unbind(); + topText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(", press R to roll the dice or H to hold your score.")); + bottomText.textProperty().unbind(); + bottomText.textProperty().bind(game.getCurrentPlayer().nameProperty().concat(" (").concat(game.getCurrentPlayer().totalScoreProperty()).concat(") has currently ").concat(game.getCurrentPlayer().currentScoreProperty())); } + String imagePath = "/dice/" + diceValue + ".png"; + Image image = new Image(imagePath); + diceImage.setImage(image); } public void initialize() { -// topText.textProperty().bind(game.getCurrentPlayer()); + joueur1.textProperty().bindBidirectional(game.getCurrentPlayer().nameProperty()); + game.passToNextPlayer(); + joueur2.textProperty().bindBidirectional(game.getCurrentPlayer().nameProperty()); updateUI(); } }