hanze/game-client

src/main/java/nl/isygameclient/views/GameCardControl.java in main
Repositories | Summary | Log | Files

GameCardControl.java (2198B) download


 1package nl.isygameclient.views;
 2
 3import com.jfoenix.controls.JFXButton;
 4import javafx.beans.NamedArg;
 5import javafx.event.ActionEvent;
 6import javafx.fxml.FXML;
 7import javafx.fxml.FXMLLoader;
 8import javafx.fxml.Initializable;
 9import javafx.scene.control.Label;
10import javafx.scene.image.Image;
11import javafx.scene.image.ImageView;
12import javafx.scene.layout.VBox;
13import nl.isygameclient.Window;
14import nl.isygameclient.util.StageHandler;
15
16import java.io.IOException;
17import java.io.InputStream;
18import java.net.URL;
19import java.util.ResourceBundle;
20
21public class GameCardControl extends VBox implements Initializable {
22
23    private final String gameTitle;
24    private final String gameImageUrl;
25    private final String gameSource;
26
27    @FXML
28    private Label label;
29
30    @FXML
31    private JFXButton button;
32
33    @FXML
34    private ImageView imageView;
35
36
37    public GameCardControl(@NamedArg("gameTitle") String gameTitle, @NamedArg("gameImageUrl") String gameImageUrl, @NamedArg("gameSource") String gameSource) {
38        this.gameTitle = gameTitle;
39        this.gameImageUrl = gameImageUrl;
40        this.gameSource = gameSource;
41
42        URL url = getClass().getResource("/nl/isygameclient/views/game-card.fxml");
43        FXMLLoader fxmlLoader = new FXMLLoader(url);
44        fxmlLoader.setRoot(this);
45        fxmlLoader.setController(this);
46
47        try {
48            fxmlLoader.load();
49        } catch (IOException e) {
50            e.printStackTrace();
51        }
52    }
53
54    @Override
55    public void initialize(URL url, ResourceBundle resourceBundle) {
56        this.label.textProperty().set(gameTitle);
57
58        InputStream imageURL = getClass().getResourceAsStream(gameImageUrl);
59        if (imageURL != null) {
60            Image image = new Image(imageURL, 250, 200, false, false);
61            this.imageView.setImage(image);
62        }
63
64        button.setOnAction(this::onStartGameButtonClick);
65    }
66
67    public void onStartGameButtonClick(ActionEvent e) {
68        var stageHandler = StageHandler.get();
69        stageHandler.changeSceneOfStage(Window.GAME, gameSource);
70        stageHandler.focusStage(Window.GAME);
71    }
72
73    public String getGameTitle() {
74        return gameTitle;
75    }
76}