GameCardControl.java (2198B)
1 package nl.isygameclient.views; 2 3 import com.jfoenix.controls.JFXButton; 4 import javafx.beans.NamedArg; 5 import javafx.event.ActionEvent; 6 import javafx.fxml.FXML; 7 import javafx.fxml.FXMLLoader; 8 import javafx.fxml.Initializable; 9 import javafx.scene.control.Label; 10 import javafx.scene.image.Image; 11 import javafx.scene.image.ImageView; 12 import javafx.scene.layout.VBox; 13 import nl.isygameclient.Window; 14 import nl.isygameclient.util.StageHandler; 15 16 import java.io.IOException; 17 import java.io.InputStream; 18 import java.net.URL; 19 import java.util.ResourceBundle; 20 21 public 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 }