首页 > Java > java教程 > 正文

如何在JavaFX中为文本节点添加模糊效果?

王林
发布: 2023-08-19 12:05:34
转载
1469 人浏览过

您可以使用setEffect()方法为JavaFX中的任何节点对象添加效果。此方法接受Effect 类的对象,并将其添加到当前节点。

javafx.scene.effect.GaussianBlur.GaussianBlur类表示内部使用高斯卷积核的模糊效果。因此,要将模糊效果添加到文本节点中:

  • 通过将基本的x、y坐标(位置)和文本字符串作为构造函数的参数来实例化Text类。

  • 设置所需的属性,如字体、描边等。

  • 通过实例化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);
   }
}
登录后复制

输出

如何在JavaFX中为文本节点添加模糊效果?

以上是如何在JavaFX中为文本节点添加模糊效果?的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!