JavaFX - 光照效果



光照效果用於模擬光源發出的光。有不同型別的燈源,包括 - **點光源、遠光源** 和 **聚光燈**。

如果我們沒有提到任何光源,它將使用 JavaFX 的預設光源。

名為 `javafx.scene.effect` 包中的 `Lighting` 類表示光照效果,此類包含十個屬性,它們是:

  • **bumpInput** - 此屬性的型別為 Effect,它表示光照效果的可選凹凸貼圖輸入。

  • **contentInput** - 此屬性的型別為 Effect,它表示光照效果的內容輸入。

  • **diffuseConstant** - 此屬性的型別為 double,它表示光的漫反射常數。

  • **specularConstant** - 此屬性的型別為 double,它表示光的鏡面反射常數。

  • **specularExponent** - 此屬性的型別為 double,它表示光的鏡面反射指數。

  • **surfaceScale** - 此屬性的型別為 double,它表示光的表面比例因子。

示例

以下程式是一個演示 JavaFX 光照效果的示例。在這裡,我們在場景中繪製文字“歡迎來到 Tutorialspoint”和一個圓形。

我們將光照效果應用於這些。在這裡,由於我們沒有提到任何光源,因此 JavaFX 使用其預設光源。

將此程式碼儲存在名為 `LightingEffectExample.java` 的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.Lighting; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
         
public class LightingEffectExample 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(50); 
      
      //Setting the text to be embedded. 
      text.setText("Welcome to Tutorialspoint");       
      
      //Setting the color of the text 
      text.setFill(Color.RED);   
       
      //Drawing a Circle 
      Circle circle = new Circle();         
      
      //Setting the center of the circle
      circle.setCenterX(300.0f); 
      circle.setCenterY(160.0f); 
      
      //Setting the radius of the circle 
      circle.setRadius(100.0f); 
      
      //setting the fill color of the circle 
      circle.setFill(Color.CORNFLOWERBLUE);   
       
      //Instantiating the Lighting class  
      Lighting lighting = new Lighting(); 
  
      //Applying lighting effect to the text 
      text.setEffect(lighting);      
      
      //Applying lighting effect to the circle 
      circle.setEffect(lighting); 
         
      //Creating a Group object  
      Group root = new Group(text,circle);   
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Distant light 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 --module-path %PATH_TO_FX% --add-modules javafx.controls LightingEffectExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls LightingEffectExample   

輸出

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

Lighting Effect
廣告