JavaFX 效果 - 運動模糊



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

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

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

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

  • `**radius**` - 此屬性為雙精度型別,表示要應用 `**Motion Blur Effect**` 的半徑。

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

示例

以下程式是一個演示運動模糊效果的示例。在這裡,我們繪製了填充為 DARKSEAGREEN 顏色的文字“Welcome to Tutorialspoint”,並以 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 MotionBlurEffectExample.java 
java MotionBlurEffectExample   

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

MotionBlur Effect
javafx_effects.htm
廣告
© . All rights reserved.