Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninja!! You must have seen shadows in real life. Ever wondered how these shadows are created digitally? JavaFX contains a class to pretty much achieve the same effect. In this blog, you will learn about Shadow class, its constructors, and properties, and you will also go through a sample program to understand the implementation of shadow class. Let's jump right into the details of this excellent class.
Shadow Class
Shadow class is a part of JavaFX and is present under scene class. Shadow class creates a blur effect of the original node passed. The Shadow is black, but it can be changed to any colour. It may be used to create a glowing effect as well. Shadow effect is present in javafx.scene.effect.Shadow class. You need to initialise the class to use the shadow effect.
Constructors
There are three constructors in the class:
public Shadow (): It creates a new object with default parameters.
public Shadow (radius, new colour): It creates a new object with specified colour and radius.
public Shadow(blur type, colour, radius): It creates a new object with the selected colour, radius and blur type.
Properties
Shadow class offers various properties to manage the state of the object, they are listed below:
Sample program
// JavaFX shadow effect implementation code
package com.example.shadow_coding_ninjas;
//Import Statements to fetch various classes from libraries
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BlurType;
import javafx.scene.effect.Shadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
// Class implementation
public class Shadow_Coding_Ninja extends Application {
//start method of Application class is overridden with custom definition
@Override
public void start(Stage view) throws IOException
{
//Creating new image object
Image pic = new Image("C:/Users/kvoxa/Downloads/ninja.png");
//Creating image view object
ImageView picView1 = new ImageView(pic);
ImageView picView2 = new ImageView(pic);
picView1.setX(150);
picView1.setY(100);
picView2.setX(450);
picView2.setY(100);
picView1.setFitHeight(200);
picView1.setFitWidth(400);
picView2.setFitHeight(200);
picView2.setFitWidth(400);
picView1.setPreserveRatio(true);
picView2.setPreserveRatio(true);
//Creating shadow object of shadow class and setting blur type and colour
Shadow shadow = new Shadow();
shadow.setBlurType(BlurType.GAUSSIAN);
shadow.setColor(Color.ORANGE);
shadow.setHeight(30);
shadow.setRadius(12);
shadow.setWidth(20);
picView2.setEffect(shadow);
Group root = new Group(picView1,picView2);
//Setting up the scene
Scene scene = new Scene(root,800,400);
view.setScene(scene);
view.setTitle(" JavaFx shadow Effect : Coding Ninjas ");
view.show();
}
//launch method
public static void main(String[] args) {
launch(args);
}
}
You can also try this code with Online Java Compiler
JavaFX is an open-source Software Development Kit for building feature-rich and modern GUI desktop and web applications. It is the outcome of the collaboration of a large pool of developers and companies to produce a toolkit to develop modern client applications.
What is a package?
A package is a namespace similar to a folder where you can put your source files. It organises related classes and interfaces. You might keep java code in one folder, images in another, and so on.
What is the @override statement?
When inheriting to over-write an existing class, an overriding statement is used. Using @override neglects the method definition available in the parent class and uses the local definition.
What are import statements?
Import statement fetches pre-written libraries, which help in the faster development of applications. Organisations or language developers provide these libraries.
Conclusion
Finally, you have made it to the end of this article. Congratulations!! In this blog, you learned about Shadow class in JavaFX. You went through the class, its constructors, and a sample program.
After reading about 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, JavaFX Menus, JavaFX Inline Styles, JavaFX circles etc.