JavaFX - InnerShadow Effect



While the drop shadow effect creates a duplicate no behind the original node by blurring its edges, the inner shadow effect will be create a shadow inside the edges of the node. They are both similar to shadow effect.

The class named InnerShadow of the package javafx.scene.effect represents the inner shadow effect. This class contains ten properties, which are −

  • color − This property is of Color type representing the color of the shadow.

  • blur type − This property is of BlurType and it represents the type of blur effect used to blur the shadow.

  • radius − This property is of the type double and it represents the radius of the shadow blur kernel.

  • width − This property is of the type double and it represents the width of the shadow blur kernel.

  • height − This property is of the type double and it represents the height of the shadow blur kernel.

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

  • spread − This property is of the type double; it represents the spread of the shadow.

  • offsetX − This property is of the type double, it represents the shadow offset in the x direction, in pixels.

  • offsetY − This property is of the type double, it represents the shadow offset in the y direction in pixels.

  • choke − This property is of double type; it represents the choke of the shadow.

Example

The following program is an example demonstrating the inner shadow effect of JavaFX. In here, we are drawing a text “Welcome to Tutorialspoint”, and a circle in a scene.

To these, we are applying the inner shadow effect. Save this code in a file with the name InnerShadowEffectExample.java.

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.InnerShadow; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
         
public class InnerShadowEffectExample 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(50); 
      
      //Setting the text to be embedded. 
      text.setText("Welcome to Tutorialspoint");        
      
      //Setting the color of the text 
      text.setFill(Color.RED);   
       
      //Drawing a Circle 
      Circle circle = new Circle();         
      
      //Setting the center of the circle 
      circle.setCenterX(300.0f); 
      circle.setCenterY(160.0f); 
      
      //Setting the radius of the circle 
      circle.setRadius(100.0f); 
      
      //setting the fill color of the circle 
      circle.setFill(Color.CORNFLOWERBLUE);   
       
      //Instantiating the InnerShadow class  
      InnerShadow innerShadow = new InnerShadow(); 
      
      //Setting the offset values of the inner shadow 
      innerShadow.setOffsetX(4); 
      innerShadow.setOffsetY(4); 
      
      //Setting the color of the inner shadow 
      innerShadow.setColor(Color.GRAY);        
      
      //Applying inner shadow effect to the text 
      text.setEffect(innerShadow);      
      
      //Applying inner shadow effect to the circle 
      circle.setEffect(innerShadow); 
         
      //Creating a Group object  
      Group root = new Group(text,circle);   
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);
      
      //Setting title to the Stage 
      stage.setTitle("Inner shadow 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 InnerShadowEffectExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls InnerShadowEffectExample  

Output

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

Inner Shadow Effect
Advertisements