JavaFX | MotionBlur Class
MotionBlur is a part of JavaFX. MotionBlur class implements motion blur effect using a Gaussian convolution kernel, with a configurable radius and angle. MotionBlur class inherits Effect class.
Constructors of the class:
- MotionBlur(): Creates a new object of MotionBlur.
- MotionBlur(double a, double r): Creates a new object of MotionBlur with specified angle and radius.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getAngle() | Return the angle of the MotionBlur object |
| getRadius() | Return the radius of the MotionBlur object |
| getInput() | Return the input of the MotionBlur object |
| setAngle(double v) | Sets the angle of the MotionBlur object |
| getRadius(double v) | Set the radius of the MotionBlur object |
| setInput(Effect v) | Sets the input of the MotionBlur object |
Below programs illustrate the use of MotionBlur class:
- Java program to import an image and add Motion Blur effect to it: In this program a FileInputStream is created and an image is taken as input from a file. Image named image is created using the input from the file input stream. From the image, an image view object is created and it is added to the VBox. The VBox is then added to the scene and the scene is added to the stage. A MotionBlur effect is created with a specified level passed as parameters and the effect is set to the image view using setEffect() function.
// Java program to import an image and// add Motion Blur effect to itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.image.*;importjavafx.scene.effect.*;importjava.io.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;publicclassmotion_blur_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage)throwsException{// set title for the stagestage.setTitle("MotionBlur Example");// create a input streamFileInputStream input =newFileInputStream("D:\\GFG.png");// create a imageImage image =newImage(input);// create a image ViewImageView imageview =newImageView(image);// create a Motion blur effectMotionBlur motion_blur =newMotionBlur();// set effectimageview.setEffect(motion_blur);// create a VBoxVBox vbox =newVBox(imageview);// create a sceneScene scene =newScene(vbox,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Input Image:
- Java program to import an image and add Motion Blur effect to it with specified angle and radius: In this program a FileInputStream is created and an image is taken as input from a file. Image named image is created using the input from the file input stream. From the image, an image view object is created and it is added to the VBox. The VBox is then added to the scene and the scene is added to the stage. A MotionBlur effect is created with a specified level passed as parameters and the effect is set to the image view using setEffect() function. The radius and the angle for the motion blur is specified using the setRadius() and setAngle() function.
// Java program to import an image and// add Motion Blur effect to it with// specified angle and radiusimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.image.*;importjavafx.scene.effect.*;importjava.io.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;publicclassmotion_blur_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage)throwsException{// set title for the stagestage.setTitle("MotionBlur Example");// create a input streamFileInputStream input =newFileInputStream("D:\\GFG.png");// create a imageImage image =newImage(input);// create a image ViewImageView imageview =newImageView(image);// create a Motion blur effectMotionBlur motion_blur =newMotionBlur();// set Radiusmotion_blur.setRadius(25.0f);// set anglemotion_blur.setAngle(400.0f);// set effectimageview.setEffect(motion_blur);// create a VBoxVBox vbox =newVBox(imageview);// create a sceneScene scene =newScene(vbox,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Input Image:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/MotionBlur.html
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.





