JavaFX Effects - Box Blur


Advertisements

In general, Blur means becoming unclear, on applying blur effect to a node it is made unclear. Box Blur is a kind of blur effect provided by JavaFX. In this effect, to apply blur to node, a simple box filter is used.

The class named BoxBlur of the package javafx.scene.effect represents the BoxBlur effect, this class contains four properties, which are −

  • height − This property is of double type representing the vertical size of the effect.

  • width − This property is of double type representing the horizontal size of the effect.

  • input − This property is of the type effect and it represents an input to the BoxBlur effect.

  • iterations − This property is of an integer type representing the number of iterations of the effect, which are to be applied on the node. This is done to improve its quality or smoothness.

Example

Following is an example demonstrating the box blur effect. In here, we are drawing the text “Welcome to Tutorialspoint” filled with DARKSEAGREEN color and applying the Box Blur effect to it.

Save this code in a file with the name BoxBlurEffectExample.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.BoxBlur; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
         
public class BoxBlurEffectExample 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(60); 
      text.setY(150);         
      
      //Setting the text to be added. 
      text.setText("Welcome to Tutorialspoint");       
      
      //Setting the color of the text 
      text.setFill(Color.DARKSEAGREEN);
      
      //Instantiating the BoxBlur class 
      BoxBlur boxblur = new BoxBlur();      
      
      //Setting the width of the box filter 
      boxblur.setWidth(8.0f);  
      
      //Setting the height of the box filter 
      boxblur.setHeight(3.0f); 
      
      //Setting the no of iterations  
      boxblur.setIterations(3);       
               
      //Applying BoxBlur effect to the text 
      text.setEffect(boxblur);          
         
      //Creating a Group object  
      Group root = new Group(text);   
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Sample Application"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}      

Compile and execute the saved java file from the command prompt using the following commands.

javac BoxBlurEffectExample.java 
java BoxBlurEffectExample 

On executing, the above program generates a JavaFX window as shown below.

Box Blur Effect
javafx_effects.htm
Advertisements