Friday, September 25, 2015

JavaFX Lighting effect of Light.Spot


javafx.scene.effect.Light.Spot represents a spot light source at a given position in 3D space, with configurable direction and focus.


package javafx_lighting;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Lighting extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        //Light.Spot: Represents a spot light source at a given position in 
        //3D space, with configurable direction and focus.
        Light.Spot lightSpot = new Light.Spot();
        lightSpot.setX(0);
        lightSpot.setY(0);
        lightSpot.setZ(0);
        lightSpot.setColor(Color.ANTIQUEWHITE);
        Lighting lighting = new Lighting();
        lighting.setLight(lightSpot);
        lighting.setSurfaceScale(5.0);
        lighting.setDiffuseConstant(1.0);
        
        ColorPicker colorPicker = new ColorPicker(Color.ANTIQUEWHITE);
        colorPicker.setOnAction(new EventHandler() {
            @Override
            public void handle(Event t) {
                lightSpot.setColor(colorPicker.getValue());
            }
        });
 
        Button btn = new Button("Button with Lighting");
        btn.setEffect(lighting);

        TextField textField = new TextField("TextField with Lighting");
        textField.setEffect(lighting);
        
        Text text = new Text("Text with lighting");
        text.setFont(Font.font ("Verdana", FontWeight.BOLD, 40));
        text.setFill(Color.WHITE);
        text.setEffect(lighting);

        Circle circle = new Circle(20,Color.WHITE);
        circle.setEffect(lighting);

        Rectangle rectangle = new Rectangle(50, 50, Color.WHITE);
        rectangle.setEffect(lighting);
        
        ImageView imageView = new ImageView(new Image("http://goo.gl/kYEQl"));
        imageView.setEffect(lighting);
        
        HBox hBoxShape = new HBox();
        hBoxShape.getChildren().addAll(rectangle, circle, imageView);

        Label labelXYZ = new Label("X, Y, Z");
        Slider sliderX = new Slider();
        sliderX.setMin(0);
        sliderX.setMax(900.0);
        sliderX.setValue(0);
        sliderX.setMajorTickUnit(100);
        sliderX.setMinorTickCount(2);
        sliderX.setShowTickLabels(true);
        sliderX.setShowTickMarks(true);
        sliderX.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightSpot.setX((Double)new_val);
        });

        Slider sliderY = new Slider();
        sliderY.setMin(0);
        sliderY.setMax(500.0);
        sliderY.setValue(0);
        sliderY.setMajorTickUnit(100);
        sliderY.setMinorTickCount(2);
        sliderY.setShowTickLabels(true);
        sliderY.setShowTickMarks(true);
        sliderY.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightSpot.setY((Double)new_val);
        });

        Slider sliderZ = new Slider();
        sliderZ.setMin(0);
        sliderZ.setMax(500.0);
        sliderZ.setValue(0);
        sliderZ.setMajorTickUnit(100);
        sliderZ.setMinorTickCount(2);
        sliderZ.setShowTickLabels(true);
        sliderZ.setShowTickMarks(true);
        sliderZ.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightSpot.setZ((Double)new_val);
        });
        
        //
        Label labelPointsAtX = new Label("PointsAtX, PointsAtY, PointsAtZ");
        
        Slider sliderPointsAtX = new Slider();
        sliderPointsAtX.setMin(0);
        sliderPointsAtX.setMax(900.0);
        sliderPointsAtX.setValue(0);
        sliderPointsAtX.setMajorTickUnit(300);
        sliderPointsAtX.setMinorTickCount(3);
        sliderPointsAtX.setShowTickLabels(true);
        sliderPointsAtX.setShowTickMarks(true);
        sliderPointsAtX.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightSpot.setPointsAtX((Double)new_val);
        });
        
        Slider sliderPointsAtY = new Slider();
        sliderPointsAtY.setMin(0);
        sliderPointsAtY.setMax(500.0);
        sliderPointsAtY.setValue(0);
        sliderPointsAtY.setMajorTickUnit(100);
        sliderPointsAtY.setMinorTickCount(2);
        sliderPointsAtY.setShowTickLabels(true);
        sliderPointsAtY.setShowTickMarks(true);
        sliderPointsAtY.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightSpot.setPointsAtY((Double)new_val);
        });
        
        Slider sliderPointsAtZ = new Slider();
        sliderPointsAtZ.setMin(0);
        sliderPointsAtZ.setMax(500.0);
        sliderPointsAtZ.setValue(0);
        sliderPointsAtZ.setMajorTickUnit(100);
        sliderPointsAtZ.setMinorTickCount(2);
        sliderPointsAtZ.setShowTickLabels(true);
        sliderPointsAtZ.setShowTickMarks(true);
        sliderPointsAtZ.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightSpot.setPointsAtZ((Double)new_val);
        });

        //
        
        Label labelSurfaceScale = new Label("SurfaceScale");
        Slider sliderSurfaceScale = new Slider();
        sliderSurfaceScale.setMin(0);
        sliderSurfaceScale.setMax(10);
        sliderSurfaceScale.setValue(5.0);
        sliderSurfaceScale.setMajorTickUnit(1);
        sliderSurfaceScale.setMinorTickCount(2);
        sliderSurfaceScale.setShowTickLabels(true);
        sliderSurfaceScale.setShowTickMarks(true);
        sliderSurfaceScale.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lighting.setSurfaceScale((Double)new_val);
        });

        Label labelDiffuse = new Label("DiffuseConstant");
        Slider sliderDiffuse = new Slider();
        sliderDiffuse.setMin(0);
        sliderDiffuse.setMax(2);
        sliderDiffuse.setValue(1);
        sliderDiffuse.setMajorTickUnit(1);
        sliderDiffuse.setMinorTickCount(4);
        sliderDiffuse.setShowTickLabels(true);
        sliderDiffuse.setShowTickMarks(true);
        sliderDiffuse.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lighting.setDiffuseConstant((Double)new_val);
        });
        
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(10, 10, 10, 10));
        vBox.getChildren().addAll(colorPicker, btn, text, textField, hBoxShape, 
                labelXYZ, sliderX, sliderY, sliderZ,
                labelPointsAtX, sliderPointsAtX, sliderPointsAtY, sliderPointsAtZ,
                labelSurfaceScale, sliderSurfaceScale, labelDiffuse, sliderDiffuse);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 1000, 600);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

JavaFX Lighting effect of Light.Point


javafx.scene.effect.Light.Point represents a light source at a given position in 3D space.


package javafx_lighting;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Lighting extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        //Light.Point: Represents a light source at a given position in 3D space.
        Light.Point lightPoint = new Light.Point();
        lightPoint.setX(0);
        lightPoint.setY(0);
        lightPoint.setZ(0);
        lightPoint.setColor(Color.GOLD);
        Lighting lighting = new Lighting();
        lighting.setLight(lightPoint);
        lighting.setSurfaceScale(5.0);
        lighting.setDiffuseConstant(1.0);
        
        ColorPicker colorPicker = new ColorPicker(Color.GOLD);
        colorPicker.setOnAction(new EventHandler() {
            @Override
            public void handle(Event t) {
                lightPoint.setColor(colorPicker.getValue());
            }
        });
 
        Button btn = new Button("Button with Lighting");
        btn.setEffect(lighting);

        TextField textField = new TextField("TextField with Lighting");
        textField.setEffect(lighting);
        
        Text text = new Text("Text with lighting");
        text.setFont(Font.font ("Verdana", FontWeight.BOLD, 40));
        text.setFill(Color.WHITE);
        text.setEffect(lighting);

        Circle circle = new Circle(20,Color.WHITE);
        circle.setEffect(lighting);

        Rectangle rectangle = new Rectangle(50, 50, Color.WHITE);
        rectangle.setEffect(lighting);
        
        ImageView imageView = new ImageView(new Image("http://goo.gl/kYEQl"));
        imageView.setEffect(lighting);
        
        HBox hBoxShape = new HBox();
        hBoxShape.getChildren().addAll(rectangle, circle, imageView);

        Label labelX = new Label("X: "
                + "The x coordinate of the light position.");
        Slider sliderX = new Slider();
        sliderX.setMin(0);
        sliderX.setMax(900.0);
        sliderX.setValue(0);
        sliderX.setMajorTickUnit(100);
        sliderX.setMinorTickCount(2);
        sliderX.setShowTickLabels(true);
        sliderX.setShowTickMarks(true);
        sliderX.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightPoint.setX((Double)new_val);
        });
        
        Label labelY = new Label("Y: "
                + "The y coordinate of the light position.");
        Slider sliderY = new Slider();
        sliderY.setMin(0);
        sliderY.setMax(500.0);
        sliderY.setValue(0);
        sliderY.setMajorTickUnit(100);
        sliderY.setMinorTickCount(2);
        sliderY.setShowTickLabels(true);
        sliderY.setShowTickMarks(true);
        sliderY.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightPoint.setY((Double)new_val);
        });
        
        Label labelZ = new Label("Z: "
                + "The z coordinate of the light position.");
        Slider sliderZ = new Slider();
        sliderZ.setMin(0);
        sliderZ.setMax(500.0);
        sliderZ.setValue(0);
        sliderZ.setMajorTickUnit(100);
        sliderZ.setMinorTickCount(2);
        sliderZ.setShowTickLabels(true);
        sliderZ.setShowTickMarks(true);
        sliderZ.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lightPoint.setZ((Double)new_val);
        });
        
        Label labelSurfaceScale = new Label("SurfaceScale");
        Slider sliderSurfaceScale = new Slider();
        sliderSurfaceScale.setMin(0);
        sliderSurfaceScale.setMax(10);
        sliderSurfaceScale.setValue(1.5);
        sliderSurfaceScale.setMajorTickUnit(1);
        sliderSurfaceScale.setMinorTickCount(2);
        sliderSurfaceScale.setShowTickLabels(true);
        sliderSurfaceScale.setShowTickMarks(true);
        sliderSurfaceScale.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lighting.setSurfaceScale((Double)new_val);
        });
        
        Label labelDiffuse = new Label("DiffuseConstant");
        Slider sliderDiffuse = new Slider();
        sliderDiffuse.setMin(0);
        sliderDiffuse.setMax(2);
        sliderDiffuse.setValue(1);
        sliderDiffuse.setMajorTickUnit(1);
        sliderDiffuse.setMinorTickCount(4);
        sliderDiffuse.setShowTickLabels(true);
        sliderDiffuse.setShowTickMarks(true);
        sliderDiffuse.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lighting.setDiffuseConstant((Double)new_val);
        });
        
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(10, 10, 10, 10));
        vBox.getChildren().addAll(colorPicker, btn, text, textField, hBoxShape, 
                labelX, sliderX, labelY, sliderY, labelZ, sliderZ,
                labelSurfaceScale, sliderSurfaceScale, labelDiffuse, sliderDiffuse);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 900, 500);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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



Related:
- JavaFX Lighting effect of Light.Distant

Tuesday, September 22, 2015

Interactive demo of JavaFX Lighting effect of Light.Distant


JavaFX_Lighting.java
package javafx_lighting;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Lighting extends Application {
    
    @Override
    public void start(Stage primaryStage) {

        //Light.Distant: Represents a distant light source.
        Light.Distant light = new Light.Distant();
        light.setAzimuth(45);
        light.setElevation(45);
        light.setColor(Color.AZURE);
        Lighting lighting = new Lighting();
        lighting.setLight(light);
        lighting.setSurfaceScale(5.0);
        lighting.setDiffuseConstant(1.0);
        
        ColorPicker colorPicker = new ColorPicker(Color.AZURE);
        colorPicker.setOnAction(new EventHandler() {
            @Override
            public void handle(Event t) {
                light.setColor(colorPicker.getValue());
            }
        });
 
        Button btn = new Button("Button with Lighting");
        btn.setEffect(lighting);

        TextField textField = new TextField("TextField with Lighting");
        textField.setEffect(lighting);
        
        Text text = new Text("Text with lighting");
        text.setFont(Font.font ("Verdana", FontWeight.BOLD, 40));
        text.setFill(Color.WHITE);
        text.setEffect(lighting);

        Circle circle = new Circle(20,  Color.rgb(255,255,255));
        circle.setEffect(lighting);

        Rectangle rectangle = new Rectangle(50, 50, Color.rgb(255,255,255));
        rectangle.setEffect(lighting);
        
        ImageView imageView = new ImageView(new Image("http://goo.gl/kYEQl"));
        imageView.setEffect(lighting);
        
        HBox hBoxShape = new HBox();
        hBoxShape.getChildren().addAll(rectangle, circle, imageView);

        Label labelAzimuth = new Label("Azimuth: "
                + "the direction angle for the light source on the XY plane.");
        Slider sliderAzimuth = new Slider();
        sliderAzimuth.setMin(0);
        sliderAzimuth.setMax(360);
        sliderAzimuth.setValue(45);
        sliderAzimuth.setMajorTickUnit(90);
        sliderAzimuth.setMinorTickCount(3);
        sliderAzimuth.setShowTickLabels(true);
        sliderAzimuth.setShowTickMarks(true);
        sliderAzimuth.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            light.setAzimuth((Double)new_val);
        });
        
        Label labelElevation = new Label("Elevation: "
                + "the direction angle for the light source on the YZ plane.");
        Slider sliderElevation = new Slider();
        sliderElevation.setMin(0);
        sliderElevation.setMax(360);
        sliderElevation.setValue(45);
        sliderElevation.setMajorTickUnit(90);
        sliderElevation.setMinorTickCount(3);
        sliderElevation.setShowTickLabels(true);
        sliderElevation.setShowTickMarks(true);
        sliderElevation.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            light.setElevation((Double)new_val);
        });
        
        Label labelSurfaceScale = new Label("SurfaceScale");
        Slider sliderSurfaceScale = new Slider();
        sliderSurfaceScale.setMin(0);
        sliderSurfaceScale.setMax(10);
        sliderSurfaceScale.setValue(1.5);
        sliderSurfaceScale.setMajorTickUnit(1);
        sliderSurfaceScale.setMinorTickCount(2);
        sliderSurfaceScale.setShowTickLabels(true);
        sliderSurfaceScale.setShowTickMarks(true);
        sliderSurfaceScale.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lighting.setSurfaceScale((Double)new_val);
        });
        
        Label labelDiffuse = new Label("DiffuseConstant");
        Slider sliderDiffuse = new Slider();
        sliderDiffuse.setMin(0);
        sliderDiffuse.setMax(2);
        sliderDiffuse.setValue(1);
        sliderDiffuse.setMajorTickUnit(1);
        sliderDiffuse.setMinorTickCount(4);
        sliderDiffuse.setShowTickLabels(true);
        sliderDiffuse.setShowTickMarks(true);
        sliderDiffuse.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            lighting.setDiffuseConstant((Double)new_val);
        });
        
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(10, 10, 10, 10));
        vBox.getChildren().addAll(colorPicker, btn, text, textField, hBoxShape, 
                labelAzimuth, sliderAzimuth, labelElevation, sliderElevation,
                labelSurfaceScale, sliderSurfaceScale, labelDiffuse, sliderDiffuse);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 853, 480);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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



Related:
- JavaFX Lighting effect of Light.Point

JavaFX Lighting effect


package javafx_lighting;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.effect.Lighting;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Lighting extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn1 = new Button();
        btn1.setText("Normal Button");
        Button btn2 = new Button();
        btn2.setText("Button with Lighting");
        btn2.setEffect(new Lighting());
        
        TextField textField1 = new TextField("Normal TextField");
        TextField textField2 = new TextField("TextField with Lighting");
        textField2.setEffect(new Lighting());
        
        Text text1 = new Text("Normal Text");
        text1.setFont(Font.font ("Verdana", FontWeight.BOLD, 40));
        text1.setFill(Color.RED);
        Text text2 = new Text("Text with lighting");
        text2.setFont(Font.font ("Verdana", FontWeight.BOLD, 40));
        text2.setFill(Color.RED);
        text2.setEffect(new Lighting());
        
        Circle circle1 = new Circle(20,  Color.rgb(156,216,255));
        Circle circle2 = new Circle(20,  Color.rgb(156,216,255));
        circle2.setEffect(new Lighting());
        
        Rectangle rectangle1 = new Rectangle(100, 100, Color.rgb(156,216,255));
        Rectangle rectangle2 = new Rectangle(100, 100, Color.rgb(156,216,255));
        rectangle2.setEffect(new Lighting());
        HBox hBoxShape = new HBox();
        hBoxShape.getChildren().addAll(rectangle1, rectangle2, circle1, circle2);

        ImageView imageView1 = new ImageView(new Image("http://goo.gl/kYEQl"));
        ImageView imageView2 = new ImageView(new Image("http://goo.gl/kYEQl"));
        imageView2.setEffect(new Lighting());
        HBox hBoxImageView = new HBox();
        hBoxImageView.getChildren().addAll(imageView1, imageView2);

        VBox vBox = new VBox();
        vBox.setPadding(new Insets(10, 10, 10, 10));
        vBox.getChildren().addAll(btn1, btn2, text1, text2, 
                textField1, textField2,
                hBoxShape, hBoxImageView);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 450, 400);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Thursday, September 17, 2015

JavaFX Transition Animation with Reflection effect

JavaFX example to combine Transition Animation and Reflection effect.



package javafx_transitionexample;

import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_TransitionExample extends Application {

    @Override
    public void start(Stage primaryStage) {

        //Transition Example
        //ref: http://docs.oracle.com/javafx/2/animations/basics.htm
        ImageView imageView = new ImageView(new Image("http://goo.gl/kYEQl"));
        
        Reflection reflection = new Reflection();
        imageView.setEffect(reflection);

        FadeTransition fadeTransition
                = new FadeTransition(Duration.millis(3000), imageView);
        fadeTransition.setFromValue(1.0f);
        fadeTransition.setToValue(0.3f);
        fadeTransition.setCycleCount(2);
        fadeTransition.setAutoReverse(true);
         
        TranslateTransition translateTransition
                = new TranslateTransition(Duration.millis(2000), imageView);
        translateTransition.setFromX(0);
        translateTransition.setToX(350);
        translateTransition.setCycleCount(2);
        translateTransition.setAutoReverse(true);
 
        RotateTransition rotateTransition
                = new RotateTransition(Duration.millis(3000), imageView);
        rotateTransition.setByAngle(180f);
        rotateTransition.setCycleCount(4);
        rotateTransition.setAutoReverse(true);
 
        ScaleTransition scaleTransition
                = new ScaleTransition(Duration.millis(2000), imageView);
        scaleTransition.setToX(2f);
        scaleTransition.setToY(2f);
        scaleTransition.setCycleCount(2);
        scaleTransition.setAutoReverse(true);
 
        ParallelTransition parallelTransition = 
                 new ParallelTransition();
        parallelTransition.getChildren().addAll(
                fadeTransition,
                translateTransition,
                rotateTransition,
                scaleTransition);
         parallelTransition.setCycleCount(1);
         //parallelTransition.play();
         
        SequentialTransition sequentialTransition = 
                new SequentialTransition();
        sequentialTransition.getChildren().addAll(
                fadeTransition,
                translateTransition,
                rotateTransition,
                scaleTransition);
        sequentialTransition.setCycleCount(1);
        //sequentialTransition.play();
        //
         
        Button btnParallelTransitionPlay = 
                new Button("parallelTransition.play");
        btnParallelTransitionPlay.setOnAction((ActionEvent event) -> {
            parallelTransition.play();
        });
 
        Button btnSequentialTransitionPlay = 
                new Button("sequentialTransition.play");
        btnSequentialTransitionPlay.setOnAction((ActionEvent event) -> {
            sequentialTransition.play();
        });
 
        HBox hbox = new HBox();
        hbox.getChildren().addAll(
                btnParallelTransitionPlay,
                imageView,
                btnSequentialTransitionPlay);
         
        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version") + "\n"
                + "os.name: " + System.getProperty("os.name")
        );
         
        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, hbox);
         
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
 
        Scene scene = new Scene(root, 700, 250);
 
        primaryStage.setTitle("java-buddy: Transition Example "
                + "- ParallelTransition/SequentialTransition");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}


JavaFX Reflection Effect

Example of using JavaFX Reflection Effect, javafx.scene.effect.Reflection.


package javafx_reflection;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_Reflection extends Application {

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("java-buddy.blogspot.com");
        Group root = new Group();
        Scene scene = new Scene(root, 700, 600, Color.WHITE);

        Reflection reflection = new Reflection();
        
        Button btn = new Button("Click Me");
        btn.setOnAction((ActionEvent event) -> {
            if(btn.getText().equals("Click Me")){
                btn.setText("Hello! Thanks...");
                reflection.setBottomOpacity(1.0);
            }else{
                btn.setText("Click Me");
                reflection.setBottomOpacity(0.0);
            }
            
        });
        btn.setEffect(reflection);

        Text textView = new Text(50, 150, "java-buddy.blogspot.com");
        textView.setFill(Color.BLUE);
        textView.setFont(Font.font("Monospaced", 40));
        textView.setEffect(reflection);
        
        ImageView imageView = new ImageView(new Image("http://goo.gl/kYEQl"));
        imageView.setEffect(reflection);
        
        Slider slider = new Slider();
        slider.setMin(-50);
        slider.setMax(250);
        slider.setValue(0);
        slider.valueProperty().addListener(
                (ObservableValue<? extends Number> ov, 
                        Number old_val, Number new_val) -> {
            reflection.setTopOffset((double) new_val);
        });

        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version") + "\n"
                + "os.name: " + System.getProperty("os.name")
        );
        labelInfo.setEffect(reflection);
        
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(20, 20, 20, 20));
        vBox.getChildren().addAll(labelInfo, imageView, textView, btn, slider);

        root.getChildren().add(vBox);

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

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

}




Next: JavaFX Transition Animation with Reflection effect

Tuesday, September 15, 2015

JavaFX example of Transition Animation, ParallelTransition and SequentialTransition

JavaFX example of Transition Animation, ParallelTransition and SequentialTransition.


JavaFX_TransitionExample.java
package javafx_transitionexample;

import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_TransitionExample extends Application {

    @Override
    public void start(Stage primaryStage) {

        //Transition Example
        //ref: http://docs.oracle.com/javafx/2/animations/basics.htm
        ImageView imageView = new ImageView(new Image("http://goo.gl/kYEQl"));

        FadeTransition fadeTransition
                = new FadeTransition(Duration.millis(3000), imageView);
        fadeTransition.setFromValue(1.0f);
        fadeTransition.setToValue(0.3f);
        fadeTransition.setCycleCount(2);
        fadeTransition.setAutoReverse(true);
        
        TranslateTransition translateTransition
                = new TranslateTransition(Duration.millis(2000), imageView);
        translateTransition.setFromX(0);
        translateTransition.setToX(350);
        translateTransition.setCycleCount(2);
        translateTransition.setAutoReverse(true);

        RotateTransition rotateTransition
                = new RotateTransition(Duration.millis(3000), imageView);
        rotateTransition.setByAngle(180f);
        rotateTransition.setCycleCount(4);
        rotateTransition.setAutoReverse(true);

        ScaleTransition scaleTransition
                = new ScaleTransition(Duration.millis(2000), imageView);
        scaleTransition.setToX(2f);
        scaleTransition.setToY(2f);
        scaleTransition.setCycleCount(2);
        scaleTransition.setAutoReverse(true);

        ParallelTransition parallelTransition = 
                 new ParallelTransition();
        parallelTransition.getChildren().addAll(
                fadeTransition,
                translateTransition,
                rotateTransition,
                scaleTransition);
         parallelTransition.setCycleCount(1);
         //parallelTransition.play();
        
        SequentialTransition sequentialTransition = 
                new SequentialTransition();
        sequentialTransition.getChildren().addAll(
                fadeTransition,
                translateTransition,
                rotateTransition,
                scaleTransition);
        sequentialTransition.setCycleCount(1);
        //sequentialTransition.play();
        //
        
        Button btnParallelTransitionPlay = 
                new Button("parallelTransition.play");
        btnParallelTransitionPlay.setOnAction((ActionEvent event) -> {
            parallelTransition.play();
        });

        Button btnSequentialTransitionPlay = 
                new Button("sequentialTransition.play");
        btnSequentialTransitionPlay.setOnAction((ActionEvent event) -> {
            sequentialTransition.play();
        });

        HBox hbox = new HBox();
        hbox.getChildren().addAll(
                btnParallelTransitionPlay,
                imageView,
                btnSequentialTransitionPlay);
        
        Label labelInfo = new Label();
        labelInfo.setText(
                "java.version: " + System.getProperty("java.version") + "\n"
                + "javafx.runtime.version: " + System.getProperty("javafx.runtime.version") + "\n"
                + "os.name: " + System.getProperty("os.name")
        );
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(labelInfo, hbox);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 700, 250);

        primaryStage.setTitle("java-buddy: Transition Example "
                + "- ParallelTransition/SequentialTransition");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}





Next: JavaFX Transition Animation with Reflection effect

Java + JavaFX + jSSC communicate with Arduino Board, run on Windows 10 and Raspberry Pi

It's a series of examples to program Java + JavaFX + jSSC(java-simple-serial-connector) on Windows 10 with NetBeans IDE. Run on Windows 10, to communicate with Arduino Board. And finally show how to remote deploy on Raspberry Pi using NetBeans IDE Remote Java SE Platform.

Prepare jSSC - download and add library to NetBeans, and create project using it
Example of using jSSC, communicate between JavaFX and Arduino Uno via USB Serial port
- JavaFX + jSSC - read byte from Arduino Uno, read from Analog Input

- JavaFX + jSSC - read byte from Arduino Uno, display in LineChart

- Bi-direction communication between Arduino and PC using Java + jSSC


- Java + JavaFX + jSSC run on Raspberry Pi, control Arduino Uno
(with setup Remote Java SE Platform on Netbeans/Windows 10, remote deploy to Raspberry Pi)



Sunday, September 13, 2015

OpenCV Computer Vision with Java

Create multiplatform computer vision desktop and web applications using the combination of OpenCV and Java

About This Book
  • Set up Java API for OpenCV to create popular Swing-based Graphical User Interfaces (GUIs)
  • Process videos and images in real-time with closer to native performance
  • Make use of rock solid Java web application development practices to create engaging augmented reality experience and work with depth images from a Kinect device
Who This Book Is For
If you are a Java developer, student, researcher, or hobbyist wanting to create computer vision applications in Java then this book is for you. If you are an experienced C/C++ developer who is used to working with OpenCV, you will also find this book very useful for migrating your applications to Java.

All you need is basic knowledge of Java, with no prior understanding of computer vision required, as this book will give you clear explanations and examples of the basics.

What You Will Learn
  • Create powerful GUIs for computer vision applications with panels, scroll panes, radio buttons, sliders, windows, and mouse interaction using the popular Swing GUI widget toolkit
  • Stretch, shrink, warp, and rotate images, as well as apply image transforms to find edges, lines, and circles, and even use Discrete Fourier Transforms (DFT)
  • Detect foreground or background regions and work with depth images with a Kinect device
  • Learn how to add computer vision capabilities to rock solid Java web applications allowing you to upload photos and create astonishing effects
  • Track faces and apply mixed reality effects such as adding virtual hats to uploaded photos
  • Filter noisy images, work with morphological operators, use flood fill, and threshold the important regions of an image
  • Open and process video streams from webcams or video files
In Detail
OpenCV 3.0 Computer Vision with Java is a practical tutorial guide that explains fundamental tasks from computer vision while focusing on Java development. This book will teach you how to set up OpenCV for Java and handle matrices using the basic operations of image processing such as filtering and image transforms. It will also help you learn how to use Haar cascades for tracking faces and to detect foreground and background regions with the help of a Kinect device. It will even give you insights into server-side OpenCV. Each chapter is presented with several projects that are ready to use. The functionality of these projects is found in many classes that allow developers to understand computer vision principles and rapidly extend or customize the projects for their needs.

Saturday, September 12, 2015

Test-Driven Java Development

Invoke TDD principles for end-to-end application development with Java

About This Book
  • Explore the most popular TDD tools and frameworks and become more proficient in building applications
  • Create applications with better code design, fewer bugs, and higher test coverage, enabling you to get them to market quickly
  • Implement test-driven programming methods into your development workflows
Who This Book Is For
If you're an experienced Java developer and want to implement more effective methods of programming systems and applications, then this book is for you.

What You Will Learn
  • Explore the tools and frameworks required for effective TDD development
  • Perform the Red-Green-Refactor process efficiently, the pillar around which all other TDD procedures are based
  • Master effective unit testing in isolation from the rest of your code
  • Design simple and easily maintainable codes by implementing different techniques
  • Use mocking frameworks and techniques to easily write and quickly execute tests
  • Develop an application to implement behaviour-driven development in conjunction with unit testing
  • Enable and disable features using Feature Toggles
In Detail
Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to optimize it.

The value of performing TDD with Java, one of the most established programming languages, is to improve the productivity of programmers, the maintainability and performance of code, and develop a deeper understanding of the language and how to employ it effectively.

Starting with the basics of TDD and reasons why its adoption is beneficial, this book will take you from the first steps of TDD with Java until you are confident enough to embrace the practice in your day-to-day routine.

You'll be guided through setting up tools, frameworks, and the environment you need, and will dive right in to hands-on exercises with the goal of mastering one practice, tool, or framework at a time. You'll learn about the Red-Green-Refactor procedure, how to write unit tests, and how to use them as executable documentation.

With this book you'll also discover how to design simple and easily maintainable code, work with mocks, utilise behaviour-driven development, refactor old legacy code, and release a half-finished feature to production with feature toggles.

You will finish this book with a deep understanding of the test-driven development methodology and the confidence to apply it to application programming with Java.

Style and approach
An easy-to-follow, hands-on guide to building applications through effective coding practices. This book covers practical examples by introducing different problems, each one designed as a learning exercise to help you understand each aspect of TDD.

Thursday, September 10, 2015

Add data to JavaFX LineChart, and shift out the oldest data.


JavaFX example to update LineChart: user enter new data to the LineChart associated series, and remove the oldest item from the series. I tried three approachs to achieve the goal. The first one is obviously wrong. Check the code to know my implement.


package javafx_linechart;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_LineChart extends Application {
    
    Label label1, label2, label3;
    XYChart.Series series1, series2, series3;
    final int MAX_NUM = 10;
    @Override
    public void start(Stage primaryStage) {

        TextField textField = new TextField();
        Button btn = new Button();
        btn.setText("Add item");
        btn.setOnAction((ActionEvent event) -> {
            try{
                int i = Integer.parseInt(textField.getText());
                System.out.println(String.valueOf(i));
                
                removeAndAdd(series1, i);
                reduceSeriesXValue(series2, i);
                shiftSeriesYValue(series3, i);
                
                printSeries(series1, label1);
                printSeries(series2, label2);
                printSeries(series3, label3);
                
            }catch (NumberFormatException ex){
                System.out.println(ex.toString());
            }
        });
        
        label1 = new Label();
        label2 = new Label();
        label3 = new Label();
        
        //prepare LineChart 1
        final NumberAxis xAxis1 = new NumberAxis();
        final NumberAxis yAxis1 = new NumberAxis();
        final LineChart<Number,Number> lineChart1 = 
                new LineChart<>(xAxis1,yAxis1);
        series1 = new XYChart.Series();
        series1.setName("series1");
        lineChart1.getData().add(series1);
        
        //prepare LineChart 2
        final NumberAxis xAxis2 = new NumberAxis();
        final NumberAxis yAxis2 = new NumberAxis();
        final LineChart<Number,Number> lineChart2 = 
                new LineChart<>(xAxis2,yAxis2);
        series2 = new XYChart.Series();
        series2.setName("series2");
        lineChart2.getData().add(series2);
        
        //prepare LineChart 3
        final NumberAxis xAxis3 = new NumberAxis();
        final NumberAxis yAxis3 = new NumberAxis();
        final LineChart<Number,Number> lineChart3 = 
                new LineChart<>(xAxis3,yAxis3);
        series3 = new XYChart.Series();
        series3.setName("series3");
        lineChart3.getData().add(series3);
        
        //init dummy data
        for(int i=0; i<MAX_NUM; i++){
            series1.getData().add(new XYChart.Data(i, i));
            series2.getData().add(new XYChart.Data(i, i));
            series3.getData().add(new XYChart.Data(i, i));
        }
        
        printSeries(series1, label1);
        printSeries(series2, label2);
        printSeries(series3, label3);
        
        VBox vBox1 = new VBox();
        vBox1.getChildren().addAll(lineChart1, label1);
        VBox vBox2 = new VBox();
        vBox2.getChildren().addAll(lineChart2, label2);
        VBox vBox3 = new VBox();
        vBox3.getChildren().addAll(lineChart3, label3);
        
        HBox chartBox = new HBox();
        chartBox.getChildren().addAll(vBox1, vBox2, vBox3);
        
        CheckBox cbAnimated = new CheckBox("animated");
        cbAnimated.setSelected(true);
        cbAnimated.selectedProperty().addListener(
                (ObservableValue<? extends Boolean> observable, 
                        Boolean oldValue, Boolean newValue) -> {
            lineChart1.setAnimated(newValue);
            lineChart2.setAnimated(newValue);
            lineChart3.setAnimated(newValue);
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(cbAnimated, textField, btn, chartBox);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 1000, 650);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    //Remove the first item, and add the new item - WRONG result!
    private void removeAndAdd(XYChart.Series series, int newValue){
        series.getData().remove(0);
        XYChart.Data newData = 
                new XYChart.Data(series.getData().size(), newValue);
        series.getData().add(newData);
    }
    
    //remove the first item
    //reduce XValue of all items by 1
    //add the new item
    public void reduceSeriesXValue(XYChart.Series series, int newValue){
        series.getData().remove(0);
        int numOfPoint = series.getData().size();
        for(int i=0; i<numOfPoint; i++){
            //reduce XValue
            XYChart.Data<Number, Number> data = 
                    (XYChart.Data<Number, Number>)series.getData().get(i);
            int x = (int)data.getXValue();
            data.setXValue(x-1);
        }
        
        series.getData().add(new XYChart.Data(numOfPoint, newValue));

    }
    
    //Shift all YValue
    //add the new item
    public void shiftSeriesYValue(XYChart.Series series, int newValue){
        int numOfPoint = series.getData().size();
        for(int i=0; i<numOfPoint-1; i++){
            XYChart.Data<Number, Number> ShiftDataUp = 
                    (XYChart.Data<Number, Number>)series.getData().get(i+1);
            Number shiftValue = ShiftDataUp.getYValue();
            XYChart.Data<Number, Number> ShiftDataDn = 
                    (XYChart.Data<Number, Number>)series.getData().get(i);
            ShiftDataDn.setYValue(shiftValue);
        }
        XYChart.Data<Number, Number> lastData = 
            (XYChart.Data<Number, Number>)series.getData().get(numOfPoint-1);
        lastData.setYValue(newValue);
    }
    
    //display the data of series
    private void printSeries(XYChart.Series series, Label label){
        String printOut = "";
        int numOfPoint = series.getData().size();
        for(int i=0; i<numOfPoint; i++){
            XYChart.Data<Number, Number> data = 
                    (XYChart.Data<Number, Number>)series.getData().get(i);
            printOut += i + " - " + data.getXValue() 
                    + " : " + data.getYValue() + "\n";
        }
        
        label.setText(printOut);
    }
    
    public static void main(String[] args) {
        launch(args);
    }
    
}

Sunday, September 6, 2015

Mastering NetBeans

Master building complex applications with NetBeans to become more proficient programmers


About This Book
  • Customize NetBeans to fit your unique needs
  • Excel in NetBeans IDE, learning the shortcuts and hidden features to become more productive
  • A comprehensive guide to become more productive at application development using NetBeans IDE
Who This Book Is For
If you are a competent developer who wants to fast-track your application development with NetBeans IDE, then this book is for you. Reasonable knowledge and an understanding of Java programming and NetBeans IDE is assumed.

What You Will Learn
  • Install NetBeans either from a distribution package or from source code
  • Test, debug, and run production code using the NetBeans IDE
  • Use external services such as PaaS environments and web services
  • Create desktop applications using Swing tools
  • Manage and configure relational databases
  • Build a Java business model and web tiers using Java EE and Spring technologies
  • Explore web services both with XML and RESTful approaches
  • Handle external services such as databases , Maven repositories, and cloud providers
  • Extend NetBeans for those situations where you require more from your IDE
In Detail
With the increasing complexity of software development and the abundance of tools available, learning your IDE in-depth will instantly increase your developer productivity. NetBeans is the only IDE that can be downloaded with Java itself and provides you with many cutting edge features not readily available with many IDEs. The IDE also provides a great set of tools for PHP and C/C++ developers. It is free and open source and has a large community of users and developers around the world.

This book will teach you to ace NetBeans IDE and make use of it in creating Java business and web services. It will help you to become a proficient developer and use NetBeans for software development. You will learn effective third-party interaction and enable yourself for productive database development.

Moving on, you will see how to create EJB projects and write effective and efficient web applications. Then you will learn how to use Swing and manage and configure a relational database. By the end of the book, you will be able to handle external services such as databases, Maven repositories, and cloud providers, and extend your NetBeans when you require more from your IDE.

Style and approach
An easy-to-follow yet comprehensive guide to help you master the exhaustive range of NetBeans features in order to become more efficient at Java programing. More advanced topics are covered in each chapter, with subjects grouped according to their complexity as well as their utility.

Friday, September 4, 2015

JavaFX example - set ViewPort of ImageView


Example to change ViewPort of ImageView, and rotate ImageView.

package javafx_imageview_viewport;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ImageView_Viewport extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        ImageView imageView1 = new ImageView(new Image("http://goo.gl/kYEQl"));
        
        //Example to rotate ImageView
        Image image2 = new Image("http://goo.gl/kYEQl");
        Rectangle2D viewportRect2 = new Rectangle2D(
                image2.getWidth()/4, 
                image2.getHeight()/4, 
                image2.getWidth()*3/4, 
                image2.getHeight()*3/4);
        ImageView imageView2 = new ImageView(image2);
        imageView2.setViewport(viewportRect2);
        
        Slider sliderRotate = new Slider();
        sliderRotate.setMin(0);
        sliderRotate.setMax(360);
        sliderRotate.setValue(0);
        sliderRotate.valueProperty().addListener(
                (ObservableValue<? extends Number> observable, 
                        Number oldValue, Number newValue) -> {
            imageView2.setRotate((double)newValue);
        });
        
        //Example to change ViewPort
        Image image3 = new Image("http://goo.gl/kYEQl");
        Rectangle2D viewportRect3 = new Rectangle2D(
                0, 
                0, 
                image3.getWidth(), 
                image3.getHeight());
        ImageView imageView3 = new ImageView(image3);
        imageView3.setViewport(viewportRect3);
        
        Slider sliderViewPort = new Slider();
        sliderViewPort.setMin(0);
        sliderViewPort.setMax(1.0);
        sliderViewPort.setValue(1.0);
        sliderViewPort.valueProperty().addListener(
                (ObservableValue<? extends Number> observable, 
                        Number oldValue, Number newValue) -> {
            Rectangle2D newViewportRect3 = new Rectangle2D(
                    0,
                    0,
                    (double)newValue*image3.getWidth(),
                    (double)newValue*image3.getHeight());
            imageView3.setViewport(newViewportRect3);
        });
        

        VBox vBox = new VBox();
        vBox.getChildren().addAll(imageView1, 
                imageView2, sliderRotate,
                imageView3, sliderViewPort);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 300, 350);
        
        primaryStage.setTitle("java-buddy: ImageVIew ViewPort");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}



Wednesday, September 2, 2015

JavaFX example: Label with image

JavaFX example to create Label with ImageView.


package javafx_imagelabel;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ImageLabel extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        /*
        //Create Label with ImageView
        Label imageLabel = new Label(
                "java-buddy.blogspot.com", 
                new ImageView(new Image("http://goo.gl/kYEQl")));
        */

        //Create Label, then add graphic to it
        Label imageLabel = new Label("java-buddy.blogspot.com");
        imageLabel.setGraphic(new ImageView(new Image("http://goo.gl/kYEQl")));

        
        StackPane root = new StackPane();
        root.getChildren().add(imageLabel);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Introducing JavaFX 8 Programming (Oracle Press)


Learn the Fundamentals of JavaFX 8 from Programming Guru Herb Schildt

Introducing JavaFX 8 Programming provides a fast-paced, practical introduction to JavaFX, Java’s next-generation GUI programming framework. In this easy-to-read guide, best-selling author Herb Schildt presents the key topics and concepts you’ll need to start developing modern, dynamic JavaFX GUI applications. The book begins with the fundamentals, including the general form of a JavaFX program. You then advance to event handling, controls, images, fonts, layouts, effects, transforms, animations (including 3-D animations), menus, and more. Numerous complete examples are included that put key topics and techniques into action. Designed for Java programmers, the book’s focus is on the JavaFX API and all examples are written entirely in Java. Best of all, the book is written in the clear, crisp, uncompromising style that has made Herb Schildt the choice of millions worldwide.

  • Learn the general form of a JavaFX program
  • Work with scenes and stages
  • Understand the fundamentals of JavaFX event handling
  • Explore several controls, such as buttons, list views, sliders, trees, tables, scroll panes, and more
  • Work with images, fonts, and layouts
  • Explore the JavaFX menu system
  • Use visual effects and transforms
  • Incorporate 2-D and 3-D animation
  • Present data in JavaFX charts
  • Display Web-based content using WebView and WebEngine