JavaFX | Light.Spot Class

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 rectangleimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.shape.Rectangle;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.effect.Light.*;importjavafx.scene.effect.*;importjavafx.scene.paint.Color;ÂÂpublicclassSpot_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating Light.Spot");       Â// create Spot Light object       ÂLight.Spot light =newLight.Spot();       Â// set coordinates       Âlight.setX(100);       Âlight.setY(100);       Âlight.setZ(100);       Â// create a lighting       ÂLighting lighting =newLighting();       Â// set Light of lighting       Âlighting.setLight(light);       Â// create a rectangle       ÂRectangle rect =newRectangle(250,250);       Â// set fill       Ârect.setFill(Color.WHITE);       Â// set effect       Ârect.setEffect(lighting);       Â// create a Group       ÂGroup group =newGroup(rect);       Â// create a scene       ÂScene scene =newScene(group,500,300);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(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 lightimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.shape.Rectangle;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.effect.Light.*;importjavafx.scene.effect.*;importjavafx.scene.paint.Color;ÂÂpublicclassSpot_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("creating Light.Spot");       Â// create Spot Light object       ÂLight.Spot light =newLight.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 =newLighting();       Â// set Light of lighting       Âlighting.setLight(light);       Â// create a rectangle       ÂRectangle rect =newRectangle(250,250);       Â// set fill       Ârect.setFill(Color.WHITE);       Â// set effect       Ârect.setEffect(lighting);       Â// create a Group       ÂGroup group =newGroup(rect);       Â// create a scene       ÂScene scene =newScene(group,500,300);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(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




