JavaFX特效 - 圖片輸入



JavaFX中的圖片輸入特效只是將圖片嵌入到JavaFX螢幕上。就像顏色輸入特效一樣,它用於將指定的彩色矩形區域作為輸入傳遞給另一個特效。圖片輸入特效用於將指定的圖片作為輸入傳遞給另一個特效。

應用此特效後,指定的圖片不會被修改。此特效可應用於任何節點。

名為ImageInput的類(位於javafx.scene.effect包中)表示圖片輸入特效,此類包含三個屬性:

  • x − 此屬性為Double型別;它表示源圖片位置的x座標。

  • y − 此屬性為Double型別;它表示源圖片位置的y座標。

  • source − 此屬性為Image型別;它表示用作此特效源的圖片。(作為輸入傳遞)

示例

以下程式是一個演示圖片輸入特效的示例。在這裡,我們在位置150, 100處建立一個圖片輸入,並使用以下圖片(tutorialspoint 徽標)作為此特效的源。

Image Input Effect

我們建立一個矩形並將此特效應用於它。將此程式碼儲存在名為ImageInputEffectExample.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.ImageInput; 
import javafx.scene.image.Image; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 
         
public class ImageInputEffectExample extends Application { 
   @Override  
   public void start(Stage stage) {       
      //Creating an image 
      Image image = new Image("https://tutorialspoint.tw/green/images/logo.png"); 
             
      //Instantiating the Rectangle class 
      Rectangle rectangle = new Rectangle(); 
     
      //Instantiating the ImageInput class 
      ImageInput imageInput = new ImageInput(); 
      
      //Setting the position of the image
      imageInput.setX(150); 
      imageInput.setY(100);       
      
      //Setting source for image input  
      imageInput.setSource(image); 
       
      //Applying image input effect to the rectangle node 
      rectangle.setEffect(imageInput);    
         
      //Creating a Group object  
      Group root = new Group(rectangle);   
               
      //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 ImageInputEffectExample.java 
java ImageInputEffectExample  

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

Image Input Effect Example
javafx_effects.htm
廣告
© . All rights reserved.