Monday, June 20, 2016

54. Spot Light

A spotlight is similar to point light in that it originates at a certain point. It is directional and the direction of the light is based on PointsAt x, y, and z.


We change PointsAt x and y using the sliders.


package ex54;

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 Ex54 extends Application {

    @Override
    public void start(Stage stage) {
        Group root = new Group();

        Light.Spot spotLight = new Light.Spot(100,50,45,1,Color.WHITE);
        spotLight.setPointsAtX(200);    
        spotLight.setPointsAtY(400);
        spotLight.setPointsAtZ(10);

        Lighting lighting = new Lighting(spotLight);
        lighting.setSurfaceScale(5);
        lighting.setSpecularExponent(10);
        lighting.setSpecularConstant(2);

        Text text = new Text(80, 200, "Spot Light");
        text.setFont(Font.font("Georgia", FontWeight.BOLD, 56));
        text.setFill(Color.GREEN);
        text.setEffect(lighting);
        
        Rectangle rect = new Rectangle(50, 50, 400, 300);
        rect.setFill(Color.RED);
        rect.setEffect(lighting);
        
        Slider sliderX = new Slider(100,300,100);
        sliderX.setBlockIncrement(5);
        sliderX.setLayoutX(100); sliderX.setLayoutY(400);
        
        Label labX = new Label("x");
        labX.setLayoutX(50); labX.setLayoutY(425);
        labX.setFont(Font.font("Georgia", FontWeight.BOLD, 32));
        Label labY = new Label("y");
        labY.setLayoutX(50); labY.setLayoutY(525);
        labY.setFont(Font.font("Georgia", FontWeight.BOLD, 32));
        root.getChildren().addAll(labX, labY);
        
        Label labelX = new Label();
        labelX.setLayoutX(100); labelX.setLayoutY(425);
        labelX.setFont(Font.font("Georgia", FontWeight.BOLD, 32));
        labelX.textProperty().bind(sliderX.valueProperty().asString());
        
        sliderX.valueProperty().addListener(e -> {
            spotLight.setPointsAtX(sliderX.getValue());
        });
        
        Slider sliderY = new Slider(50,275,50);
        sliderY.setBlockIncrement(5);
        sliderY.setLayoutX(100); sliderY.setLayoutY(500);
        
        Label labelY = new Label();
        labelY.setLayoutX(100); labelY.setLayoutY(525);
        labelY.setFont(Font.font("Georgia", FontWeight.BOLD, 32));
        labelY.textProperty().bind(sliderY.valueProperty().asString());
        
        sliderY.valueProperty().addListener(e -> {
            spotLight.setPointsAtY(sliderY.getValue());
        });
        
        root.getChildren().addAll(rect, text,
                sliderX, labelX,
                sliderY, labelY);       
        
        Scene scene = new Scene(root, 500, 600, Color.LIGHTYELLOW);
        stage.setTitle("Example 54. SpotLight");
        stage.setScene(scene);
        stage.show();
    }

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


This is the output:


No comments:

Post a Comment