Table of contents
1.
Introduction
1.1.
Constructors
1.2.
Properties
1.3.
Usage
2.
Implementation
3.
Frequently Asked Questions
3.1.
What is the motion blur effect?
3.2.
How do you change the background color in JavaFX?
3.3.
What is the best approach to use JavaFX in your browser?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX MotionBlur Effect

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

Introduction

The JavaFX MotionBlur class is used to apply various motion blur effects. MotionBlur is a JavaFX effect that blurs the nodes, much like Gaussian Effect does. It also employs a Gaussian Convolution Kernel, which contributes to the blurring effect. The main difference between Gaussian Effect and Motion Blur is that the Gaussian Convolution Kernel is used at a specific angle with the Gaussian Effect.

As the name implies, this effect causes the node to appear to be in motion. The motion blur effect is represented by javafx.scene.effect.MotionBlur class. This class must be instantiated in order to produce the desired effect.

Constructors

There are two different types of constructors available in the motion blur:

  1. public MotionBlur(): Creates a new MotionBlur object with the default values.
     
  2. public MotionBlur(double angle, double radius): Creates a new MotionBlur object with the specified angle and radius.

Properties

The table below describes the MotionBlur class properties as well as the setter methods.

Table for properties

Usage

This program serves as an example of the Motion Blur Effect. In this case, we're drawing the text "Welcome to Coding Ninjas" in Grey and applying the Motion Blur Effect at a 45-degree angle to it.

Put this code in a file called MotionBlurEffectExample.java.

Implementation

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.Group; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.stage.Stage; 
import javafx.scene.text.FontWeight; 
import javafx.scene.effect.MotionBlur; 
import javafx.scene.text.Text; 
         
public class MotionBlurEffectExample extends Application { 
   @Override 
   public void start(Stage stage) {       
      //Create a Text object 
      Text text = new Text();       
      
      //Setting the font 
      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 Coding Ninjas");
      
      //Set the color of the text 
      text.setFill(Color.GREY);  
       
      //Instantiating the MotionBlur class 
      MotionBlur motionBlur = new MotionBlur();       
      
      //Setting the radius and angle to the effect
      motionBlur.setRadius(10.5);  
      motionBlur.setAngle(45);        
      
      //Applying MotionBlur effect to text
      text.setEffect(motionBlur);        
         
      //Creating a Group object  
      Group root = new Group(text);   
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      stage.setTitle("Imageinput effect example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displays the content
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}
You can also try this code with Online Java Compiler
Run Code


How to execute the above program?

To execute the above program. You can follow the given steps:

  1. Open the terminal in the same folder where you have saved your source code.
     
  2. Enter the below code and hit enter.
    javac --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java
    After you hit enter a new file will automatically be generated namely yourFilename.class
     
  3. Now, enter the below command and hit enter to execute the above program.
    java --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java
     
  4. You have successfully executed the JavaFX program.
     

Output

Output

Frequently Asked Questions

What is the motion blur effect?

The streaking of moving objects caused by quick movement or extended exposure periods is the motion blur effect.
 

How do you change the background color in JavaFX?

Setting the JavaFX Scene background color or image is done by invoking Scene's setFill() method, which accepts a color, gradient, or image pattern.
 

What is the best approach to use JavaFX in your browser?

For embedding a JavaFX application inside a web page or running it from within a web browser, the Deployment Toolkit package is suggested.

Conclusion

In this article, we have extensively discussed the MotionBlur Class present inside JavaFX. We have seen different constructors to initialize the MotionBlur effect. Also, we have seen various methods available inside the class along with the implementation.

If you think this blog has helped you enhance your knowledge about JavaFx path transition and if you would like to learn more, check out our articles JavaFXJavaFX Rotate TransitionJavaFX Introduction, and many more in our Library.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass