Open In App

JavaFX | AmbientLight Class

Last Updated : 09 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

AmbientLight class is a part of JavaFX. This class defines an Ambient light object. AmbientLight class creates a light source that seems to come from all direction.

The constructor of the class are:

  1. AmbientLight(): Creates a ambient light source with default white color
  2. AmbientLight(Color c): Creates a ambient light source with specified color

The following methods are inherited from LightBase :

Methods Explanation
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.

Below programs will illustrate the use of the AmbientLight class:

  1. Java program to create a ambient light of default color: This program creates a Sphere indicated by the name sphere(radius is passed as arguments). A AmbientLight is created named ambient_light of default white color. A Button named button will be created which will be used to turn the ambient light on or off. The Sphere will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the sphere, button and ambient light is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.




    // 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<ActionEvent> event = 
            new EventHandler<ActionEvent>() {
      
                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);
        }
    }

    
    

    Output:


  2. Java program to create a ambient light of a specified color: This program creates a Sphere indicated by the name sphere(radius is passed as arguments). A AmbientLight is created named ambient_light of a specified color(RED). A Button named button will be created which will be used to turn the ambient light on or off. The Sphere will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the sphere, button and ambient light is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.




    // 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<ActionEvent> event = 
            new EventHandler<ActionEvent>() {
      
                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);
        }
    }

    
    

    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/AmbientLight.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads