JavaFX - 方框模糊效果



一般來說,模糊意味著變得不清楚,將模糊效果應用於節點會使其變得不清楚。方框模糊是 JavaFX 提供的一種模糊效果。在這種效果中,要將模糊應用於節點,使用的是簡單的方框濾波器。

名為BoxBlur的類屬於javafx.scene.effect包,代表方框模糊效果,此類包含四個屬性,它們是:

  • height - 此屬性為雙精度型別,表示效果的垂直大小。

  • width - 此屬性為雙精度型別,表示效果的水平大小。

  • input - 此屬性為 effect 型別,表示方框模糊效果的輸入。

  • iterations - 此屬性為整數型別,表示要應用於節點的效果迭代次數。這樣做是為了提高其質量或平滑度。

示例

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

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

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.BoxBlur; 
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 BoxBlurEffectExample 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 added. 
      text.setText("Welcome to Tutorialspoint");       
      
      //Setting the color of the text 
      text.setFill(Color.DARKSEAGREEN);
      
      //Instantiating the BoxBlur class 
      BoxBlur boxblur = new BoxBlur();      
      
      //Setting the width of the box filter 
      boxblur.setWidth(8.0f);  
      
      //Setting the height of the box filter 
      boxblur.setHeight(3.0f); 
      
      //Setting the no of iterations  
      boxblur.setIterations(3);       
               
      //Applying BoxBlur effect to the text 
      text.setEffect(boxblur);          
         
      //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("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 BoxBlurEffectExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls BoxBlurEffectExample 

輸出

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

Box Blur Effect
廣告