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.
We are creating a rectangle and applying this effect to it. Save this code in a file with the name ImageInputEffectExample.java.
importjavafx.application.Application;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.scene.effect.ImageInput;importjavafx.scene.image.Image;importjavafx.scene.shape.Rectangle;importjavafx.stage.Stage;publicclassImageInputEffectExampleextendsApplication{@Overridepublicvoidstart(Stage stage){//Creating an image Image image =newImage("http://www.tutorialspoint.com/green/images/logo.png");//Instantiating the Rectangle class Rectangle rectangle =newRectangle();//Instantiating the ImageInput class ImageInput imageInput =newImageInput();//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 =newGroup(rectangle);//Creating a scene object Scene scene =newScene(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();}publicstaticvoidmain(String args[]){launch(args);}}
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX%--add-modules javafx.controls ImageInputEffectExample.java
java --module-path %PATH_TO_FX%--add-modules javafx.controls ImageInputEffectExample
Output
On executing, the above program generates a JavaFX window as shown below.
Leave a Reply