Wednesday 4 November 2020

JavaFX: Lighting

The lighting effect simulates a light source shining on the given content, which can be used to give flat objects a more realistic three-dimensional appearance.

 

Why lighting?

Makes scenes look like 3D.

 

Light class

Light class is an abstract base class for all light implementations.

 

Different types of Light implementations

JavaFX support 3 different types of lights.

a.   Distant light (Light.Distant)

b.   Point light (Light.Point) 

c. Spot light (Light.Spot)

 


Lighting class

Lighting an effect that simulates a light source shining on the given content, which can be used to give flat objects a more realistic, three-dimensional appearance.

 

You need to create a Lighting instance with specific Light instance and apply Lighting effect a node.

 

Example

Light.Distant distantLight = new Light.Distant();

distantLight.setAzimuth(160);

 

Lighting lighting = new Lighting(distantLight);

lighting.setSurfaceScale(10);

 

text1.setEffect(lighting);

 

Find the below working application.

lightingEffectDemo.fxml

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.effects.controller.LightingController">
	
	<Text fx:id = "text1" text="With Light"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	underline="true"
	x="50" y = "100"/>
	
	<Text fx:id = "text2" text="Without Light"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	underline="true"
	x="50" y = "200"/>

</Group>

LightingController.java

package com.sample.app.effects.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.text.Text;

public class LightingController implements Initializable {

	@FXML
	private Text text1;
	
	@FXML
	private Text text2;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		Light.Distant distantLight = new Light.Distant();
		distantLight.setAzimuth(160);

		Lighting lighting = new Lighting(distantLight);
		lighting.setSurfaceScale(10);

		text1.setEffect(lighting);

	}

}

LightingDemo.java

package com.sample.app.effects;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class LightingDemo extends Application {

	private static final String FXML_FILE = "/lightingEffectDemo.fxml";
	private static final String STAGE_TITLE = "Lighting Effect";

	public static void main(String args[]) {
		launch(args);

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

		Scene scene = new Scene(root, 600, 400, Color.WHITE);

		primaryStage.setTitle(STAGE_TITLE);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

Output



Previous                                                    Next                                                    Home

No comments:

Post a Comment