Updated April 12, 2023
Definition of JavaFX Background
In JavaFX, Background is a class that helps in setting the background of a selected region. Each background is formed of different fills or different background images which cannot be null but can be empty. As this class is immutable, the same Background can be used in several regions. Moreover, every BackgroundFill is rendered in an order, which is followed by its mentioned BackgroundImage. The outsets of background explain any extension of the Region’s drawing area which is essential to account for every background drawing.
Syntax:
// Background creation
Background bg = new Background(background_fill);
Here, bg is the object of background class.
Constructors
Let us see the constructors of the background class in JavaFX.
- Background(BackgroundFill… f): A new background object will be created with fills mentioned.
- Background(BackgroundFill[] f, BackgroundImage[] im): A new background object will be created with fills as well as the background images mentioned.
- Background(BackgroundImage… i): A new background object will be created with the background image mentioned.
- Background(List f, List im): A new background object will be created with a list of fills as well as a list of background images mentioned.
Methods
Following are the commonly used methods of background class in JavaFX.
- getFills(): A list of all background fills is returned.
- getImages(): A list of all background images is returned.
- getOutsets(): A list of all background outset is returned.
- isEmpty(): This method checks whether the background is empty or not and returns the same.
- isFillPercentageBased(): This method checks whether the background fill is based on percentages or not and returns the same.
Examples of JavaFX Background
Now, it is time to see some sample programs on background class in JavaFX.
Example #1
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import java.io.*;
import javafx.geometry.*;
import javafx.scene.Group;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.paint.*;
//main class
public class BackgroundClassProgram extends Application {
// application launches here
public void start(Stage st)
{
try {
// set stage title
st.setTitle("Background creation. . .") ;
// label creation
Label lb = new Label("user name : ") ;
// text field creation
TextField tf = new TextField();
// column count has to be set based on the requirement
tf.setPrefColumnCount(10);
// button creation
Button bt = new Button("OK");
// add to hbox the created label lb, text field tf and button bt
HBox hb = new HBox(lb, tf, bt);
// spacing set
hb.setSpacing(10);
// alignment settng for the HBox
hb.setAlignment(Pos.CENTER);
// scene creation
Scene sc = new Scene(hb, 280, 280);
// background fill creation
BackgroundFill bf = new BackgroundFill(Color.RED,
CornerRadii.EMPTY , Insets.EMPTY);
//Background creation
Background bg = new Background(bf);
// set background
hb.setBackground(bg);
// scene setting
st.setScene(sc);
st.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
In this program, a button, label, and text field are created. As a background, red color is given and on executing the code, it gets displayed as shown above.
Example #2
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import java.io.*;
import javafx.geometry.*;
import javafx.scene.Group;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.paint.*;
//main class
public class BackgroundClassProgram extends Application {
// application launches here
public void start(Stage st)
{
try {
// set stage title
st.setTitle("Background creation. . .") ;
// label creation
Label lb = new Label("user name : ") ;
// text field creation
TextField tf = new TextField();
// column count has to be set based on the requirement
tf.setPrefColumnCount(10);
// button creation
Button bt = new Button("OK");
// add to hbox the created label lb, text field tf and button bt
HBox hb = new HBox(lb, tf, bt);
//spacing set
hb.setSpacing(10);
// alignment setting for the HBox
hb.setAlignment(Pos.CENTER);
// scene creation
Scene sc = new Scene(hb, 280, 280);
// input stream creation
FileInputStream inp = new FileInputStream("d:\\eduCBA-logo1.png");
//image creation
Image im = new Image(inp);
// create a background image
BackgroundImage bi = new BackgroundImage(im,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
// Background creation
Background bg = new Background(bi);
// set background
hb.setBackground(bg);
// scene setting
st.setScene(sc);
st.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
Similar to the above program, a text field, label, and a button gets displayed on executing the code. Here, a background image is also displayed.
Example #3
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.*;
import javafx.scene.image.*;
import java.io.*;
import javafx.geometry.*;
import javafx.scene.Group;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.paint.*;
import javafx.scene.shape.Circle;
//main class
public class BackgroundClassProgram extends Application {
// application launches here
public void start(Stage st)
{
try {
// set stage title
st.setTitle("Background creation. . .") ;
// label creation
Label lb = new Label("user name : ") ;
//Draw a Circle
Circle c = new Circle();
//Set the circle properties
c.setCenterX(310.0f);
c.setCenterY(125.0f);
c.setRadius(110.0f);
/// add circle
HBox hb = new HBox(c);
// set spacing
hb.setSpacing(10);
// hbox alignment setting
hb.setAlignment(Pos.CENTER);
// scene creation
Scene sc = new Scene(hb, 280, 280);
// background fill creation
BackgroundFill bf = new BackgroundFill(Color.RED,
CornerRadii.EMPTY , Insets.EMPTY);
// Background creation
Background bg = new Background(bf);
// set background
hb.setBackground(bg);
// scene setting
st.setScene(sc);
st.show();
}
//catch the exception
catch (Exception ec) {
System.out.println(ec.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// application launches here
launch(args);
}
}
Output:
Unlike the above programs, a circle gets displayed on executing the code with a red background color.
Conclusion
The background is a class that helps in setting the background of a selected region. In this article, different aspects such as syntax, constructors, methods, and examples of JavaFX background class is explained in detail.
Recommended Articles
This is a guide to JavaFX Background. Here we also discuss the Definition, Constructors, methods, and along with different examples. You may also have a look at the following articles to learn more –