Open In App

JavaFX | MotionBlur Class

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. MotionBlur(): Creates a new object of MotionBlur.
  2. 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:

  1. 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 it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.image.*;
    import javafx.scene.effect.*;
    import java.io.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
      
    public class motion_blur_1 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("MotionBlur Example");
      
            // create a input stream
            FileInputStream input = new FileInputStream("D:\\GFG.png");
      
            // create a image
            Image image = new Image(input);
      
            // create a image View
            ImageView imageview = new ImageView(image);
      
            // create a Motion blur effect
            MotionBlur motion_blur = new MotionBlur();
      
            // set effect
            imageview.setEffect(motion_blur);
      
            // create a VBox
            VBox vbox = new VBox(imageview);
      
            // create a scene
            Scene scene = new Scene(vbox, 200, 200);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Input Image:

    Output:

  2. 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 radius
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.image.*;
    import javafx.scene.effect.*;
    import java.io.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
      
    public class motion_blur_2 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("MotionBlur Example");
      
            // create a input stream
            FileInputStream input = new FileInputStream("D:\\GFG.png");
      
            // create a image
            Image image = new Image(input);
      
            // create a image View
            ImageView imageview = new ImageView(image);
      
            // create a Motion blur effect
            MotionBlur motion_blur = new MotionBlur();
      
            // set Radius
            motion_blur.setRadius(25.0f);
      
            // set angle
            motion_blur.setAngle(400.0f);
      
            // set effect
            imageview.setEffect(motion_blur);
      
            // create a VBox
            VBox vbox = new VBox(imageview);
      
            // create a scene
            Scene scene = new Scene(vbox, 200, 200);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Input Image:

    Output:

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



Last Updated : 20 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads