如何在 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