本实例演示用JavaFx改变文字的字体,制造文字的阴影和倒影等效果。将会用到如下的三个类:
javafx.scene.text.Font
javafx.scene.effect.DropShadow
javafx.scene.effect.Reflection
本实例的代码如下:
import javafx.application.Application; import javafx.scene.layout.Pane; import javafx.scene.Scene; import javafx.scene.effect.DropShadow; import javafx.scene.effect.Reflection; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; public class FontEffect extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Pane pane = new Pane(); Scene scene = new Scene(pane, 550, 250, Color.WHITE); // Serif with drop shadow Text text2 = new Text(50, 50, "JavaFx Programing 冯斌"); Font serif = Font.font("Serif", 30); text2.setFont(serif); text2.setFill(Color.RED); DropShadow dropShadow = new DropShadow(); dropShadow.setOffsetX(2.0f); dropShadow.setOffsetY(2.0f); dropShadow.setColor(Color.rgb(50, 50, 50, .588)); text2.setEffect(dropShadow); pane.getChildren().add(text2); // SanSerif Text text3 = new Text(50, 100, "JavaFx Programing 冯斌"); Font sanSerif = Font.font("SanSerif", 30); text3.setFont(sanSerif); text3.setFill(Color.BLUE); pane.getChildren().add(text3); //Monospaced Text text4 = new Text(50, 150, "JavaFx Programing 冯斌"); Font monoFont = Font.font("Monospaced", 30); text4.setFont(monoFont); text4.setFill(Color.BLACK); pane.getChildren().add(text4); Reflection refl = new Reflection(); refl.setFraction(0.8f); text4.setEffect(refl); primaryStage.setTitle("FontEffect");// Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show();// Display the stage } }
运行结果如下:
说明:
1、第25行代码中的0.588代表的是透明度。
2、第44行代码中的0.8f表示的是可以看到80%的倒影。
时间: 2024-11-17 20:30:36