JavaFX: How to Make Things Blurry

By Doug Lowe

JavaFX provides three effects classes that can make an object blurry. Each of these classes uses a different method for applying the blur:

  • BoxBlur: Applies a simple and efficient blurring technique in which each pixel in the blurred region is calculated by averaging its neighboring pixels in the input image.

  • GaussianBlur: Uses a Gaussian blurring algorithm, which is more accurate but less efficient than a box blur.

  • MotionBlur: Blurs the shape directionally, creating the effect of motion.

This table shows the members of the three blur effect classes.

The BoxBlur, GaussianBlur, and MotionBlur Classes
Constructors Explanation
BoxBlur() Creates a new BoxBlur effect with default settings.
GaussianBlur() Creates a new GaussianBlur effect with default settings.
MotionBlur() Creates a new MotionBlur effect with default settings.
BoxBlur Methods Explanation
void setHeight(double value) Sets the vertical size of the blur effect.
void setWidth(double value) Sets the horizontal size of the blur effect.
void setIterations(int value) Sets the number of times the effect should be repeated. The
default is 1.
GaussianBlur Methods Explanation
void setRadius(double value) The radius of the blur effect. The default is 10.0.
MotionBlur Methods Explanation
void setAngle(double value) The angle of the motion effect, in degrees. The default is
0.0.
void setRadius(double value) The radius of the blur effect. The default is 10.

The figure shows the effect of each of these blur types on a text shape. The following code was used to create these three blurs:

Text t1 = new Text("BoxBlur");
t1.setFont(new Font("Times New Roman", 60));
t1.setFill(Color.LIGHTGRAY);
t1.setStroke(Color.BLACK);
t1.setEffect(new BoxBlur());
Text t2 = new Text("GaussianBlur");
t2.setFont(new Font("Times New Roman", 60));
t2.setFill(Color.LIGHTGRAY);
t2.setStroke(Color.BLACK);
t2.setEffect(new GaussianBlur());
Text t3 = new Text("MotionBlur");
t3.setFont(new Font("Times New Roman", 60));
t3.setFill(Color.LIGHTGRAY);
t3.setStroke(Color.BLACK);
t3.setEffect(new MotionBlur());

As you can see, default values were used for each of the blurs. If you wish, you can use the methods listed in the table to tweak the appearance of the blur effects.

image0.jpg