Skip to content
Related Articles

Related Articles

JavaFx | GaussianBlur Class
  • Last Updated : 20 Aug, 2018

GaussianBlur is a part of JavaFX. GaussianBlur is an implementation of a blur effect using a Gaussian convolution kernel, with a configurable radius.

Constructors of the class:

  1. GaussianBlur(): Creates an new object of GaussianBlur.
  2. GaussianBlur(double r): Creates a new Object of GaussianBlur with a specified radius.

Commonly Used Methods:

MethodExplanation
getRadius()Return the value of the property radius.
getInput()Return the value of the property input.
setInput(Effect val)Sets the value of the property input.
setRadius(double val)Sets the value of the property radius.

Below programs illustrate the use of GaussianBlur class:

  1. Java program to import an image and add GaussianBlur 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 GaussianBlur 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 Gaussian 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 gaussian_blur_1 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("GussianBlur 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 gaussian blur effect
            GaussianBlur gaussian_blur = new GaussianBlur();
      
            // set effect
            imageview.setEffect(gaussian_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 GaussianBlur effect to it with a specified 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 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 GaussianBlur effect is created with a specified level passed as parameters and the effect is set to the image view using setEffect() function. The radius of the GaussianBlur is specified using the setRadius() function.




    // Java program to import an image and
    // add Gaussian Blur effect to it with
    // a specified 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 gaussian_blur_2 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("GaussianBlur 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 gaussian blur effect
            GaussianBlur gaussian_blur = new GaussianBlur();
      
            // set radius for gaussian blur
            gaussian_blur.setRadius(20.0f);
      
            // set effect
            imageview.setEffect(gaussian_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/GaussianBlur.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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :