Monday, June 20, 2016

53. Point Light

A point light originates at a certain point in space (x,y,z).


We can change x and y, with the 2 sliders, within a range, and can see the light moving. Since we set the BlockIncrement to 5, pressing the arrows will move in increments or decrements of 5 as left and right arrows are pressed, for the slider in focus.


package ex53;

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

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

        Light.Point pointLight = new Light.Point(200,100,75,Color.GRAY);
        
        Lighting lighting = new Lighting(pointLight);
        lighting.setSurfaceScale(5);
        lighting.setSpecularExponent(5);

        Text text = new Text(80, 200, "Point 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,200);
        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 -> {
            pointLight.setX(sliderX.getValue());
        });
        
        Slider sliderY = new Slider(50,275,100);
        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 -> {
            pointLight.setY(sliderY.getValue());
        });
        
        root.getChildren().addAll(rect, text,
                sliderX, labelX,
                sliderY, labelY);

        Scene scene = new Scene(root, 500, 600, Color.TURQUOISE);
        stage.setTitle("Example 53. PointLight");
        stage.setScene(scene);
        stage.show();
    }

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

This is the output at a certain x and y where light seems to be centered:


No comments:

Post a Comment