Table of contents
1.
Introduction
2.
Effects in JavaFX
3.
Bloom Class
3.1.
Properties
3.2.
Constructors
3.2.1.
Syntax
3.3.
Methods
4.
Sample Example
4.1.
Steps to Apply Bloom Effect
4.2.
Example
4.2.1.
Code
4.2.2.
Output
5.
Frequently Asked Questions
5.1.
How many life cycle stages are there in the JavaFX application?
5.2.
Does JavaFX run in browsers?
5.3.
Is JavaFX preferable to Swing?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Bloom Effect in JavaFX

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In general, the term "bloom" implies that the flower is in its fullest, brightest phase. But in the programming world, it's quite different. A computer graphics effect is known as bloom also referred to as light bloom or glow, replicates an imaging distortion of real-world cameras in video games, demos, and high dynamic range rendering (HDRR).
 

This article will help you understand the Bloom Effect in JavaFx. But Before understanding the bloom effect let’s first discuss what an effect is in JavaFX So, without any further ado let’s Start!

Effects in JavaFX

An effect is a filter that receives one or more graphical inputs, processes the inputs with an algorithm, and outputs the result. Effects are typically used on nodes to produce visually attractive user interfaces. Effects include things like shadowsblurwarpsglowsreflections, blending, and various lighting kinds, among others. 

Effects are features with conditions. If they are not present on a platform, they are applied to nodes and are disregarded. An effect is represented by an instance of the Effect class. The abstract base for all effect classes is the Effect class.

When used consecutively, some effects can be chained with additional effects. The first effect's output serves as the second effect's input, and so forth. Classes of effects that let

Bloom Class

The Bloom class is a component of JavaFX. Bloom is a creative effect that, in accordance with a programmable threshold, causes brighter areas of the input image to appear to glow. The javafx.scene.effect package is inherited by the Bloom class.

Properties

The Bloom class contains two properties:

  1. Input: This attribute, which belongs to the Effect class, is an input for the bloom effect. The setInput(Effect value) is used to set it.
     
  2. Threshold: The threshold value of brightness for the node's pixels is represented by this attribute, which is of the double type. Each pixel with luminance greater than or equal to this value gets illuminated. The threshold value's range is 0.0 to 1.0. The setThreshold(Double value) is used to set it.

Constructors

Bloom(): The Bloom() constructor generates a new Bloom Object.

Bloom(double th): This constructor adds a new instance of the Bloom effect with the given threshold.

Syntax

The Syntax for creating the bloom effect:

Bloom blm = new Bloom()

A Bloom object is created using the no-args constructor with a default threshold of 0.30. You can define the threshold value with the other constructor.

Methods

Let’s discuss some commonly used methods of the Bloom effect class in JavaFX.

  • getInput(): It obtains the value of the input property.
  • getThreshold(): It returns the threshold value that was set.
  • Input (Effect e): It sets the value of the input set property.
  • Threshold (double th):  It sets the effect's threshold value.

Sample Example

 We will see the example of the Bloom effect in JavaFX for your better grasp of the bloom effect. Before that let’s understand the steps of applying the bloom effect.

Steps to Apply Bloom Effect

Step 1: First of all, we need to import all the required libraries. Such as:

  • javafx.application.Application
  • javafx.scene.Group
  • javafx.scene.Scene
  • javafx.scene.image.Bloom
  • javafx.stage.Stage and many more as per your requirement.
     

Step 2: Create one class extending the Application Class. In order to provide implementation details, we also need to override the start method. This function generates a primaryStage object from a Stage object.

Step 3: Create an instance of the Bloom effect class which should be applied.

Step 4: Now set the properties of the bloom effect.

Step 5: Use the instance object to call the setEffect() function and pass the Bloom effect class object as an argument to that function.

Example

Here is one example of the Bloom Effect in JavaFX.

Code

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


public class Bloom_Example1 extends Application {
   @Override
   public void start(Stage stage) {
       //Creating a Text object
       Text text = new Text();


       //Setting font to the text
       text.setFont(Font.font(null, FontWeight.BOLD, 40));


       //setting the position of the text
       text.setX(150);
       text.setY(200);


       //Setting the text to be embedded.
       text.setText("Hey Ninja! Welcome!!");


       //Setting the colour to the text
       text.setFill(Color.PURPLE);


       //Instantiating the shape named Rectangle class
       Rectangle shape = new Rectangle();


       //Setting the position of our rectangle
       shape.setX(60.0f);
       shape.setY(80.0f);


       //Setting the width of rectangle
       shape.setWidth(500.0f);


       //Setting the height of rectangle
       shape.setHeight(150.0f);


       //Setting the color of the rectangle
       shape.setFill(Color.GOLDENROD);


       //Instantiating the Bloom class
       Bloom blm = new Bloom();


       //setting threshold for bloom
       blm.setThreshold(0.1);


       //Applying bloom effect to text
       text.setEffect(blm);


       //Creating a Group object
       Group root = new Group(shape, text);


       //Creating a scene object
       Scene scene = new Scene(root, 600, 300);


       //Setting title to the Stage
       stage.setTitle("Coding_Ninjas " +
               "Application");


       //Adding scene to the stage
       stage.setScene(scene);


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

Output

                                                                 Output Image of Bloom effect

Frequently Asked Questions

How many life cycle stages are there in the JavaFX application?

The life cycle of a JavaFX application has three stages.

  • Start()
  • Stop()
  • Init()

Does JavaFX run in browsers?

The Deployment Toolkit library is suggested for usage when launching a JavaFX application from a web page or embedding one there. The JavaScript API offered by the Deployment Toolkit makes it easier to deploy JavaFX applications on the web and enhances the user experience when the program launches.

Is JavaFX preferable to Swing?

Both approaches have a lot to offer Java developers who want to create plug-in UI components. Swing might provide developers an advantage with its extensive collection of UI components, but JavaFX can beat Swing when it comes to creating trendy, rich online applications.

Conclusion

In this blog, we ran through the Bloom effect in JavaFX. We discussed the Properties, Constructors, and Methods of the Bloom effect. We have also discussed sample example to understand the working of the Bloom effect in JavaFX.

We hope this blog increased your knowledge regarding the bloom effect in JavaFX. We recommend you to visit our articles on different topics of JavaFX, such as

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

 

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck! Keep coding and keep reading Ninja!!

Live masterclass