1

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.

Michal
  • 11
  • 2
  • 1
    https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other – SedJ601 May 21 '20 at 16:58
  • Thank you for link, but I don't have any erros. – Michal May 21 '20 at 17:10
  • If you read the linked question that should resolve all of your problems. – SedJ601 May 21 '20 at 17:33
  • You haven't posted info about what `gameSceneController.gameBorderPane` refers to, but it doesn't seem like the right way of [passing parameters to a fxml controller](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml). To me this rather seems like you're encountering [issues with `static`](https://stackoverflow.com/questions/23105433/javafx-8-compatibility-issues-fxml-static-fields) that are probably just circumvented by initializing fields with nodes that are never shown on screen; at least that would be my guess on why this fails without an exception. – fabian May 22 '20 at 06:20
  • Thank you for your answer and links. The BorderPane "gameBorderPane" have controller (GameSceneController class). I calling in a class appropriate for buttons (LeftMenuButtonsController) in a method responsive to pressing the mouse button (onMouseClicked) a BorderPane (gameBorderPane) in which I want to display the previously selected ship. I have paths to individual ship images declared as static fields. I also tried to declare them in Image, but it also did not help. – Michal May 24 '20 at 16:00

0 Answers0