當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaFX 類 Reflection用法及代碼示例

反射類是JavaFX的一部分。 Reflection類用於在輸入值的實際圖像下方添加反射圖像。反射圖像將不響應鼠標事件或輸入中的包含方法。

該類的構造函數:

  1. Reflection():創建一個新的反射對象。
  2. Reflection(double topOffset, double fraction, double topOpacity, double bottomOpacity):使用指定的topOffset,fraction,topOpacity和bottomOpacity創建一個新的Reflection對象。

常用方法:


方法 說明
getBottomOpacity() 返回bottomOpacity的值
getTopOpacity() 返回topOpacity的值
getFraction() 返回反射圖像與真實圖像的分數
getTopOffset() 返回頂部偏移量的值
getInput() 返回屬性輸入的值
setBottomOpacity(double v) 設置bottomOpacity的值
setTopOpacity(double v) 設置topOpacity的值
setFraction(double v) 設置反射圖像占真實圖像的比例
setTopOffset(double v) 設置頂部偏移的值
setInput(Effect v) 設置屬性輸入的值

以下示例程序旨在說明Reflection類的用法:

  1. Java程序,使用反射類將反射添加到圖像:在此程序中,將創建FileInputStream並將圖像作為文件的輸入。使用文件輸入流中的輸入創建名為image的Image。從圖像創建圖像視圖對象,並將其添加到VBox。然後將VBox添加到場景,並將場景添加到舞台。創建反射效果,並使用setEffect()函數將其設置為圖像視圖。
    // Java program to add a reflection to  
    // the image using the reflection class 
    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.stage.Stage; 
    import javafx.scene.image.*; 
    import javafx.scene.effect.*; 
    import java.io.*; 
    import javafx.event.ActionEvent; 
    import javafx.event.EventHandler; 
    import javafx.scene.Group; 
      
    public class reflection_1 extends Application { 
      
        // launch the application 
        public void start(Stage stage) throws Exception 
        { 
      
            // set title for the stage 
            stage.setTitle("reflection example"); 
      
            // create a input stream 
            FileInputStream input = new FileInputStream("D:\\GFG.png"); 
      
            // create a image 
            Image image = new Image(input); 
      
            // create a image View 
            ImageView imageview = new ImageView(image); 
      
            // create a reflection effect 
            Reflection reflection = new Reflection(); 
      
            // set effect 
            imageview.setEffect(reflection); 
      
            // create a VBox 
            VBox vbox = new VBox(imageview); 
      
            // create a scene 
            Scene scene = new Scene(vbox, 200, 200); 
      
            // set the scene 
            stage.setScene(scene); 
      
            stage.show(); 
        } 
      
        // Main Method 
        public static void main(String args[]) 
        { 
            // launch the application 
            launch(args); 
        } 
    }

    輸出:

  2. Java程序使用反射類向圖像添加反射並設置頂部偏移,頂部不透明度,底部不透明度和分數圖像將作為反射出現:在此程序中,將創建FileInputStream並將圖像作為文件的輸入。使用來自文件輸入流的輸入來創建名為image的圖像。從圖像創建圖像視圖對象,並將其添加到VBox。然後將VBox添加到場景,並將場景添加到舞台。創建反射效果,並使用setEffect()函數將其設置為圖像視圖。底部不透明度,頂部不透明度,頂部偏移和分數分別使用setBottomOpacity(),setTopOpacity(),setFraction()和setTopOffset()函數進行設置。
    // Java program to add a reflection to the image 
    // using the reflection class and set the top  
    // offset, top opacity bottom opacity and fraction 
    // of image which will appear as reflection 
    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.stage.Stage; 
    import javafx.scene.image.*; 
    import javafx.scene.effect.*; 
    import java.io.*; 
    import javafx.event.ActionEvent; 
    import javafx.event.EventHandler; 
    import javafx.scene.Group; 
      
    public class reflection_2 extends Application { 
      
        // launch the application 
        public void start(Stage stage) throws Exception 
        { 
      
            // set title for the stage 
            stage.setTitle("reflection example"); 
      
            // create a input stream 
            FileInputStream input = new FileInputStream("D:\\GFG.png"); 
      
            // create a image 
            Image image = new Image(input); 
      
            // create a image View 
            ImageView imageview = new ImageView(image); 
      
            // create a reflection effect 
            Reflection reflection = new Reflection(); 
      
            // set fraction 
            reflection.setFraction(0.6); 
      
            // set top Opacity 
            reflection.setTopOpacity(0.3); 
      
            // set bottom Opacity 
            reflection.setBottomOpacity(0.1); 
      
            // set top offset 
            reflection.setTopOffset(0.5); 
      
            // set effect 
            imageview.setEffect(reflection); 
      
            // create a VBox 
            VBox vbox = new VBox(imageview); 
      
            // create a scene 
            Scene scene = new Scene(vbox, 200, 200); 
      
            // set the scene 
            stage.setScene(scene); 
      
            stage.show(); 
        } 
      
        // Main Method 
        public static void main(String args[]) 
        { 
      
            // launch the application 
            launch(args); 
        } 
    }

    輸出:

注意:以上程序可能無法在在線IDE中運行。請使用離線編譯器。

參考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/Reflection.html



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | Reflection Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。