如何在 JavaFX 中向文字節點新增內陰影效果?


你可以使用 setEffect() 方法向 JavaFX 中的任何節點物件新增效果。此方法接受 Effect 類的物件並將其新增到當前節點。

javafx.scene.effect.InnerShadow 類表示內陰影效果。此效果利用指定引數(顏色、偏移量、半徑)在其邊緣內部呈現給定內容的陰影。

為文字節點新增反射效果:

  • 例項化 Text 類,將基本 x、y 座標(位置)和文字字串作為建構函式的引數傳入。

  • 設定所需的屬性,如字型、描邊等。

  • 透過例項化 InnerShadow 類來建立內陰影效果。

  • 使用 setEffect() 方法將建立的效果設定為文字節點。

  • 最後,將建立的文字節點新增到 Group 物件。

示例

import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.InnerShadow;
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 InnerShadowEffectExample extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a text object
      String str = "Tutorialspoint";
      Text text = new Text(30.0, 100.0, str);
      //Setting the font
      Font font = Font.font("Brush Script MT", FontWeight.BOLD, 110);
      text.setFont(font);
      //Setting color of the text
      text.setFill(Color.BLUEVIOLET);
      //Creating the inner shadow effect
      InnerShadow shadow = new InnerShadow();
      shadow.setOffsetX(8.0);
      shadow.setOffsetY(8.0);
      //Setting the effect to the text
      text.setEffect(shadow);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Inner Shadow Effect");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於:2020 年 5 月 16 日

182 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.