Do you think IIT Guwahati certified course can help you in your career?
No
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.
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:
DropShadow (): It creates a new object with default parameters.
DropShadow (radius, colour): It creates a new object with specified colour and radius.
DropShadow (radius, offsetX,offsetY, colour): It creates a new object with the selected colour, radius and offset in x and y directions.
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:
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
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 Effects, JavaFX ImageInput, JavaFX ColorInput, JavaFX Bloom etc.