JavaFX Effects - Image Input



Image input effect in JavaFX just embeds an image to the JavaFX screen. Just like in the Color Input effect, it is used to pass the specified colored rectangular region as an input to another effect. An Image Input effect is used to pass the specified image as an input to another effect.

On applying this effect, the image specified will not be modified. This effect is applied to any node.

The class named ImageInput of the package javafx.scene.effect represents the Image Input effect, this class contains three properties, which are −

  • x − This property is of Double type; it represents the x coordinate of the position of the source image.

  • y − This property is of Double type; it represents the y coordinate of the position of the source image.

  • source − his property is of Image type; it represents image that is to be used as a source to this effect. (Passed as input)

Example

The following program is an example demonstrating the Image input effect. In here, we are creating an image input at the position 150, 100, and taking the following image (tutorialspoint logo) as a source for this effect.

Image Input Effect

We are creating a rectangle and applying this effect to it. Save this code in a file with the name 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("http://www.tutorialspoint.com/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); 
   } 
}      

Compile and execute the saved java file from the command prompt using the following commands.

javac ImageInputEffectExample.java 
java ImageInputEffectExample  

On executing, the above program generates a JavaFX window as shown below.

Image Input Effect Example
javafx_effects.htm
Advertisements