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 class
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.image.*;
import
javafx.scene.effect.*;
import
java.io.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.Group;
Â
Âpublic
class
reflection_1
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
throws
Exception
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"reflection example"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a input stream
       Â
FileInputStream input =
new
FileInputStream(
"D:\\GFG.png"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a image
       Â
Image image =
new
Image(input);
Â
ÂÂ Â Â Â Â Â Â Â
// create a image View
       Â
ImageView imageview =
new
ImageView(image);
Â
ÂÂ Â Â Â Â Â Â Â
// create a reflection effect
       Â
Reflection reflection =
new
Reflection();
Â
ÂÂ Â Â Â Â Â Â Â
// set effect
       Â
imageview.setEffect(reflection);
Â
ÂÂ Â Â Â Â Â Â Â
// create a VBox
       Â
VBox vbox =
new
VBox(imageview);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(vbox,
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(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 reflection
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.image.*;
import
javafx.scene.effect.*;
import
java.io.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.Group;
Â
Âpublic
class
reflection_2
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
throws
Exception
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"reflection example"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a input stream
       Â
FileInputStream input =
new
FileInputStream(
"D:\\GFG.png"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a image
       Â
Image image =
new
Image(input);
Â
ÂÂ Â Â Â Â Â Â Â
// create a image View
       Â
ImageView imageview =
new
ImageView(image);
Â
ÂÂ Â Â Â Â Â Â Â
// create a reflection effect
       Â
Reflection reflection =
new
Reflection();
Â
ÂÂ Â Â Â Â Â Â Â
// 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 =
new
VBox(imageview);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(vbox,
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(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