Browse Source

add destroy and getTitle methode to controllers

main^2
fdai7736 12 months ago
parent
commit
1771cc38b2
  1. 32
      src/main/java/de/hsfulda/onses/App.java
  2. 27
      src/main/java/de/hsfulda/onses/controllers/AppController.java
  3. 10
      src/main/java/de/hsfulda/onses/controllers/CardController.java
  4. 4
      src/main/java/de/hsfulda/onses/controllers/Controller.java
  5. 10
      src/main/java/de/hsfulda/onses/controllers/GameController.java
  6. 11
      src/main/java/de/hsfulda/onses/controllers/PlayerController.java
  7. 6
      src/main/resources/de/hsfulda/onses/views/app.fxml
  8. 5
      src/main/resources/de/hsfulda/onses/views/game.fxml
  9. 4
      src/test/java/de/hsfulda/onses/GuiTest.java

32
src/main/java/de/hsfulda/onses/App.java

@ -1,18 +1,44 @@
package de.hsfulda.onses;
import de.hsfulda.onses.controllers.AppController;
import de.hsfulda.onses.controllers.Controller;
import de.hsfulda.onses.services.GameService;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import java.io.IOException;
public class App extends Application {
private Stage stage;
private Controller controller;
@Override
public void start(Stage stage) throws Exception {
final AppController appController = new AppController(new GameService(), stage);
public void start(Stage stage) {
this.stage = stage;
final AppController appController = new AppController(this, new GameService());
stage.setTitle("Onses - Uno");
stage.setScene(new Scene(appController.render()));
stage.setScene(new Scene(new Label("Loading...")));
show(appController);
stage.show();
}
public void show(Controller controller) {
try {
final Parent parent = controller.render();
stage.getScene().setRoot(parent);
} catch (IOException e) {
throw new RuntimeException(e);
}
if(this.controller != null) {
this.controller.destroy();
}
this.controller = controller;
stage.setTitle(controller.getTitle());
}
}

27
src/main/java/de/hsfulda/onses/controllers/AppController.java

@ -1,13 +1,11 @@
package de.hsfulda.onses.controllers;
import de.hsfulda.onses.App;
import de.hsfulda.onses.Main;
import de.hsfulda.onses.models.Game;
import de.hsfulda.onses.services.GameService;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Objects;
@ -15,11 +13,11 @@ import java.util.Objects;
public class AppController implements Controller {
private final GameService gameService;
private final Stage stage;
private final App app;
public AppController(GameService gameService, Stage stage) {
public AppController(App app, GameService gameService) {
this.app = app;
this.gameService = gameService;
this.stage = stage;
}
@Override
public Parent render() throws IOException {
@ -28,13 +26,18 @@ public class AppController implements Controller {
Button button = (Button) parent.lookup("#startGameBtn");
button.setOnAction(e -> {
try {
stage.setScene(new Scene(gameController.render()));
stage.setTitle("Onses - Uno Game");
} catch (IOException ex) {
throw new RuntimeException(ex);
}
app.show(gameController);
});
return parent;
}
@Override
public String getTitle() {
return "Onses - Uno";
}
@Override
public void destroy() {
}
}

10
src/main/java/de/hsfulda/onses/controllers/CardController.java

@ -84,4 +84,14 @@ public class CardController implements Controller {
private String addStyle(String oldStyle, String newStyle) {
return oldStyle + "; " + newStyle;
}
@Override
public String getTitle() {
return "Onses - Uno Game";
}
@Override
public void destroy() {
}
}

4
src/main/java/de/hsfulda/onses/controllers/Controller.java

@ -6,4 +6,8 @@ import java.io.IOException;
public interface Controller {
Parent render() throws IOException;
void destroy();
String getTitle();
}

10
src/main/java/de/hsfulda/onses/controllers/GameController.java

@ -62,4 +62,14 @@ public class GameController implements Controller {
return parent;
}
@Override
public String getTitle() {
return "Onses - Uno Game";
}
@Override
public void destroy() {
}
}

11
src/main/java/de/hsfulda/onses/controllers/PlayerController.java

@ -9,7 +9,6 @@ import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
public class PlayerController implements Controller {
@ -44,4 +43,14 @@ public class PlayerController implements Controller {
return parent;
}
@Override
public String getTitle() {
return "Onses - Uno Game";
}
@Override
public void destroy() {
}
}

6
src/main/resources/de/hsfulda/onses/views/app.fxml

@ -5,14 +5,14 @@
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="456.0" prefWidth="683.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
<AnchorPane prefHeight="690.0" prefWidth="1254.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="252.0" layoutY="96.0" text="Uno Game" AnchorPane.leftAnchor="252.0" AnchorPane.topAnchor="96.0">
<Label layoutX="537.0" layoutY="147.0" text="Uno Game" AnchorPane.leftAnchor="537.0" AnchorPane.topAnchor="147.0">
<font>
<Font size="34.0" />
</font>
</Label>
<Button fx:id="startGameBtn" layoutX="274.0" layoutY="257.0" mnemonicParsing="false" text="Start Game" AnchorPane.leftAnchor="274.0" AnchorPane.topAnchor="257.0">
<Button fx:id="startGameBtn" layoutX="559.0" layoutY="345.0" mnemonicParsing="false" text="Start Game" AnchorPane.leftAnchor="559.0" AnchorPane.topAnchor="345.0">
<font>
<Font size="19.0" />
</font>

5
src/main/resources/de/hsfulda/onses/views/game.fxml

@ -41,5 +41,10 @@
<Font size="18.0" />
</font>
</Button>
<Button fx:id="exitBtn" layoutX="1048.0" layoutY="53.0" mnemonicParsing="false" text="return to menu" AnchorPane.rightAnchor="62.0" AnchorPane.topAnchor="53.0">
<font>
<Font size="16.0" />
</font>
</Button>
</children>
</AnchorPane>

4
src/test/java/de/hsfulda/onses/GuiTest.java

@ -10,11 +10,13 @@ import static org.testfx.matcher.control.LabeledMatchers.*;
public class GuiTest extends ApplicationTest {
private Stage stage;
private App app;
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
new App().start(stage);
app = new App();
app.start(stage);
}
@Test

Loading…
Cancel
Save