如何在 JavaFX 中向文字節點新增投影效果?
你可以使用 setEffect() 方法為 JavaFX 中的任何節點物件新增一個效果。此方法接受一個 Effect 類的物件並將其新增到當前節點。
javafx.scene.effect.DropShadow 類表示投影效果。此效果會在給定內容的後面呈現其陰影,並有指定的引數(顏色、偏移量、半徑)。
因此,為文字節點新增投影效果,請-
建立 Text 類,繞過基本 x、y 座標(位置)並將文字字串作為引數傳遞給建構函式。
設定所需屬性,如字型、輪廓等。
透過例項化DropShadow 類來建立投影效果。
使用 setEffect() 方法將建立的效果設定為文字節點。
最後,將建立的文字節點新增到 Group 物件中。
示例
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
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 DropShadowEffectExample 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 deep shadow effect to the text
DropShadow shadow = new DropShadow();
text.setEffect(shadow);
//Setting the stage
Group root = new Group(text);
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Drop Shadow Effect");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}輸出

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C #
MongoDB
MySQL
JavaScript
PHP