Javafx 简明教程

JavaFX - Box Blur Effect

一般来说,模糊表示变得模糊不清,对节点应用模糊效果将使该节点变得模糊不清。盒状模糊是 JavaFX 提供的一种模糊效果。在此效果中,使用一个简单的盒形滤镜对节点应用模糊。

In general, Blur means becoming unclear, on applying blur effect to a node it is made unclear. Box Blur is a kind of blur effect provided by JavaFX. In this effect, to apply blur to node, a simple box filter is used.

javafx.scene.effect 中名为 BoxBlur 的类表示盒状模糊效果,该类包含四个属性,它们是:

The class named BoxBlur of the package javafx.scene.effect represents the BoxBlur effect, this class contains four properties, which are −

  1. height − This property is of double type representing the vertical size of the effect.

  2. width − This property is of double type representing the horizontal size of the effect.

  3. input − This property is of the type effect and it represents an input to the BoxBlur effect.

  4. iterations − This property is of an integer type representing the number of iterations of the effect, which are to be applied on the node. This is done to improve its quality or smoothness.

Example

以下是演示盒状模糊效果的示例。在此,我们用 DARKSEAGREEN 颜色填充文本“Welcome to Tutorialspoint”,并对其应用盒状模糊效果。

Following is an example demonstrating the box blur effect. In here, we are drawing the text “Welcome to Tutorialspoint” filled with DARKSEAGREEN color and applying the Box Blur effect to it.

将此代码保存在名为 BoxBlurEffectExample.java 的文件中。

Save this code in a file with the name BoxBlurEffectExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BoxBlur;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class BoxBlurEffectExample extends Application {
   @Override
   public void start(Stage stage) {
      //Creating a Text object
      Text text = new Text();

      //Setting font to the text
      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 Tutorialspoint");

      //Setting the color of the text
      text.setFill(Color.DARKSEAGREEN);

      //Instantiating the BoxBlur class
      BoxBlur boxblur = new BoxBlur();

      //Setting the width of the box filter
      boxblur.setWidth(8.0f);

      //Setting the height of the box filter
      boxblur.setHeight(3.0f);

      //Setting the no of iterations
      boxblur.setIterations(3);

      //Applying BoxBlur effect to the text
      text.setEffect(boxblur);

      //Creating a Group object
      Group root = new Group(text);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

      //Setting title to the Stage
      stage.setTitle("Sample Application");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls BoxBlurEffectExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls BoxBlurEffectExample

Output

执行后,上述程序会生成如下所示的 JavaFX 窗口。

On executing, the above program generates a JavaFX window as shown below.

boxblur effect