Table of contents
1.
Introduction
2.
DropShadow Class
3.
Constructors
4.
Properties
5.
Sample program
6.
Frequently Asked Questions
6.1.
What is root?
6.2.
What is the use of the launch method?
6.3.
How does inheritance work in the given sample program?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX DropShadow

Author Manish Kumar
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaFX offers effects of various types. The DropShadow effect generates a shadow behind the main image. In this blog, you will learn about DropShadow class, its constructors, and properties, and you will also go through a sample program to understand the implementation of the drop shadow class. Let's jump right into the details of this excellent class.

JavaFX Logo

DropShadow Class

DropShadow class is a part of JavaFX and is present under the javafx.scene.effect package. On applying the drop shadow effect a shadow is created behind the target node.  You need to initialise the class to use the shadow effect. In the sample program given below, the drop shadow effect of the GAUSSIAN blur type is created with the HOTPINK colour.

Constructors

There are four constructors in the class:

  1. DropShadow (): It creates a new object with default parameters.
  2. DropShadow (radius, colour): It creates a new object with specified colour and radius.
  3. DropShadow (radius, offsetX,offsetY, colour): It creates a new object with the selected colour, radius and offset in x and y directions.
  4. DropShadow (blur type, radius, colour, spread, offsetX, offsetY): It creates a new object with the selected colour, radius and offset in x and y directions, blur type and spread.

Properties

DropShadow class offers various properties to manage the state of the object, they are listed below:

Properties Table

Sample program

 

//Java code to implement drop shadow effect
//Coding Ninjas
package com.example.dropshadow_coding_ninjas;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BlurType;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

//Class definition
public class CodingNinjas extends Application {
   //override method
   
   //start method from application class with custom definition
   @Override
   public void start(Stage view)
   {
       Image pic = new Image("C:/Users/kvoxa/Downloads/ninja.png");
       ImageView picView1 = new ImageView(pic);
       ImageView picView2 = new ImageView(pic);
       //setting up the view coordinates
       picView1.setX(140);
       picView1.setY(90);
       picView2.setX(440);
       picView2.setY(90);

       picView1.setFitHeight(150);
       picView1.setFitWidth(350);
       picView2.setFitHeight(150);
       picView2.setFitWidth(350);
       //maintaining ratio of views
       picView1.setPreserveRatio(true);
       picView2.setPreserveRatio(true);
       
       //Creating drop shadow object and configuring its properties
       DropShadow shade = new DropShadow();
       shade.setBlurType(BlurType.GAUSSIAN);
       shade.setColor(Color.HOTPINK);
       shade.setHeight(130);
       shade.setWidth(90);
       shade.setOffsetX(20);
       shade.setOffsetY(20);
       shade.setSpread(0.3);
       shade.setRadius(20);
       picView2.setEffect(shade);
       Group root = new Group(picView1,picView2);
       
       //Configuring the scene object
       Scene scene = new Scene(root,600,300);
       view.setScene(scene);
       view.setTitle(" JavaFx Drop shadow : Coding Ninjas ");
       view.show();
   }
   
   //main method to launch
   public static void main(String[] args) {
       launch(args);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Output of Sample Program

Frequently Asked Questions

What is root?

The scene graph nodes make up the JavaFX scene tree. The topmost node is known as the root.

What is the use of the launch method?

The launch method is part of the Application class and it helps to run the JavaFX program.

How does inheritance work in the given sample program?

The class ‘CodingNinjas’ inherits from the Application class using the ‘extends’ keyword.

Conclusion

Finally, you have made it to the end of this article. Congratulations!! In this blog, you learnt about DropShadow class in JavaFX. You went through the class, its constructors and a sample program.

After reading about the drop shadow class in JavaFX, are you not feeling excited to read more articles on the topic of JavaFX? Don't worry; Coding Ninjas has you covered. To learn, see the JavaFX EffectsJavaFX ImageInputJavaFX ColorInputJavaFX Bloom etc.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enrol in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please do upvote our blogs if you find them helpful and informative!

Ninja Image

Happy learning!

Live masterclass