JavaFX - 輝光效果



就像光暈效果一樣,輝光效果也會使給定的輸入影像發光。此效果使輸入畫素變得更亮。

名為 Glowjavafx.scene.effect 包中的類表示輝光效果。此類包含兩個屬性,即:

  • input - 此屬性的型別為 Effect,它表示輝光效果的輸入。

  • level - 此屬性的型別為 double;它表示輝光的強度。級別值的範圍為 0.0 到 1.0。

示例

以下程式是一個演示 JavaFX 輝光效果的示例。在這裡,我們使用 ImageImageView 類將以下影像(Tutorialspoint 徽標)嵌入 JavaFX 場景中。這將在位置 100、70 處完成,並且分別具有適合高度和適合寬度 200 和 400。

Glow Effect

對於此影像,我們應用輝光效果,級別值為 0.9。將此程式碼儲存在名為 GlowEffectExample.java 的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.Glow; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.stage.Stage; 
         
public class GlowEffectExample extends Application { 
   @Override 
   public void start(Stage stage) {               
      //Creating an image 
      Image image = new Image("https://tutorialspoint.tw/green/images/logo.png");
   
      //Setting the image view 
      ImageView imageView = new ImageView(image); 
      
      //setting the fit width of the image view 
      imageView.setFitWidth(200);  
      
      //Setting the preserve ratio of the image view 
      imageView.setPreserveRatio(true);       
       
      //Instantiating the Glow class 
      Glow glow = new Glow(); 
      
      //setting level of the glow effect 
      glow.setLevel(0.9); 
      
      //Applying bloom effect to text 
      imageView.setEffect(glow);          
         
      //Creating a Group object  
      Group root = new Group(imageView);   
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Sample Application"); 
         
      //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 GlowEffectExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls GlowEffectExample

輸出

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

Glow Effect Example
廣告

© . All rights reserved.