JavaFX | Reflection Class

Reflection class is a part of JavaFX. The Reflection class is used to add a reflected image below the actual image of the input value. The Reflected image will not respond to mouse events or the containment methods on the input.
Constructors of the class:
- Reflection(): Creates a new Reflection Object.
- Reflection(double topOffset, double fraction, double topOpacity, double bottomOpacity): Creates a new Reflection object with the specified topOffset, fraction, topOpacity and bottomOpacity.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getBottomOpacity() | Returns the value of bottomOpacity |
| getTopOpacity() | Returns the value of topOpacity |
| getFraction() | Returns the fraction which the reflected image is of the real image |
| getTopOffset() | Returns the value of top offset |
| getInput() | Returns the value of property input |
| setBottomOpacity(double v) | Sets the value of bottomOpacity |
| setTopOpacity(double v) | Sets the value of topOpacity |
| setFraction(double v) | Sets the fraction which the reflected image is of the real image |
| setTopOffset(double v) | Sets the value of top offset |
| setInput(Effect v) | Sets the value of property input |
Below programs illustrate the use of Reflection class:
- Java program to add a reflection to the image using the reflection class: In this program a FileInputStream is created and an image is taken as input from a file. An Image named image is created using the input from the file input stream. From the image, an image view object is created and it is added to the VBox. The VBox is then added to the scene and the scene is added to the stage. A Reflection effect is created and the effect is set to the image view using setEffect() function.
// Java program to add a reflection to// the image using the reflection classimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.image.*;importjavafx.scene.effect.*;importjava.io.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;publicclassreflection_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage)throwsException{// set title for the stagestage.setTitle("reflection example");// create a input streamFileInputStream input =newFileInputStream("D:\\GFG.png");// create a imageImage image =newImage(input);// create a image ViewImageView imageview =newImageView(image);// create a reflection effectReflection reflection =newReflection();// set effectimageview.setEffect(reflection);// create a VBoxVBox vbox =newVBox(imageview);// create a sceneScene scene =newScene(vbox,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java program to add a reflection to the image using the reflection class and set the top offset, top opacity, bottom opacity and fraction of image which will appear as a reflection: In this program a FileInputStream is created and an image is taken as input from a file. Image named image is created using the input from the file input stream. From the image, an image view object is created and it is added to the VBox . The VBox is then added to the scene and the scene is added to the stage. A Reflection effect is created and the effect is set to the image view using setEffect() function. The bottom Opacity, top Opacity, top offset, and fraction are set using the setBottomOpacity(), setTopOpacity(), setFraction(), and setTopOffset() function respectively.
// Java program to add a reflection to the image// using the reflection class and set the top// offset, top opacity bottom opacity and fraction// of image which will appear as reflectionimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.image.*;importjavafx.scene.effect.*;importjava.io.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;publicclassreflection_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage)throwsException{// set title for the stagestage.setTitle("reflection example");// create a input streamFileInputStream input =newFileInputStream("D:\\GFG.png");// create a imageImage image =newImage(input);// create a image ViewImageView imageview =newImageView(image);// create a reflection effectReflection reflection =newReflection();// set fractionreflection.setFraction(0.6);// set top Opacityreflection.setTopOpacity(0.3);// set bottom Opacityreflection.setBottomOpacity(0.1);// set top offsetreflection.setTopOffset(0.5);// set effectimageview.setEffect(reflection);// create a VBoxVBox vbox =newVBox(imageview);// create a sceneScene scene =newScene(vbox,200,200);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/Reflection.html




