JavaFX 效果 - 反射



在 JavaFX 中將反射效果應用於節點時,會在節點底部新增其反射。

名為 Reflection 的類(位於 javafx.scene.effect 包中)表示反射效果。此類包含四個屬性,它們是 -

  • topOpacity - 此屬性為 double 型別,表示反射頂部邊緣的透明度值。

  • bottomOpacity - 此屬性為 double 型別,表示反射底部邊緣的透明度值。

  • input - 此屬性為 Effect 型別,表示反射效果的輸入。

  • topOffset - 此屬性為 double 型別,表示輸入底部和反射頂部之間的距離。

  • fraction - 此屬性為 double 型別,表示輸出中可見的輸入分數。fraction 值的範圍是 0.0 到 1.0。

示例

以下程式是一個演示反射效果的示例。在這裡,我們繪製了填充為 DARKSEAGREEN 顏色的文字“Welcome to Tutorialspoint”,並對其應用了反射效果。

將此程式碼儲存在名為 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 檔案。

javac ReflectionEffectExample.java 
java ReflectionEffectExample   

執行上述程式後,將生成一個如下所示的 JavaFX 視窗。

Reflection Effect
javafx_effects.htm
廣告

© . All rights reserved.