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 application   Âpublicvoidstart(Stage stage)throwsException   Â{       Â// set title for the stage       Âstage.setTitle("reflection example");       Â// create a input stream       ÂFileInputStream input =newFileInputStream("D:\\GFG.png");       Â// create a image       ÂImage image =newImage(input);       Â// create a image View       ÂImageView imageview =newImageView(image);       Â// create a reflection effect       ÂReflection reflection =newReflection();       Â// set effect       Âimageview.setEffect(reflection);       Â// create a VBox       ÂVBox vbox =newVBox(imageview);       Â// create a scene       ÂScene scene =newScene(vbox,200,200);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(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 application   Âpublicvoidstart(Stage stage)throwsException   Â{       Â// set title for the stage       Âstage.setTitle("reflection example");       Â// create a input stream       ÂFileInputStream input =newFileInputStream("D:\\GFG.png");       Â// create a image       ÂImage image =newImage(input);       Â// create a image View       ÂImageView imageview =newImageView(image);       Â// create a reflection effect       ÂReflection reflection =newReflection();       Â// set fraction       Âreflection.setFraction(0.6);       Â// set top Opacity       Âreflection.setTopOpacity(0.3);       Â// set bottom Opacity       Âreflection.setBottomOpacity(0.1);       Â// set top offset       Âreflection.setTopOffset(0.5);       Â// set effect       Âimageview.setEffect(reflection);       Â// create a VBox       ÂVBox vbox =newVBox(imageview);       Â// create a scene       ÂScene scene =newScene(vbox,200,200);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(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

