JavaFX 效果 - 方框模糊



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

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

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

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

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

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

示例

以下示例演示了方框模糊效果。在這裡,我們繪製填充為 DARKSEAGREEN 顏色的文字“歡迎來到 Tutorialspoint”,並對其應用方框模糊效果。

將此程式碼儲存在名為 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 BoxBlurEffectExample.java 
java BoxBlurEffectExample 

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

Box Blur Effect
javafx_effects.htm
廣告
© . All rights reserved.