如何在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 {
//创建一个文本对象
String str = "Welcome to Nhooo";
Text text = new Text(30.0, 80.0, str);
//设置字体
Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 65);
text.setFont(font);
//设置文字的颜色
text.setFill(Color.BROWN);
//设置笔触的宽度和颜色
text.setStrokeWidth(2);
text.setStroke(Color.BLUE);
//为文本设置深阴影效果
DropShadow shadow = new DropShadow();
text.setEffect(shadow);
//设置舞台
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);
}
}输出结果