如何在 JavaFX 中给文本节点添加模糊效果?
你可以使用 setEffect() 方法给 JavaFX 的任何节点对象添加效果。此方法接受 Effect 类的一个对象,并将其添加到当前节点。
javafx.scene.effect.GaussianBlur.GaussianBlur 类代表模糊效果,其在内部使用高斯卷积核。因此,要给文本节点添加模糊效果,可以:
实例化 Text 类,从构造函数传递基本 x、y 坐标(位置)和文本字符串作为参数。
设置所需的属性,如字体、笔触等。
通过实例化 GaussianBlur 类创建模糊效果。
使用 setEffect() 方法给文本节点设置创建的效果。
最后,将创建的文本节点添加到 Group 对象。
示例
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class TextBlurEffect extends Application {
public void start(Stage stage) throws FileNotFoundException {
//Creating a text object
String str = "Welcome to Tutorialspoint";
Text text = new Text(30.0, 80.0, str);
//Setting the font
Font font = Font.font("Brush Script MT", FontWeight.BOLD,
FontPosture.REGULAR, 65);
text.setFont(font);
//Setting the color of the text
text.setFill(Color.BROWN);
//Setting the width and color of the stroke
text.setStrokeWidth(2);
text.setStroke(Color.BLUE);
//Setting the blur effect to the text
GaussianBlur blur = new GaussianBlur();
text.setEffect(blur);
//Setting the stage
Group root = new Group(text);
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Blur Effect");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP