📜  JavaFX |带示例的 PointLight

📅  最后修改于: 2022-05-13 01:54:34.656000             🧑  作者: Mango

JavaFX |带示例的 PointLight

PointLight 是 JavaFX 的一部分。 PointLight 定义了一个点光源。 PointLight 是空间中的固定光源,向各个方向辐射光。

类的构造函数是:

  1. PointLight() : 创建一个新的点光源实例
  2. PointLight(Color c) : 创建特定颜色的点光源的新实例

常用方法:

MethodExplanation
getColor()returns the color of the light
isLightOn()returns whether the light is on or not
setColor(Color value)sets the color of the light
setLightOn(boolean value)Sets the value of the property lightOn.

下面的程序说明了 PointLight 类:

  1. 创建默认颜色的点光源的Java程序:该程序创建一个由名称 sphere 指示的球体(半径作为参数传递)。创建了一个名为 pointlight 的 PointLight,它是一个点光源,向所有方向辐射光。球体将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加球体和点光源。该组已连接到现场。最后调用show()方法显示最终结果。将创建一个透视相机并将其添加到场景中以在 3D 中渲染圆柱体。
    // Java program to create a point light of default color
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.shape.DrawMode;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.scene.PointLight;
    import javafx.scene.shape.Sphere;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.PerspectiveCamera;
    import javafx.scene.paint.Color;
      
    public class pointlight_2 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
              
            // set title for the stage
            stage.setTitle("creating sphere");
      
            // create a sphere
            Sphere sphere = new Sphere(80.0f);
      
            // create a point light
            PointLight pointlight = new PointLight();
      
            // create a Group
            Group group = new Group(sphere, pointlight);
      
            // translate the sphere to a position
            sphere.setTranslateX(100);
            sphere.setTranslateY(100);
      
            // translate point light
            pointlight.setTranslateZ(-1000);
            pointlight.setTranslateX(+1000);
            pointlight.setTranslateY(+10);
      
            // create a perspective camera
            PerspectiveCamera camera = new PerspectiveCamera(false);
            camera.setTranslateX(0);
            camera.setTranslateY(0);
            camera.setTranslateZ(0);
      
            // create a scene
            Scene scene = new Scene(group, 500, 300);
      
            scene.setCamera(camera);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
          
        // Main Method
        public static void main(String args[])
        {
              
            // launch the application
            launch(args);
        }
    }
    

    输出:

  2. 用于创建指定颜色的点光源的Java程序(例如 RED):该程序创建一个由名称 sphere 表示的球体(半径作为参数传递)。一个名为 pointlight( 的 PointLight 被创建,指定的颜色作为参数传递,它是一个点光源,向所有方向辐射光。球体将在场景中创建,而场景又将托管在舞台中。函数setTitle ()用于为舞台提供标题。然后创建一个Group,并附加球体和点光源。该组附加到场景中。最后调用show()方法显示最终结果。一个透视图相机将被创建并添加到场景中以 3D 渲染圆柱体。
    // Java program to create a point light
    // of specified color(eg RED)
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.shape.DrawMode;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.scene.PointLight;
    import javafx.scene.shape.Sphere;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.PerspectiveCamera;
    import javafx.scene.paint.Color;
      
    public class sphere_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
              
            // set title for the stage
            stage.setTitle("creating sphere");
      
            // create a sphere
            Sphere sphere = new Sphere(80.0f);
      
            // create a point light
            PointLight pointlight = new PointLight(Color.RED);
      
            // create a Group
            Group group = new Group(sphere, pointlight);
      
            // translate the sphere to a position
            sphere.setTranslateX(100);
            sphere.setTranslateY(100);
      
            // translate point light
            pointlight.setTranslateZ(-1000);
            pointlight.setTranslateX(+1000);
            pointlight.setTranslateY(+10);
      
            // create a perspective camera
            PerspectiveCamera camera = new PerspectiveCamera(false);
            camera.setTranslateX(0);
            camera.setTranslateY(0);
            camera.setTranslateZ(0);
      
            // create a scene
            Scene scene = new Scene(group, 500, 300);
      
            scene.setCamera(camera);
      
            // 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/PointLight.html