JavaFX - 運動模糊效果



就像高斯模糊效果一樣,運動模糊也是 JavaFX 中用於模糊節點的效果。它也使用高斯卷積核來產生模糊效果。高斯模糊效果和運動模糊的唯一區別在於,高斯卷積核是與指定的角度一起使用的。

顧名思義,透過指定某個角度應用此效果後,給定的輸入看起來就像您在運動中看到它一樣。

名為MotionBlur的包javafx.scene.effect中的類表示運動模糊效果。此類包含三個屬性,包括:

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

  • radius - 此屬性為雙精度型別,表示要應用運動模糊效果的半徑。

  • Angle - 此屬性為雙精度型別,它表示運動效果的角度(以度為單位)。

示例

以下程式是一個演示運動模糊效果的示例。在這裡,我們繪製文字“歡迎來到 Tutorialspoint”,並用 DARKSEAGREEN 顏色填充,並以 45 度角應用運動模糊效果。

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

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
import javafx.scene.effect.MotionBlur; 
         
public class MotionBlurEffectExample 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 MotionBlur class 
      MotionBlur motionBlur = new MotionBlur();       
      
      //Setting the radius to the effect 
      motionBlur.setRadius(10.5); 
      
      //Setting angle to the effect 
      motionBlur.setAngle(45);        
      
      //Applying MotionBlur effect to text
      text.setEffect(motionBlur);        
         
      //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 MotionBlurEffectExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls MotionBlurEffectExample   

輸出

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

MotionBlur Effect
廣告