Shadow class is a part of JavaFX. Shadow class creates a monochromatic shadow with blurry edges. The Shadow is of black Color (by default) and can be combined with the original to create a shadow. The Shadow of different color can be added with original to create a Glow effect. Shadow class inherits Effect class.
Constructors of the class:
- Shadow(): Creates a new Shadow object.
- Shadow(BlurType t, Color c, double r): Creates a new Shadow Object with specified blur type, color, and radius.
- Shadow(double r, Color c): Creates a new Shadow Object with radius and color.
Commonly Used Methods:
Method | Explanation |
---|---|
getBlurType() | Returns the blur type of the effect. |
getColor() | Returns the color of the effect. |
getInput() | Returns the value of property input. |
getRadius() | Returns the radius of the Shadow effect. |
setBlurType(BlurType v) | Sets the blur type of the Shadow effect. |
setColor(Color v) | Sets the Color of the Shadow effect. |
setInput(Effect v) | Sets the value of the property input. |
setRadius(double v) | Sets the Radius of the shadow effect. |
Below programs illustrate the use of Shadow class:
- Java program to create a Circle and add Shadow effect to it: In this program we will create a Circle named circle and create a Shadow effect shadow with specified radius and color. The shadow effect will be added to the circle using the setEffect() function and the circle will be added to the group. The circles will be translated to specific position in the stage using setTranslateX() and setTranslateY() function. The group will be added to the scene and the scene will be added to the stage.
// Java program to create a CircleÂ
// and add Shadow effect to it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.image.*;
import
javafx.scene.effect.*;
import
java.io.*;
import
javafx.scene.shape.Circle;
import
javafx.scene.paint.Color;
import
javafx.scene.Group;
Â
Âpublic
class
shadow_1
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
throws
Exception
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"shadow example"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a circle
       Â
Circle circle =
new
Circle(
50
.0f,
50
.0f,
25
.0f);
Â
ÂÂ Â Â Â Â Â Â Â
// translate to a position
       Â
circle.setTranslateX(
50
.0f);
       Â
circle.setTranslateY(
50
.0f);
Â
ÂÂ Â Â Â Â Â Â Â
// create a shadow effect
       Â
Shadow shadow =
new
Shadow(
10
, Color.RED);
Â
ÂÂ Â Â Â Â Â Â Â
// set effect
       Â
circle.setEffect(shadow);
Â
ÂÂ Â Â Â Â Â Â Â
// create a Group
       Â
Group group =
new
Group(circle);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(group,
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(String args[])
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// launch the application
       Â
launch(args);
   Â
}
}
Output:
- Java program to create four Circles and add Shadow effect to it( of different BlurType): In this program we will create a Circles named circle, circle1, circle2, circle3 and create a Shadow effects named shadow1, shadow2, shadow3, shadow4 with specified radius, color and blur type. The shadow effect will be added to the circle using the setEffect() function and the circles will be added to the group.The circles will be translated to specific position in the stage using setTranslateX() and setTranslateY() function. The group will be added to the scene and the scene will be added to the stage.
// Java program to create four Circles and add Shadow
// effect to it which are of different BlurType
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.image.*;
import
javafx.scene.effect.*;
import
java.io.*;
import
javafx.scene.shape.Circle;
import
javafx.scene.paint.Color;
import
javafx.scene.Group;
Â
Âpublic
class
shadow_2
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
throws
Exception
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"shadow example"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a circle
       Â
Circle circle =
new
Circle(
0
.0f,
0
.0f,
25
.0f);
       Â
Circle circle1 =
new
Circle(
0
.0f,
0
.0f,
25
.0f);
       Â
Circle circle2 =
new
Circle(
0
.0f,
0
.0f,
25
.0f);
       Â
Circle circle3 =
new
Circle(
0
.0f,
0
.0f,
25
.0f);
Â
ÂÂ Â Â Â Â Â Â Â
// translate to a position
       Â
circle.setTranslateX(
50
.0f);
       Â
circle.setTranslateY(
50
.0f);
Â
ÂÂ Â Â Â Â Â Â Â
// translate to a position
       Â
circle1.setTranslateX(
150
.0f);
       Â
circle1.setTranslateY(
50
.0f);
Â
ÂÂ Â Â Â Â Â Â Â
// translate to a position
       Â
circle2.setTranslateX(
50
.0f);
       Â
circle2.setTranslateY(
150
.0f);
Â
ÂÂ Â Â Â Â Â Â Â
// translate to a position
       Â
circle3.setTranslateX(
150
.0f);
       Â
circle3.setTranslateY(
150
.0f);
Â
ÂÂ Â Â Â Â Â Â Â
// create shadow effect
       Â
Shadow shadow1 =
new
Shadow(BlurType.values()[
0
], Color.BLUE,
10
);
       Â
Shadow shadow2 =
new
Shadow(BlurType.values()[
1
], Color.BLUE,
10
);
       Â
Shadow shadow3 =
new
Shadow(BlurType.values()[
2
], Color.BLUE,
10
);
       Â
Shadow shadow4 =
new
Shadow(BlurType.values()[
3
], Color.BLUE,
10
);
Â
ÂÂ Â Â Â Â Â Â Â
// set effect
       Â
circle.setEffect(shadow1);
       Â
circle1.setEffect(shadow2);
       Â
circle2.setEffect(shadow3);
       Â
circle3.setEffect(shadow4);
Â
ÂÂ Â Â Â Â Â Â Â
// create a Group
       Â
Group group =
new
Group(circle, circle1, circle2, circle3);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(group,
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(String args[])
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// launch the application
       Â
launch(args);
   Â
}
}
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/Shadow.html