Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX How to set scene background image

How can I set the background image of a scene?

like image 573
John Avatar asked Mar 16 '12 13:03

John


People also ask

How do I add an image to a JavaFX scene?

Create a FileInputStream representing the image you want to load. Instantiate the Image class bypassing the input stream object created above, as a parameter to its constructor. Instantiate the ImageView class. Set the image to it by passing above the image object as a parameter to the setImage() method.

How do I make a scene transparent in JavaFX?

TRANSPARENT is a Color (which is a Paint ) and is not an int . So setting the fill to the transparent color is OK. This solution should work fine unless a Control is involved in the scene, in which case the additional CSS setting discussed in other answers is also required.


2 Answers

One of the approaches may be like this:

1) Create a CSS file with name "style.css" and define an id selector in it:

#pane{     -fx-background-image: url("background_image.jpg");     -fx-background-repeat: stretch;        -fx-background-size: 900 506;     -fx-background-position: center center;     -fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);  } 

2) Set the id of the most top control (or any control) in the scene with value defined in CSS and load this CSS file into the scene:

  public class Test extends Application {      public static void main(String[] args) {         launch(args);     }      @Override     public void start(Stage primaryStage) {         StackPane root = new StackPane();         root.setId("pane");         Scene scene = new Scene(root, 300, 250);         scene.getStylesheets().addAll(this.getClass().getResource("style.css").toExternalForm());         primaryStage.setScene(scene);         primaryStage.show();     } } 

You can also give an id to the control in a FXML file:

<StackPane id="pane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="demo.Sample">     <children>     </children> </StackPane> 

For more info about JavaFX CSS Styling refer to this guide.

like image 188
Uluk Biy Avatar answered Sep 21 '22 14:09

Uluk Biy


I know this is an old Question

But in case you want to do it programmatically or the java way

For Image Backgrounds; you can use BackgroundImage class

BackgroundImage myBI= new BackgroundImage(new Image("my url",32,32,false,true),         BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,           BackgroundSize.DEFAULT); //then you set to your node myContainer.setBackground(new Background(myBI)); 

For Paint or Fill Backgrounds; you can use BackgroundFill class

BackgroundFill myBF = new BackgroundFill(Color.BLUEVIOLET, new CornerRadii(1),          new Insets(0.0,0.0,0.0,0.0));// or null for the padding //then you set to your node or container or layout myContainer.setBackground(new Background(myBF)); 

Keeps your java alive && your css dead..

like image 34
Elltz Avatar answered Sep 19 '22 14:09

Elltz