lima
lima

Reputation: 45

How to put a shape in background in JavaFX?

I have been trying to put a background color in my app and someone taught me to create a shape (rectangle), set its color and put it in the background. I have created the rectangle, but I do not know how to set it as background. How can I do this using only JavaFX?

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import java.io.FileNotFoundException;
import java.io.FileInputStream;

public class Testes extends Application {

Stage j;
Scene c1, scene2;
Button b1;
Pane l1;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws FileNotFoundException {
    j = primaryStage;
    j.setMaximized(true);

    Image image = new Image(new FileInputStream("/home/jenisson/Imagens/mpba.jpg"));  
    ImageView imageView = new ImageView(image); 
    imageView.setX(600); 
    imageView.setY(10); 

    //setting the fit height and width of the image view 
    imageView.setFitHeight(90); 
    imageView.setFitWidth(150); 

    //Button 1
    Button b1 = new Button("+");
    b1.setLayoutX(10);
    b1.setLayoutY(110);

    //Button 2
    Button b2 = new Button("Consultar");
    b2.setLayoutX(50);
    b2.setLayoutY(140);

    Pane l1 = new Pane ();
    l1.getChildren().addAll(b1, b2);
    c1 = new Scene(l1);
    l1.getChildren().addAll(imageView);

    TextField consulta = new TextField();
    l1.setPadding(new Insets(10));
    l1.getChildren().add(consulta);
    consulta.setLayoutX(50);
    consulta.setLayoutY(110);

    Rectangle r = new Rectangle(0, 0, 1366, 1366);
    r.setFill(Color.WHITE);
    l1.getChildren().addAll(r);

    //Display scene 1 at first
    j.setScene(c1);
    j.setTitle("Controle de processos - 1ª Promotoria de Justiça");
    j.show();
}

}

With this code I am obviously putting the rectangle on top of all the buttons and other stuff. I must correct that.

Upvotes: 0

Views: 1549

Answers (1)

Sai Dandem
Sai Dandem

Reputation: 9869

If the purpose of the Rectangle is to just set some color as background, then you can set the default background color to Scene itself while initiating it. Scene has some constructors to set the fill. Something like,..

c1 = new Scene(l1, Color.WHITE);

Alternately from your example, you can set a StackPane as your root node to scene, and set the background color of the StackPane.

StackPane root = new StackPane();
root.setStyle("-fx-background-color:white;");
root.getChildren().add(l1);
c1 = new Scene(root);

Or if you have some other reasons to use a Shape, then you can set your Shape as the first child to the StackPane.

StackPane root = new StackPane();
Rectangle r = new Rectangle(0, 0, 1366, 1366);
r.setFill(Color.WHITE);
root.getChildren().addAll(r,l1);
c1 = new Scene(root);

I would recommend you to go through all the JavaFX layouts to get some basic idea. That will help in taking some good design desicions. :)

Upvotes: 2

Related Questions