JavaFX setFill() Method

Sheeraz Gul Jul 07, 2022
JavaFX setFill() Method

The setFill() method is used to fill the colors in shapes and other elements in JavaFX. This tutorial demonstrates using the setFill() method in JavaFX.

JavaFX setFill() Method

The setFill() method can fill uniform image patterns and gradient patterns to the shapes in JavaFX. To use the setFill() method we need the JavaFX.scene.paint package.

The setFill() can be used to fill the colors to the classes like Shape, Text etc.

Syntax:

//Setting color to the text
Color color = new Color.Red
text.setFill(color);

The above syntax uses to Color class from the paint package to specify the color and fill it to the text using the setFill() method. The following are the steps to fill color into the scene using the setFill method.

  1. Create a class that extends the Application class and implements the start() method.
  2. Create a group by instantiating the Group class.
  3. Create a scene by instantiating the Scene class and passing the group to it.
  4. Fill the colors to the scene using the setFill method.
  5. Create a shape, circle, rectangle, etc., and add the shape to the group.
  6. Pass the scene to the stage and display the stage by the Show method.
  7. Launch the application in the main method.

Let’s have an example based on the steps above.

Example Code:

package delftstack;

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

public class JavaFX_SetFill extends Application {
    @Override
    public void start(Stage DemoStage) {
        Group DemoGroup = new Group();
        Scene DemoScene = new Scene(DemoGroup, 200, 150);
        DemoScene.setFill(Color.LIGHTBLUE);

        Circle DemoCircle = new Circle(100, 100, 80, Color.RED);

        DemoGroup.getChildren().add(DemoCircle);
        DemoStage.setScene(DemoScene);
        DemoStage.show();
    }

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

The code above will create a scene with a circle shape. It uses the setFill method to fill color to the scene.

Output:

JavaFX SetFill

Let’s try to fill the color to the shape and text using the setFill() method.

Example Code:

package delftstack;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class JavaFX_SetFill extends Application {
    @Override
    public void start(Stage DemoStage) {
        //Draw a Square
        Rectangle Square = new Rectangle();

        //Set the properties of the Square
        Square.setX(200.0f);
        Square.setY(200.0f);
        Square.setWidth(300.0f);
        Square.setHeight(300.0f);

        //Set color to the Square
        Square.setFill(Color.LIGHTBLUE);

        //Set the stroke width
        Square.setStrokeWidth(3);

        //Set color to the stroke
        Square.setStroke(Color.LIGHTGREEN);

        //Draw a text
        Text DemoText = new Text("This is a colored Square");

        //Set the font of the text
        DemoText.setFont(Font.font("Edwardian Script ITC", 60));

        //Set the position of the text
        DemoText.setX(155);
        DemoText.setY(50);

        //Set color to the text
        DemoText.setFill(Color.BEIGE);
        DemoText.setStrokeWidth(2);
        DemoText.setStroke(Color.LIGHTBLUE);

        //Create a Group object
        Group Group_Root = new Group(Square, DemoText);

        //Create a scene object
        Scene DemoScene = new Scene(Group_Root, 600, 300);

        //Set title to the Stage
        DemoStage.setTitle("SetFill Example");

        //Add scene to the stage
        DemoStage.setScene(DemoScene);

        //Display the contents of the stage
        DemoStage.show();
    }
    public static void main(String args[]){
        launch(args);
    }
}

The code above will create a square and a text and then use the setfill method to fill the square with color. It also uses the setStroke method for the borders.

Output:

JavaFX SetFill Shape

The setFill method can also fill an image gradient to the shape or text.

Example Code:

package delftstack;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class JavaFX_SetFill extends Application {
    @Override
    public void start(Stage DemoStage) throws FileNotFoundException {
        //Draw a Square
        Rectangle Square = new Rectangle();

        //Set the properties of the Square
        Square.setX(200.0f);
        Square.setY(200.0f);
        Square.setWidth(300.0f);
        Square.setHeight(300.0f);

        //Draw a text
        Text DemoText = new Text("This is a Gradient Square");

        //Set the font of the text
        DemoText.setFont(Font.font("Edwardian Script ITC", 60));

        //Set the position of the text
        DemoText.setX(155);
        DemoText.setY(50);

        //Set the image pattern
        Image DemoImage = new Image(new FileInputStream("Delftstack.png"));
        ImagePattern Image_Gradient = new ImagePattern(DemoImage, 80, 80, 160, 160, false);

        //Set the linear gradient to the Square
        Square.setFill(Image_Gradient);

        //Create a Group object
        Group Group_Root = new Group(Square, DemoText);

        //Create a scene object
        Scene DemoScene = new Scene(Group_Root, 600, 300);

        //Set title to the Stage
        DemoStage.setTitle("SetFill Example");

        //Add scene to the stage
        DemoStage.setScene(DemoScene);

        //Display the contents of the stage
        DemoStage.show();
    }
    public static void main(String args[]){
        launch(args);
    }
}

The code above will fill the image gradient to the square shape.

Output:

JavaFX SetFill Gradient

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java JavaFX