JavaFX – Reflection Effect


JavaFX – Reflection Effect


”;


Reflection is scientifically defined as a phenomenon occurring when a light hits a surface (called a reflective surface like mirror), it gets thrown back. In such cases, all the objects present in the line of these light waves are also projected back as an image. You can also apply this reflection effect in JavaFX applications.

On applying the reflection effect to a node in JavaFX, a reflection of it is added at the bottom of the node.

The class named Reflection of the package javafx.scene.effect represents the reflection effect. This class contains four properties, which are −

  • topOpacity − This property is of double type representing the top extremes opacity value of the reflection.

  • bottomOpacity − This property is of double type representing the bottom extremes opacity value of the reflection.

  • input − This property is of the type Effect and it represents an input to the reflection effect.

  • topOffset − This property is of double type representing the distance between the bottom of the input and the top of the reflection.

  • fraction −This property is of double type representing the fraction of input that is visible in the output. The range of the fraction value is 0.0 to 1.0.

Example

The following program is an example demonstrating the reflection effect. In here, we are drawing the text “Welcome to Tutorialspoint” filled with DARKSEAGREEN color and applying the reflection effect to it.

Save this code in a file with the name ReflectionEffectExample.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.Reflection; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
         
public class ReflectionEffectExample extends Application { 
   @Override 
   public void start(Stage stage) {       
      //Creating a Text object 
      Text text = new Text();       
      
      //Setting font to the text 
      text.setFont(Font.font(null, FontWeight.BOLD, 40));       
      
      //setting the position of the text 
      text.setX(60); 
      text.setY(150); 
      
      //Setting the text to be embedded. 
      text.setText("Welcome to Tutorialspoint");       
      
      //Setting the color of the text 
      text.setFill(Color.DARKSEAGREEN);  
       
      //Instanting the reflection class 
      Reflection reflection = new Reflection(); 
      
      //setting the bottom opacity of the reflection 
      reflection.setBottomOpacity(0.0); 
      
      //setting the top opacity of the reflection 
      reflection.setTopOpacity(0.5); 
      
      //setting the top offset of the reflection 
      reflection.setTopOffset(0.0);
      
      //Setting the fraction of the reflection 
      reflection.setFraction(0.7); 
       
      //Applying reflection effect to the text 
      text.setEffect(reflection);          
         
      //Creating a Group object  
      Group root = new Group(text);   
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 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); 
   } 
}       

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 ReflectionEffectExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls ReflectionEffectExample   

Output

On executing, the above program generates a JavaFX window as shown below.

Reflection Effect

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *