📜  JavaFX |环境光类

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

JavaFX |环境光类

AmbientLight 类是 JavaFX 的一部分。这个类定义了一个环境光对象。 AmbientLight 类创建了一个似乎来自各个方向的光源。

该类的构造函数是:

  1. AmbientLight() :创建具有默认白色的环境光源
  2. AmbientLight(Color c) :创建具有指定颜色的环境光源

以下方法继承自 LightBase :

MethodsExplanation
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.

下面的程序将说明 AmbientLight 类的使用:

  1. 创建默认颜色环境光的Java程序:该程序创建一个由名称 sphere 指示的球体(半径作为参数传递)。创建了一个名为ambient_light默认白色的AmbientLight。将创建一个名为 button 的 Button,用于打开或关闭环境光。球体将在场景内创建,而场景又将托管在舞台内。函数setTitle()用于为舞台提供标题。然后创建一个组,并附加球体、按钮和环境光。组附加到场景。最后调用show()方法显示最终结果。
    // Java program to create a ambient 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.AmbientLight;
    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;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
      
    public class ambient_light_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            // set title for the stage
            stage.setTitle("creating ambient_light");
      
            // create a sphere
            Sphere sphere = new Sphere(80.0f);
      
            // create a ambient light
            AmbientLight ambient_light = new AmbientLight();
      
            // create a button
            Button button = new Button("light");
      
            // create a Group
            Group group = new Group(sphere, ambient_light, button);
      
            // translate the sphere to a position
            sphere.setTranslateX(100);
            sphere.setTranslateY(100);
      
            // action event
            EventHandler event = 
            new EventHandler() {
      
                public void handle(ActionEvent e)
                {
                    ambient_light.setLightOn(!ambient_light.isLightOn());
                }
            };
      
            // set on action
            button.setOnAction(event);
      
            // 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);
        }
    }
    

    输出:

  2. 用于创建指定颜色的环境光的Java程序:该程序创建一个由名称 sphere 指示的球体(半径作为参数传递)。创建一个名为ambient_light的指定颜色(RED) 的AmbientLight。将创建一个名为 button 的 Button,用于打开或关闭环境光。球体将在场景内创建,而场景又将托管在舞台内。函数setTitle()用于为舞台提供标题。然后创建一个组,并附加球体、按钮和环境光。组附加到场景。最后调用show()方法显示最终结果。
    // Java program to create a ambient light 
    // of a specified 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.AmbientLight;
    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;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
      
    public class ambient_light_2 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            // set title for the stage
            stage.setTitle("creating ambient_light");
      
            // create a sphere
            Sphere sphere = new Sphere(80.0f);
      
            // create a ambient light
            AmbientLight ambient_light = new AmbientLight(Color.RED);
      
            // create a button
            Button button = new Button("light");
      
            // create a Group
            Group group = new Group(sphere, ambient_light, button);
      
            // translate the sphere to a position
            sphere.setTranslateX(100);
            sphere.setTranslateY(100);
      
            // action event
            EventHandler event = 
            new EventHandler() {
      
                public void handle(ActionEvent e)
                {
                    ambient_light.setLightOn(!ambient_light.isLightOn());
                }
            };
      
            // set on action
            button.setOnAction(event);
      
            // 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/AmbientLight.html