Table of contents
1.
Introduction
1.1.
Constructors
1.2.
Properties
1.3.
Usage
2.
Implementation
3.
Frequently Asked Questions
3.1.
What does opacity determine?
3.2.
Is JavaFX suitable for desktop applications?
3.3.
Can JavaFX run in a browser?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Reflection

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaFX includes the Reflection class. Reflection, in general, can be defined as a change in direction.JavaFX allows us to generate the reflection effect on any image. The Reflection class is used to display a reflected image below the original image of the input value.

Image contains reflection of text "CodingNinjas"

The class javafx.scene.effect.Reflection in JavaFX represents the Reflection effect. To generate an appropriate impact, we simply need to instantiate this class.

Constructors

The class contains two constructors.

  1. public Reflection(): Creates a new Reflection instance.
     
  2. public Reflection(double topOffset, double fraction, double topOpacity, double bottomOpacity): Creates an instance of Reflection with the specified parameters.

Properties

The table below describes the Reflection class properties as well as the setter methods.

Properties of Reflection class

Usage

This program serves as an example of the Reflection Effect in JavaFX. In this case, we're writing the words "Welcome to Coding Ninjas" in Black and adding a reflection effect to it.

Save this code in a file named ReflectionEffect.java.

Implementation

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.Group; 
import javafx.scene.effect.Reflection; 
import javafx.stage.Stage; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.Text; 
import javafx.scene.text.FontWeight; 
         
public class ReflectionEffectExample extends Application { 
   @Override 
   public void start(Stage stage) {       
      //Create a Text object 
      Text text = new Text();       
      
      //Setting font and position of text 
      text.setFont(Font.font(null,FontWeight.BLACK,FontPosture.REGULAR,20)); 
      text.setX(90); 
      text.setY(90); 
      
      //Set the text to be embedded. 
      text.setText("Welcome to Coding Ninjas");       
      
      //Setting the color of the text 
      text.setFill(Color.Black);  
       
      //Instantiate the reflection class 
      Reflection reflection = new Reflection(); 
      
      //setting the bottom & top opacity of the reflection 
      reflection.setBottomOpacity(0.2);
      reflection.setTopOpacity(0.2); 
      
      //setting the top offset of the reflection 
      reflection.setTopOffset(10);
      
      //Setting the fraction of the reflection 
      reflection.setFraction(12); 
       
      //Applying reflection effect to the text 
      text.setEffect(reflection);          
         
      //Creating a Group & scene object  
      Group root = new Group(text);
      Scene scene = new Scene(root, 400, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Reflection effect example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}
You can also try this code with Online Java Compiler
Run Code


Execution

To execute the above program. You can follow the given steps:

  1. Open the terminal in the same folder where you have saved your source code.
     
  2. Enter the below code and hit enter.
    javac --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java
     
  3. After you hit enter a new file will automatically be generated namely filename.class
     
  4. Now, enter the below command and hit enter to execute the above program.
    java --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java
     
  5. You have successfully executed the JavaFX program.
     

Output

Output

Frequently Asked Questions

What does opacity determine?

The opacity of the layer determines whether it is visible or opaque. In other words, it governs how much of the layers beneath may be seen.
 

Is JavaFX suitable for desktop applications?

JavaFX is a compelling way of creating GUI applications for developers familiar with Java.
 

Can JavaFX run in a browser?

Yes, JavaFX applications can be set up to execute on an HTML page hosted by a web browser. The Java Plugin is the type of technology that makes this possible.

Conclusion

In this article, we have extensively discussed the Reflection class present inside JavaFX. We have seen different constructors to initialize the Reflection effect. Also, we have seen various methods available inside the class along with the implementation.

If you think this blog has helped you enhance your knowledge about JavaFx path transition and if you would like to learn more, check out our articles JavaFXJavaFX Rotate TransitionJavaFX Introduction, and many more in our Library.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass