Quick Contact

    Hibernate

    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

    // JavaFX program that demonstrates the working of background class.

    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
    TextFieldtf = 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
    HBoxhb = 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);
    }
    }
    

    Example #2

    Code:

    // JavaFX program that demonstrates the working of background class

    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
    TextFieldtf = 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
    HBoxhb = 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
    FileInputStreaminp = 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);
    }
    }
    

    Example #3

    // JavaFX program that demonstrates the working of background class.

    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
    HBoxhb = 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);
    }
    }
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.