首頁  >  文章  >  Java  >  如何在JavaFX中為文字節點添加模糊效果?

如何在JavaFX中為文字節點添加模糊效果?

王林
王林轉載
2023-08-19 12:05:341470瀏覽

您可以使用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刪除