您可以使用seteffect()方法为javafx中的任何节点对象添加效果。此方法接受effect 类的对象,并将其添加到当前节点。
javafx.scene.effect.GaussianBlur.GaussianBlur类表示内部使用高斯卷积核的模糊效果。因此,要将模糊效果添加到文本节点中:
通过将基本的x、y坐标(位置)和文本字符串作为构造函数的参数来实例化Text类。
设置所需的属性,如字体、描边等。
通过实例化GaussianBlur 类来创建模糊效果。
立即学习“Java免费学习笔记(深入)”;
使用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);
}
}
以上就是如何在JavaFX中为文本节点添加模糊效果?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号