Light.Spot class is a part of JavaFX. Light.Spot class is used to create a spot light with configurable values of direction vector of light, focus and Color in 3D space. Light.Spot class inherits Light.Point class.
Constructors of the class:
- Spot(): Creates a spot light with default values
- Spot(double x, double y, double z, double s, Color c): Creates a spot light with specified values of x, y, z, specularExponent and color of light
Commonly Used Methods:
Methods | Explanation |
---|---|
getPointsAtX() | Returns the x coordinate of the direction vector of light. |
getPointsAtY() | Returns the y coordinate of the direction vector of light. |
getPointsAtZ() | Returns the z coordinate of the direction vector of light. |
getSpecularExponent() | Returns the value of specularExponent. |
setPointsAtX(double v) | Sets the value of x coordinate of the direction vector of light. |
setPointsAtY(double v) | Sets the value of y coordinate of the direction vector of light. |
setPointsAtZ(double v) | Sets the value of z coordinate of the direction vector of light. |
setSpecularExponent(double v) | Sets the value of specularExponent. |
Below programs illustrate the use of Light.Spot class:
- Java Program to create a Spot light and add it to a rectangle: In this program we will create a Rectangle named rectangle with specified height and width. We will also create a Light.Spot object named light. We will set the x, y, z values using setX(), setY() and setZ() function. Now create a lighting object and add the light object to lighting using setLight() function. We will set the Lighting effect to the Rectangle and add it to the scene and add the scene to the stage and call the show function to display the results.
// Java Program to create a Spot light
// and add it to a rectangle
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.shape.Rectangle;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.Group;
import
javafx.scene.effect.Light.*;
import
javafx.scene.effect.*;
import
javafx.scene.paint.Color;
Â
Âpublic
class
Spot_1
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"creating Light.Spot"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create Spot Light object
       Â
Light.Spot light =
new
Light.Spot();
Â
ÂÂ Â Â Â Â Â Â Â
// set coordinates
       Â
light.setX(
100
);
       Â
light.setY(
100
);
       Â
light.setZ(
100
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a lighting
       Â
Lighting lighting =
new
Lighting();
Â
ÂÂ Â Â Â Â Â Â Â
// set Light of lighting
       Â
lighting.setLight(light);
Â
ÂÂ Â Â Â Â Â Â Â
// create a rectangle
       Â
Rectangle rect =
new
Rectangle(
250
,
250
);
Â
ÂÂ Â Â Â Â Â Â Â
// set fill
       Â
rect.setFill(Color.WHITE);
Â
ÂÂ Â Â Â Â Â Â Â
// set effect
       Â
rect.setEffect(lighting);
Â
ÂÂ Â Â Â Â Â Â Â
// create a Group
       Â
Group group =
new
Group(rect);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(group,
500
,
300
);
Â
ÂÂ Â Â Â Â Â Â Â
// 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 a Spotlight and add it to a rectangle and set the coordinates of the direction vector of light and color of light: In this program we will create a Rectangle named rectangle with specified height and width. We will also create a Light.Spot object named light. We will set the x, y, z values using setX(), setY() and setZ() function. The coordinate of the direction vector of light is set using setPointsAtX(), setPointsAtY() and setPointsAtX(), and specify the value of color using setColor() function Now create a lighting object and add the light object to lighting using setLight() function. We will set the Lighting effect to the Rectangle and add it to the scene and add the scene to the stage and call the show function to display the results.
// Java Program to create a Spot light
// and add it to a rectangle and set the
// coordinates of direction vector ofÂ
// light and color of light
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.shape.Rectangle;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.Group;
import
javafx.scene.effect.Light.*;
import
javafx.scene.effect.*;
import
javafx.scene.paint.Color;
Â
Âpublic
class
Spot_2
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"creating Light.Spot"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create Spot Light object
       Â
Light.Spot light =
new
Light.Spot();
Â
ÂÂ Â Â Â Â Â Â Â
// set coordinate of direction
       Â
// the vector of this light
       Â
light.setPointsAtX(
0
);
       Â
light.setPointsAtY(
0
);
       Â
light.setPointsAtZ(-
60
);
Â
ÂÂ Â Â Â Â Â Â Â
// set specular exponent
       Â
light.setSpecularExponent(
2
);
Â
ÂÂ Â Â Â Â Â Â Â
// set color of light
       Â
light.setColor(Color.RED);
Â
ÂÂ Â Â Â Â Â Â Â
// set coordinates
       Â
light.setX(
100
);
       Â
light.setY(
100
);
       Â
light.setZ(
200
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a lighting
       Â
Lighting lighting =
new
Lighting();
Â
ÂÂ Â Â Â Â Â Â Â
// set Light of lighting
       Â
lighting.setLight(light);
Â
ÂÂ Â Â Â Â Â Â Â
// create a rectangle
       Â
Rectangle rect =
new
Rectangle(
250
,
250
);
Â
ÂÂ Â Â Â Â Â Â Â
// set fill
       Â
rect.setFill(Color.WHITE);
Â
ÂÂ Â Â Â Â Â Â Â
// set effect
       Â
rect.setEffect(lighting);
Â
ÂÂ Â Â Â Â Â Â Â
// create a Group
       Â
Group group =
new
Group(rect);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(group,
500
,
300
);
Â
ÂÂ Â Â Â Â Â Â Â
// 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/Light.Spot.html