Javafx 简明教程
JavaFX - Reflection Effect
反射在科学上定义为当光线照射到表面(称为反射表面,如镜子)上时发生的现象,它会被反射回来。在这种情况下,这些光波线上的所有物体还会作为图像投影回来。你也可以在 JavaFX 应用程序中应用这个反射效果。
Reflection is scientifically defined as a phenomenon occurring when a light hits a surface (called a reflective surface like mirror), it gets thrown back. In such cases, all the objects present in the line of these light waves are also projected back as an image. You can also apply this reflection effect in JavaFX applications.
在 JavaFX 中将反射效果应用于一个节点时,会在该节点的底部添加其反射。
On applying the reflection effect to a node in JavaFX, a reflection of it is added at the bottom of the node.
包 javafx.scene.effect 中名为 Reflection 的类表示反射效果。该类包含四个属性,它们是 -
The class named Reflection of the package javafx.scene.effect represents the reflection effect. This class contains four properties, which are −
-
topOpacity − This property is of double type representing the top extremes opacity value of the reflection.
-
bottomOpacity − This property is of double type representing the bottom extremes opacity value of the reflection.
-
input − This property is of the type Effect and it represents an input to the reflection effect.
-
topOffset − This property is of double type representing the distance between the bottom of the input and the top of the reflection.
-
fraction −This property is of double type representing the fraction of input that is visible in the output. The range of the fraction value is 0.0 to 1.0.
Example
以下程序是一个演示反射效果的示例。在此,我们用 DARKSEAGREEN 颜色绘制文本“欢迎来到 Tutorialspoint”,并对其应用反射效果。
The following program is an example demonstrating the reflection effect. In here, we are drawing the text “Welcome to Tutorialspoint” filled with DARKSEAGREEN color and applying the reflection effect to it.
将代码保存到 ReflectionEffectExample.java 命名的文件中。
Save this code in a file with the name ReflectionEffectExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Reflection;
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 ReflectionEffectExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Text object
Text text = new Text();
//Setting font to the text
text.setFont(Font.font(null, FontWeight.BOLD, 40));
//setting the position of the text
text.setX(60);
text.setY(150);
//Setting the text to be embedded.
text.setText("Welcome to Tutorialspoint");
//Setting the color of the text
text.setFill(Color.DARKSEAGREEN);
//Instanting the reflection class
Reflection reflection = new Reflection();
//setting the bottom opacity of the reflection
reflection.setBottomOpacity(0.0);
//setting the top opacity of the reflection
reflection.setTopOpacity(0.5);
//setting the top offset of the reflection
reflection.setTopOffset(0.0);
//Setting the fraction of the reflection
reflection.setFraction(0.7);
//Applying reflection effect to the text
text.setEffect(reflection);
//Creating a Group object
Group root = new Group(text);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Reflection effect example");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls ReflectionEffectExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ReflectionEffectExample