I actual get problem to set an background image to a scene. I tried it in different ways:
public void btn_create_event() throws IOException, Exception{
Stage curstage = (Stage) config_btn.getScene().getWindow();
Stage stage = new Stage();
FieldLayerController flc = new FieldLayerController();
// flc.buildField("seeschlacht_background.jpg");
flc.buildField();
flc.buildScene();
flc.buildCamera();
// String image = FieldLayerController.class.getResource("seeschlacht_background.jpg").toExternalForm();
// flc.root.setStyle("-fx-background-image: url('" + image + "'); "
// + "-fx-background-position: center center; "
// + "-fx-background-repeat: stretch;"
// + "-fx-background-color:transparent;"
// );
// flc.root.setStyle("-fx-background-image: url('seeschlacht_background.jpg')");
Scene scene = new Scene(flc.root, 1024, 768, true);
// scene.getStylesheets().addAll(this.getClass().getResource("Seeschlacht.css").toExternalForm());
flc.handleKeyboard(scene, (Node)flc.world);
flc.handleMouse(scene, (Node)flc.world);
scene.setCamera(flc.camera);
stage.setScene(scene);
stage.show();
curstage.close();
}
These are the Methode from the FieldLayerController which are ncessary to build the window:
public void buildScene() {
System.out.println("buildScene");
root.getChildren().add(world);
}
public void buildField(){
final PhongMaterial redMaterial = new PhongMaterial();
redMaterial.setDiffuseColor(Color.DARKRED);
redMaterial.setSpecularColor(Color.RED);
final PhongMaterial whiteMaterial = new PhongMaterial();
whiteMaterial.setDiffuseColor(Color.WHITE);
whiteMaterial.setSpecularColor(Color.LIGHTBLUE);
final PhongMaterial greyMaterial = new PhongMaterial();
greyMaterial.setDiffuseColor(Color.GREEN);
greyMaterial.setSpecularColor(Color.DARKGREEN);
final PhongMaterial lightgreyMaterial = new PhongMaterial();
greyMaterial.setDiffuseColor(Color.LIGHTGREY);
greyMaterial.setSpecularColor(Color.GREY);
//Image img = new Image(img_path);
//BackgroundImage back_img = new BackgroundImage(img, BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
GraficForm fieldGrafikForm = new GraficForm();
GraficForm areaGrafikForm = new GraficForm();
ArrayList<Box> ab = new ArrayList<Box>();
for (int x = -2; x < 2; x++) {
for (int z = -2; z < 2; z++) {
Box box = new Box(50, 0, 50);
box.setId("X" + (x + 2));
box.setTranslateX(x + 25 + (x * 50));
box.setTranslateZ(z + 25 + (z * 50));
if ((x % 2 == 0 & z % 2 != 0) | (x % 2 != 0 & z % 2 == 0)) {
box.setMaterial(redMaterial);
} else {
box.setMaterial(lightgreyMaterial);
}
ab.add(box);
}
}
fieldGrafikForm.getChildren().add(areaGrafikForm);
areaGrafikForm.getChildren().addAll(ab);
BattleFieldGroup.getChildren().add(fieldGrafikForm);
world.getChildren().addAll(BattleFieldGroup);
}
So what can i do to set a external img file (the file is in the same Path of the other files and the css-file too) to the Window i create with the FieldLayerController?
Using JavaFX 8 and NetBeans 8. I read already the threads to this topic here in stackoverflow:
- => JavaFX display scene background image
- => JavaFX How to set scene background image
- => JavaFx set external background image programmatically
And so on...