From aeef48258fb8d7cf75aaa4ea22f249d78cc2bb8a Mon Sep 17 00:00:00 2001 From: Francesco Bariatti <francesco.bariatti@insa-rennes.fr> Date: Mon, 2 May 2016 16:36:25 +0200 Subject: [PATCH] Added score and turn label to GUI --- gui/src/controller/Controller.java | 6 +++- gui/src/controller/UpdateThread.java | 26 +++++++++------ gui/src/main/Main.java | 3 +- gui/src/view/view.fxml | 48 ++++++++++++++++++++++++++-- 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/gui/src/controller/Controller.java b/gui/src/controller/Controller.java index c96d1eb..9fdf2cc 100644 --- a/gui/src/controller/Controller.java +++ b/gui/src/controller/Controller.java @@ -6,6 +6,7 @@ import javafx.fxml.Initializable; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import javafx.scene.control.ChoiceDialog; +import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; @@ -40,6 +41,9 @@ public class Controller implements Initializable @FXML private BorderPane mainPane; + @FXML + private Label scoreRed, scoreBlue, turnLabel; + @Override public void initialize(URL location, ResourceBundle resources) { @@ -91,7 +95,7 @@ public class Controller implements Initializable gameInput.println("c"); } - UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.board, this.boardView, humanPlayer); + UpdateThread upT = new UpdateThread(gameProcess, this.gameState, this.board, this.boardView, humanPlayer, scoreRed, scoreBlue, turnLabel); upT.setDaemon(true); upT.start(); } diff --git a/gui/src/controller/UpdateThread.java b/gui/src/controller/UpdateThread.java index 9a2e539..92f2834 100644 --- a/gui/src/controller/UpdateThread.java +++ b/gui/src/controller/UpdateThread.java @@ -3,6 +3,7 @@ package controller; import javafx.application.Platform; import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; +import javafx.scene.control.Label; import model.GameState; import model.Player; import model.Tile; @@ -18,8 +19,9 @@ public class UpdateThread extends Thread Tile[] board; TileView[] boardView; Player humanPlayer; + Label scoreRed, scoreBlue, turnLabel; - public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Player humanPlayer) + public UpdateThread(Process program, GameState gameState, Tile[] board, TileView[] boardView, Player humanPlayer, Label scoreRed, Label scoreBlue, Label turnLabel) { this.reader = new BufferedReader(new InputStreamReader(program.getInputStream())); this.writer = new PrintWriter(new OutputStreamWriter(program.getOutputStream()), true); @@ -27,6 +29,9 @@ public class UpdateThread extends Thread this.board = board; this.boardView = boardView; this.humanPlayer = humanPlayer; + this.scoreRed = scoreRed; + this.scoreBlue = scoreBlue; + this.turnLabel = turnLabel; } @@ -42,8 +47,7 @@ public class UpdateThread extends Thread if (line == null) { gameRunning = false; - } - else if(line.startsWith(Player.Red+" won") || line.startsWith(Player.Blue+" won") || line.startsWith("draw")) + } else if (line.startsWith(Player.Red + " won") || line.startsWith(Player.Blue + " won") || line.startsWith("draw")) { //System.out.println("======= END OF GAME======="); Platform.runLater(() -> { @@ -55,12 +59,11 @@ public class UpdateThread extends Thread message = "You just lost the penguin game."; else message = "That's a draw. Not bad!"; - Alert alert = new Alert(Alert.AlertType.INFORMATION, message, ButtonType.OK); + Alert alert = new Alert(Alert.AlertType.INFORMATION, message, ButtonType.OK); alert.showAndWait(); }); gameRunning = false; - } - else if (line.contains("{")) //Line contains JSON + } else if (line.contains("{")) //Line contains JSON { //UPDATE MODEL gameState.update(line.substring(line.indexOf("{"), line.lastIndexOf("}") + 1)); //Extract JSON string @@ -78,6 +81,11 @@ public class UpdateThread extends Thread //UPDATE VIEW for (int i = 0; i < boardView.length; i++) boardView[i].update(); + Platform.runLater(() -> { + scoreRed.setText(Integer.toString(gameState.getScore(Player.Red))); + scoreBlue.setText(Integer.toString(gameState.getScore(Player.Blue))); + turnLabel.setText(gameState.getCurrent_player()+ "'s turn"); + }); if (gameState.getCurrent_player() == humanPlayer) { @@ -92,8 +100,7 @@ public class UpdateThread extends Thread } } } - } - catch (IOException e) + } catch (IOException e) { gameRunning = false; Platform.runLater(() -> @@ -103,8 +110,7 @@ public class UpdateThread extends Thread e.printStackTrace(); Platform.exit(); }); - } - catch (Throwable e) + } catch (Throwable e) { gameRunning = false; Platform.runLater(() -> { diff --git a/gui/src/main/Main.java b/gui/src/main/Main.java index 96a011f..0e0910b 100644 --- a/gui/src/main/Main.java +++ b/gui/src/main/Main.java @@ -23,8 +23,9 @@ public class Main extends Application FXMLLoader loader = new FXMLLoader(); loader.setLocation(Main.class.getResource("../view/view.fxml")); BorderPane root = (BorderPane) loader.load(); - Scene scene = new Scene(root, 1024, 680); + Scene scene = new Scene(root); primaryStage.setScene(scene); + primaryStage.setTitle("Penguin game"); primaryStage.show(); } catch (Exception e) diff --git a/gui/src/view/view.fxml b/gui/src/view/view.fxml index 275ea39..2f7f1d6 100644 --- a/gui/src/view/view.fxml +++ b/gui/src/view/view.fxml @@ -1,13 +1,19 @@ <?xml version="1.0" encoding="UTF-8"?> +<?import javafx.geometry.*?> +<?import javafx.scene.text.*?> +<?import java.lang.*?> +<?import javafx.scene.control.*?> +<?import javafx.scene.layout.*?> +<?import javafx.scene.shape.*?> <?import java.lang.Double?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.BorderPane?> <?import javafx.scene.shape.Polygon?> -<BorderPane fx:id="mainPane" xmlns="http://javafx.com/javafx/8.0.71" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller"> +<BorderPane fx:id="mainPane" xmlns="http://javafx.com/javafx/8.0.92" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller"> <center> - <AnchorPane prefHeight="487.0" prefWidth="742.0" BorderPane.alignment="CENTER"> + <AnchorPane prefHeight="487.0" prefWidth="557.0" BorderPane.alignment="CENTER"> <children> <Polygon fx:id="tile7" fill="#1f93ff" layoutX="39.0" layoutY="434.0" stroke="BLACK" strokeType="INSIDE"> <points> @@ -975,7 +981,45 @@ <Double fx:value="-15.0" /> </points> </Polygon> + <Label layoutX="482.0" layoutY="104.0" text="Score"> + <font> + <Font size="24.0" /> + </font> + </Label> + <Label layoutX="482.0" layoutY="182.0" text="Blue :"> + <font> + <Font size="22.0" /> + </font> + </Label> + <Label layoutX="481.0" layoutY="151.0" text="Red :"> + <font> + <Font size="22.0" /> + </font> + </Label> + <Label fx:id="scoreRed" layoutX="556.0" layoutY="152.0" text="0"> + <font> + <Font size="22.0" /> + </font> + </Label> + <Label fx:id="scoreBlue" layoutX="556.0" layoutY="183.0" text="0"> + <font> + <Font size="22.0" /> + </font> + </Label> + <Label layoutX="216.0" layoutY="22.0" text="Penguin Game"> + <font> + <Font size="39.0" /> + </font> + </Label> + <Label fx:id="turnLabel" layoutX="482.0" layoutY="227.0" text="Blue's turn yo!"> + <font> + <Font size="24.0" /> + </font> + </Label> </children> </AnchorPane> </center> + <padding> + <Insets right="20.0" /> + </padding> </BorderPane> -- GitLab