Monday, June 20, 2016

52. Distant Light

Distant Light is one of the 3 kinds of lighting possible in JavaFX.


With the sliders, we can change angle of light. By changing the slider position light with come from right (0), downwards (90), left (180), and up (270) and we can see the corresponding sides lighter.


package ex52;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Ex52 extends Application {

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        
        Light.Distant distantLight = new Light.Distant();
        distantLight.setAzimuth(0);
        Lighting lighting = new Lighting(distantLight);
        lighting.setSurfaceScale(5);
        
        Text text = new Text(40, 110, "Distant Light");
        text.setFont(Font.font("Georgia", FontWeight.BOLD, 64));
        text.setFill(Color.GREEN);
        text.setEffect(lighting);
        
        Rectangle rectangle = new Rectangle(20, 40, 470, 100);
        rectangle.setFill(Color.LIGHTGRAY);
        rectangle.setEffect(lighting);
        
        Slider slider = new Slider(0,360,0);
        slider.setBlockIncrement(15);
        slider.setLayoutX(200); slider.setLayoutY(200);
        
        Label label = new Label();
        label.setLayoutX(200); label.setLayoutY(220);
        label.setFont(Font.font("Georgia", FontWeight.BOLD, 32));
        label.textProperty().bind(slider.valueProperty().asString());
        
        root.getChildren().addAll(rectangle, text, slider, label);
        
        slider.valueProperty().addListener(e -> {
            distantLight.setAzimuth(slider.getValue());
        });
        
        Scene scene = new Scene(root, 500, 300, Color.LIGHTCYAN);
        stage.setTitle("Example 52. Distant Light");
        stage.setScene(scene);
        stage.show();
    }

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

This is the output at selected slider position. The down side is lighter since light is being shone towards that direction.


No comments:

Post a Comment