JavaFX How to change css for an HBox


Question

I wanna change the background image for the different levels i got: This is my Methode to set the BackgroundImage while using css:

public void setBackgroundImage(HBox group, String ebene) {
        switch (ebene) {
            case "air": {
                String air_img = GrafikManager.class.getResource("/obkclient/img/Himmelsschlacht.jpg").toExternalForm();
                group.getStyleClass().removeAll(); //also tried with getStylsheets()
                group.setStyle("-fx-background-image: url('" + air_img + "');\n"
                        + "-fx-background-size: 1024, 768;\n"
                        + "-fx-background-repeat: stretch;\n"
                );
                break;
            }
            case "sub": {
                String sub_img = GrafikManager.class.getResource("/obkclient/img/Unterwasserschlacht.jpg").toExternalForm();
                group.getStyleClass().removeAll();
                group.setStyle("-fx-background-image: url('" + sub_img + "');\n"
                        + "-fx-background-size: 1024, 768;\n"
                        + "-fx-background-repeat: stretch;\n"
                );
                break;
            }
            default: {
                String image = GrafikManager.class.getResource("/obkclient/img/Seeschlacht.jpg").toExternalForm();
                group.getStyleClass().removeAll();
                group.setStyle("-fx-background-image: url('" + image + "');\n"
                        + "-fx-background-size: 1024, 768;\n"
                        + "-fx-background-repeat: stretch;\n"
                );
                break;
            }
        }
    } 

But if i start i only see the default BackgroundImage. I'm using this Methode in following Context:

public SubScene buildFieldBackground(Scene scene){
        PerspectiveCamera subSceneFieldCamera = new PerspectiveCamera(false);
        scene.setCamera(subSceneFieldCamera);
        HBox subSceneGroup2 = new HBox();
        setBackgroundImage(subSceneGroup2, currentEbene);
        subSceneGroup2.setPadding(new Insets(0, 0, 0, 0));
        SubScene subScene2 = new SubScene(subSceneGroup2, 1024, 768);
        subScene2.setLayoutX(100);
        subScene2.setLayoutY(100);

        return subScene2;
    }

and the CurrentEbene can get following stats (setting by an other Methode): "air", "sea", "sub"

Where is the mistake i dont find, yet. Please help.

1
0
4/24/2014 7:51:10 PM

I would actually recommend using an external style sheet for this, and manipulating the style class based on your "level" variable. You can do:

public void setBackgroundImage(HBox group, String ebene) {
    ObservableList<String> styleClass = group.getStyleClass();
    styleClass.setAll("hbox-group", ebene);
}


public SubScene buildFieldBackground(Scene scene){
        PerspectiveCamera subSceneFieldCamera = new PerspectiveCamera(false);
        scene.setCamera(subSceneFieldCamera);
        HBox subSceneGroup2 = new HBox();

        subSceneGroup2.getStylesheets().add(getClass().getResource("subscene.css").toExternalForm());

        setBackgroundImage(subSceneGroup2, currentEbene);
        subSceneGroup2.setPadding(new Insets(0, 0, 0, 0));
        SubScene subScene2 = new SubScene(subSceneGroup2, 1024, 768);
        subScene2.setLayoutX(100);
        subScene2.setLayoutY(100);


        return subScene2;
    }

and then

subscene.css:

.hbox-group {
    -fx-background-size: 1024, 768;
    -fx-background-repeat: stretch;
    -fx-background-image: url('/obkclient/img/Seeschlacht.jpg') ;
}
.air {
    -fx-background-image: url('/obkclient/img/Himmelsschlacht.jpg') ;
}
.sea {
    -fx-background-image: url('/obkclient/img/Unterwasserschlacht.jpg') ;
}
2
4/24/2014 10:42:52 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon