JavaFX |光点类
Light.Spot 类是 JavaFX 的一部分。 Light.Spot 类用于在 3D 空间中创建具有可配置的光、焦点和颜色方向矢量值的聚光灯。 Light.Spot 类继承Light.Point类。
类的构造函数:
- Spot() :使用默认值创建聚光灯
- Spot(double x, double y, double z, double s, Color c) : 创建一个具有指定值 x、y、z、specularExponent 和光颜色的聚光灯
常用方法:
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. |
下面的程序说明了 Light.Spot 类的使用:
- 创建聚光灯并将其添加到矩形的Java程序:在此程序中,我们将创建一个名为矩形的矩形,具有指定的高度和宽度。我们还将创建一个名为light的Light.Spot 对象。我们将使用setX() 、 setY()和setZ()函数设置 x、y、z 值。现在创建一个光照对象并使用setLight()函数将光照对象添加到光照中。我们将Lighting效果设置为Rectangle并将其添加到场景中并将场景添加到舞台并调用show函数以显示结果。
// 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); } }
输出:
- Java程序创建一个 Spotlight 并将其添加到一个矩形中,并设置光的方向向量和光的颜色的坐标:在这个程序中,我们将创建一个名为矩形的 Rectangle,具有指定的高度和宽度。我们还将创建一个名为light的Light.Spot 对象。我们将使用setX() 、 setY()和setZ()函数设置 x、y、z 值。使用setPointsAtX() 、 setPointsAtY()和setPointsAtX()设置光的方向向量的坐标,并使用setColor()函数指定颜色的值 现在创建一个光照对象,并使用setLight () 将光照对象添加到光照中函数。我们将Lighting效果设置为Rectangle并将其添加到场景中并将场景添加到舞台并调用show函数以显示结果。
// 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); } }
输出:
注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/Light.Spot.html