JavaFX | AmbientLight Class
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:
- AmbientLight(): Creates a ambient light source with default white color
- 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:
- 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 colorimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.shape.DrawMode;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.AmbientLight;importjavafx.scene.shape.Sphere;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.PerspectiveCamera;importjavafx.scene.paint.Color;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;publicclassambient_light_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating ambient_light");// create a sphereSphere sphere =newSphere(80.0f);// create a ambient lightAmbientLight ambient_light =newAmbientLight();// create a buttonButton button =newButton("light");// create a GroupGroup group =newGroup(sphere, ambient_light, button);// translate the sphere to a positionsphere.setTranslateX(100);sphere.setTranslateY(100);// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){ambient_light.setLightOn(!ambient_light.isLightOn());}};// set on actionbutton.setOnAction(event);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:


- 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 colorimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.shape.DrawMode;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.AmbientLight;importjavafx.scene.shape.Sphere;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.PerspectiveCamera;importjavafx.scene.paint.Color;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;publicclassambient_light_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating ambient_light");// create a sphereSphere sphere =newSphere(80.0f);// create a ambient lightAmbientLight ambient_light =newAmbientLight(Color.RED);// create a buttonButton button =newButton("light");// create a GroupGroup group =newGroup(sphere, ambient_light, button);// translate the sphere to a positionsphere.setTranslateX(100);sphere.setTranslateY(100);// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){ambient_light.setLightOn(!ambient_light.isLightOn());}};// set on actionbutton.setOnAction(event);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


