JavaFX | PointLight with examples

PointLight is a part of JavaFX. PointLight defines a point light source. PointLight is a fixed light source in space that radiates light in all direction.
Constructor of the class are:
- PointLight(): Creates a new instance of pointlight
- PointLight(Color c): Creates a new instance of point light of specific color
Commonly Used Methods:
| Method | 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 illustrate the PointLight class:
- Java program to create a point light of default color: This program creates a Sphere indicated by the name sphere(radius is passed as arguments). A PointLight is created named pointlight which is a point light source and radiates light in all direction. 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 and pointlight are attached. The group is attached to the scene. Finally, the show() method is called to display the final results. A perspective camera will be created and added to the scene to render the cylinder in 3D.
// Java program to create a point light of default colorimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.shape.DrawMode;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.PointLight;importjavafx.scene.shape.Sphere;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.PerspectiveCamera;importjavafx.scene.paint.Color;publicclasspointlight_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating sphere");// create a sphereSphere sphere =newSphere(80.0f);// create a point lightPointLight pointlight =newPointLight();// create a GroupGroup group =newGroup(sphere, pointlight);// translate the sphere to a positionsphere.setTranslateX(100);sphere.setTranslateY(100);// translate point lightpointlight.setTranslateZ(-1000);pointlight.setTranslateX(+1000);pointlight.setTranslateY(+10);// create a perspective cameraPerspectiveCamera camera =newPerspectiveCamera(false);camera.setTranslateX(0);camera.setTranslateY(0);camera.setTranslateZ(0);// create a sceneScene scene =newScene(group,500,300);scene.setCamera(camera);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java program to create a point light of a specified color(e.g. RED): This program creates a Sphere indicated by the name sphere( radius is passed as arguments). A PointLight is created named pointlight( with a specified color passed as an argument which is a point light source and radiates light in all direction. 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 and pointlight are attached. The group is attached to the scene. Finally, the show() method is called to display the final results. A perspective camera will be created and added to the scene to render the cylinder in 3D.
// Java program to create a point light// of specified color(eg RED)importjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.shape.DrawMode;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.PointLight;importjavafx.scene.shape.Sphere;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;importjavafx.scene.PerspectiveCamera;importjavafx.scene.paint.Color;publicclasssphere_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating sphere");// create a sphereSphere sphere =newSphere(80.0f);// create a point lightPointLight pointlight =newPointLight(Color.RED);// create a GroupGroup group =newGroup(sphere, pointlight);// translate the sphere to a positionsphere.setTranslateX(100);sphere.setTranslateY(100);// translate point lightpointlight.setTranslateZ(-1000);pointlight.setTranslateX(+1000);pointlight.setTranslateY(+10);// create a perspective cameraPerspectiveCamera camera =newPerspectiveCamera(false);camera.setTranslateX(0);camera.setTranslateY(0);camera.setTranslateZ(0);// create a sceneScene scene =newScene(group,500,300);scene.setCamera(camera);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}
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/PointLight.html




