this is my first question in stackoverflow. I write the game using Java and Java Fx (scenebulider). I have problem with loading chosen image in second pane. First pane is menu pane and second pane is game pane.
To load fxml files I use FxmlUtils class I created:
public class FxmlUtils {
public static Pane fxmlLoader(String fxmlPath) {
FXMLLoader loader = new FXMLLoader(FxmlUtils.class.getResource(fxmlPath));
loader.setResources(getResourceBundle());
try {
return loader.load();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static ResourceBundle getResourceBundle() {
return ResourceBundle.getBundle("bundles.messages");
}
}
To load menu pane I use two methods:
@FXML
public void initialize() {
leftMenuButtonsController.setMainPaneController(this);
}
public void setButtonsPaneToLeft(String fxmlPath) {
buttonsBorderPane.setLeft(FxmlUtils.fxmlLoader(fxmlPath));
}
To load game pane I use that method:
@FXML
public void createGameStage(ActionEvent event) {
Parent gamePaneParent = FxmlUtils.fxmlLoader(PLAY_BUTTON_FXML);
Scene gameScene = new Scene(gamePaneParent);
Stage gameStage = (Stage)((Node)event.getSource()).getScene().getWindow();
gameStage.setScene(gameScene);
gameStage.setTitle(FxmlUtils.getResourceBundle().getString("title.game"));
gameStage.setResizable(false);
gameStage.show();
}
To back to menu pane I use button and method:
@FXML
public void backToMainMenu(ActionEvent event) {
Parent gamePaneParent = FxmlUtils.fxmlLoader(MAIN_MENU_FXML);
Scene gameScene = new Scene(gamePaneParent);
Stage gameStage = (Stage)((Node)event.getSource()).getScene().getWindow();
gameStage.setScene(gameScene);
gameStage.setTitle(FxmlUtils.getResourceBundle().getString("back.menu"));
gameStage.setResizable(false);
gameStage.show();
}
The pictures I saved in resources/ships folders in game. I try to load the chosen ship picture by using that methods:
public void clickedRedShip(MouseEvent event) {
gameSceneController.gameBorderPane.setBottom(redShipImage);
System.out.println("Red ship event");
}
public void clickedGreenShip(MouseEvent event) {
gameSceneController.gameBorderPane.setBottom(greenShipImage);
}
public void clickedBlueShip(MouseEvent event) {
gameSceneController.gameBorderPane.setBottom(blueShipImage);
}
public void clickedYellowShip(MouseEvent mouseEvent) {
gameSceneController.gameBorderPane.setBottom(yellowShipImage);
}
This above methods are defined in scene builder as "On Mouse Clicked". But unfortunately the chosen picture doesn't loading on game pane. I will be grateful for any help.