Wednesday, May 28, 2014

JavaFX 8 PauseTransition example

Example of using PauseTransition to insert a pause between transitions in SequentialTransition.


package javafx8_animation;

import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.animation.PauseTransition;
import javafx.animation.RotateTransition;
import javafx.animation.SequentialTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 350, Color.BLACK);
        scene.setFill(Color.WHITE);

        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect = new Rectangle(150, 150, 50, 50);
        rect.setFill(linearGradientTransparence);
        rect.setStroke(Color.BLUE);
        rect.setStrokeWidth(3);
        
        MoveTo moveTo = new MoveTo();
        moveTo.setX(175f);
        moveTo.setY(175f);
        
        ArcTo arcTo = new ArcTo();
        arcTo.setX(375f);
        arcTo.setY(175f);
        arcTo.setRadiusX(100f);
        arcTo.setRadiusY(50f);
        
        Path path = new Path();
        path.getElements().add (moveTo);
        path.getElements().add (arcTo);

        PathTransition pathTransition = new PathTransition();
        pathTransition.setDuration(Duration.millis(2000));
        pathTransition.setPath(path);
        pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
        pathTransition.setCycleCount(2);
        pathTransition.setAutoReverse(true);
        
        //pause for 2 second
        PauseTransition pauseTransition = 
            new PauseTransition(Duration.millis(2000)); 
 
        RotateTransition rotateTransition = 
            new RotateTransition(Duration.millis(2000));
        rotateTransition.setByAngle(360);
        rotateTransition.setCycleCount(2);
        rotateTransition.setAutoReverse(true);
        
        SequentialTransition sequentialTransition2 = 
            new SequentialTransition (
                    rect, 
                    pathTransition, 
                    pauseTransition,
                    rotateTransition);

        rect.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            sequentialTransition2.play();
        });
        
        root.getChildren().add(rect);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

Monday, May 26, 2014

JavaFX 8 ParallelTransition and SequentialTransition

ParallelTransition and SequentialTransition plays a list of Animations in parallel and in sequential order.


package javafx8_animation;

import javafx.animation.FadeTransition;
import javafx.animation.FillTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.animation.RotateTransition;
import javafx.animation.ScaleTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.StrokeTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 350, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        Rectangle rect1 = new Rectangle(50, 50, 50, 50);
        rect1.setFill(Color.BLUEVIOLET);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(3);
        
        Path path1 = new Path();
        path1.getElements().add (new MoveTo (75f, 75f));
        path1.getElements().add (new LineTo (75f, 175f));
        path1.getElements().add (new LineTo (175f, 175f));
        path1.getElements().add (new LineTo (175f, 75f));
        path1.getElements().add(new ClosePath());
        
        PathTransition pathTransition1 = new PathTransition();
        pathTransition1.setDuration(Duration.millis(4000));
        pathTransition1.setPath(path1);
        pathTransition1.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
        pathTransition1.setCycleCount(2);
        pathTransition1.setAutoReverse(true);
        
        StrokeTransition strokeTransition1 = 
            new StrokeTransition(
                        Duration.millis(1000), 
                        Color.RED, 
                        Color.BLUEVIOLET);
        strokeTransition1.setCycleCount(8);
        strokeTransition1.setAutoReverse(true);
        
        ScaleTransition scaleTransition1 = 
            new ScaleTransition(Duration.millis(2000));
        scaleTransition1.setByX(1.0f);
        scaleTransition1.setByY(1.0f);
        scaleTransition1.setCycleCount(6);
        scaleTransition1.setAutoReverse(true);
        
        ParallelTransition parallelTransition1 = 
            new ParallelTransition (
                    rect1, 
                    pathTransition1, 
                    strokeTransition1, 
                    scaleTransition1);

        rect1.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            parallelTransition1.play();
        });

        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(150, 150, 50, 50);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(3);
        
        MoveTo moveTo = new MoveTo();
        moveTo.setX(175f);
        moveTo.setY(175f);
        
        ArcTo arcTo = new ArcTo();
        arcTo.setX(375f);
        arcTo.setY(175f);
        arcTo.setRadiusX(100f);
        arcTo.setRadiusY(50f);
        
        Path path2 = new Path();
        path2.getElements().add (moveTo);
        path2.getElements().add (arcTo);

        PathTransition pathTransition2 = new PathTransition();
        pathTransition2.setDuration(Duration.millis(2000));
        pathTransition2.setNode(rect2);
        pathTransition2.setPath(path2);
        pathTransition2.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
        pathTransition2.setCycleCount(2);
        pathTransition2.setAutoReverse(true);
        
        FillTransition fillTransition2 = 
            new FillTransition(Duration.millis(2000), 
                    Color.BLUEVIOLET, 
                    Color.YELLOW);
        fillTransition2.setCycleCount(2);
        fillTransition2.setAutoReverse(true);
        
        FadeTransition fadeTransition2 
            = new FadeTransition(Duration.millis(2000));
        fadeTransition2.setFromValue(1.0);
        fadeTransition2.setToValue(0.0);
        fadeTransition2.setCycleCount(2);
        fadeTransition2.setAutoReverse(true);
        
        RotateTransition rotateTransition2 = 
            new RotateTransition(Duration.millis(2000));
        rotateTransition2.setByAngle(360);
        rotateTransition2.setCycleCount(2);
        rotateTransition2.setAutoReverse(true);
        
        SequentialTransition sequentialTransition2 = 
            new SequentialTransition (
                    rect2, 
                    pathTransition2, 
                    fillTransition2, 
                    fadeTransition2,
                    rotateTransition2);

        rect2.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            sequentialTransition2.play();
        });
        
        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

Friday, May 23, 2014

JavaFX 8 PathTransition


package javafx8_animation;

import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 350, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        Rectangle rect1 = new Rectangle(50, 50, 50, 50);
        rect1.setFill(Color.BLUEVIOLET);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(3);
        
        Path path1 = new Path();
        path1.getElements().add (new MoveTo (75f, 75f));
        path1.getElements().add (new LineTo (75f, 175f));
        path1.getElements().add (new LineTo (175f, 175f));
        path1.getElements().add (new LineTo (175f, 75f));
        path1.getElements().add(new ClosePath());
        
        PathTransition pathTransition1 = new PathTransition();
        pathTransition1.setDuration(Duration.millis(4000));
        pathTransition1.setNode(rect1);
        pathTransition1.setPath(path1);
        pathTransition1.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
        pathTransition1.setCycleCount(2);
        pathTransition1.setAutoReverse(true);

        rect1.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            pathTransition1.play();
        });

        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(150, 150, 50, 50);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(3);
        
        MoveTo moveTo = new MoveTo();
        moveTo.setX(175f);
        moveTo.setY(175f);
        
        ArcTo arcTo = new ArcTo();
        arcTo.setX(375f);
        arcTo.setY(175f);
        arcTo.setRadiusX(100f);
        arcTo.setRadiusY(50f);
        
        Path path2 = new Path();
        path2.getElements().add (moveTo);
        path2.getElements().add (arcTo);

        PathTransition pathTransition2 = new PathTransition();
        pathTransition2.setDuration(Duration.millis(4000));
        pathTransition2.setNode(rect2);
        pathTransition2.setPath(path2);
        pathTransition2.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
        pathTransition2.setCycleCount(2);
        pathTransition2.setAutoReverse(true);

        rect2.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            pathTransition2.play();
        });
        
        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

Thursday, May 22, 2014

JavaFX 8 StrokeTransition


package javafx8_animation;

import javafx.animation.StrokeTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 350, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        Rectangle rect1 = new Rectangle(60, 30, 350, 200);
        rect1.setFill(Color.BLUEVIOLET);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);
        
        StrokeTransition strokeTransition1 = 
                new StrokeTransition(
                        Duration.millis(2000), 
                        rect1, 
                        Color.RED, 
                        Color.BLUEVIOLET);
        strokeTransition1.setCycleCount(2);
        strokeTransition1.setAutoReverse(true);
        rect1.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            strokeTransition1.play();
        });

        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(100, 100, 250, 200);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);
        
        StrokeTransition strokeTransition2 = 
                new StrokeTransition(
                        Duration.millis(2000), 
                        rect2, 
                        Color.BLUE, 
                        Color.CORAL);
        strokeTransition2.setCycleCount(2);
        strokeTransition2.setAutoReverse(true);
        rect2.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            strokeTransition2.play();
        });
        
        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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


Wednesday, May 21, 2014

JavaFX 8 TranslateTransition

Example of JavaFX animation of TranslateTransition:


package javafx8_animation;

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 350, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        Rectangle rect1 = new Rectangle(60, 30, 350, 200);
        rect1.setFill(Color.BLUEVIOLET);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);
        
        TranslateTransition translateTransition1 = new TranslateTransition(Duration.millis(500), rect1);
        translateTransition1.setByX(40);
        translateTransition1.setByY(70);
        translateTransition1.setCycleCount(2);
        translateTransition1.setAutoReverse(true);
        rect1.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            translateTransition1.play();
        });

        //Rectangle with LinearGradient Transparence background
        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(100, 100, 250, 200);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);
        
        TranslateTransition translateTransition2 = new TranslateTransition(Duration.millis(500), rect2);
        translateTransition2.setByX(-40);
        translateTransition2.setByY(-70);
        translateTransition2.setCycleCount(2);
        translateTransition2.setAutoReverse(true);
        rect2.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            translateTransition2.play();
        });
        
        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

JavaFX 8 ScaleTransition

ScaleTransition example:


package javafx8_animation;

import javafx.animation.FillTransition;
import javafx.animation.ScaleTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 350, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        Rectangle rect1 = new Rectangle(60, 30, 350, 200);
        rect1.setFill(Color.BLUEVIOLET);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);
        
        ScaleTransition scaleTransition1 = new ScaleTransition(Duration.millis(500), rect1);
        scaleTransition1.setByX(0.1f);
        scaleTransition1.setByY(0.1f);
        scaleTransition1.setCycleCount(4);
        scaleTransition1.setAutoReverse(true);
        
        rect1.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            scaleTransition1.play();
        });

        //Rectangle with LinearGradient Transparence background
        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(100, 100, 250, 200);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);
        
        ScaleTransition scaleTransition2 = new ScaleTransition(Duration.millis(500), rect2);
        scaleTransition2.setByX(-0.1f);
        scaleTransition2.setByY(-0.1f);
        scaleTransition2.setCycleCount(4);
        scaleTransition2.setAutoReverse(true);
        
        rect2.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            scaleTransition2.play();
        });

        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

Tuesday, May 20, 2014

JavaFX 8 FillTransition



package javafx8_animation;

import javafx.animation.FillTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 350, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        Rectangle rect1 = new Rectangle(60, 30, 350, 200);
        rect1.setFill(Color.BLUEVIOLET);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);

        FillTransition fillTransition = 
            new FillTransition(Duration.millis(5000), 
                    rect1, 
                    Color.BLUEVIOLET, 
                    Color.YELLOW);
        fillTransition.setCycleCount(2);
        fillTransition.setAutoReverse(true);
        
        rect1.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            fillTransition.play();
        });

        //Rectangle with LinearGradient Transparence background
        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(100, 100, 250, 200);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);

        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

JavaFX 8 FadeTransition

JavaFX 8 example to implement RotateTransition:



package javafx8_animation;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        //Rectangle with LinearGradient background
        Stop[] stops = new Stop[] { 
            new Stop(0, Color.BLACK), 
            new Stop(1, Color.RED)};
        LinearGradient linearGradient = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stops);
        
        Rectangle rect1 = new Rectangle(60, 60, 350, 300);
        rect1.setFill(linearGradient);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);

        FadeTransition fadeTransition1 = new FadeTransition(Duration.millis(2000), rect1);
        fadeTransition1.setFromValue(1.0);
        fadeTransition1.setToValue(0.3);
        
        rect1.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            fadeTransition1.play();
        });

        //Rectangle with LinearGradient Transparence background
        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(100, 200, 250, 250);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);
        
        FadeTransition fadeTransition2 = new FadeTransition(Duration.millis(2000), rect2);
        fadeTransition2.setFromValue(1.0);
        fadeTransition2.setToValue(0.0);
        fadeTransition2.setCycleCount(2);
        fadeTransition2.setAutoReverse(true);
        
        rect2.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            fadeTransition2.play();
        });

        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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


Monday, May 19, 2014

JavaFX 8 RotateTransition

Example of JavaFX 8 RotateTransition:


package javafx8_animation;

import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Animation extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        //Rectangle with LinearGradient background
        Stop[] stops = new Stop[] { 
            new Stop(0, Color.BLACK), 
            new Stop(1, Color.RED)};
        LinearGradient linearGradient = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stops);
        
        Rectangle rect1 = new Rectangle(60, 60, 350, 300);
        rect1.setFill(linearGradient);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);
        
        RotateTransition rotateTransition1 = 
            new RotateTransition(Duration.millis(2000), rect1);
        rotateTransition1.setByAngle(360);
        
        rect1.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            rotateTransition1.play();
        });

        //Rectangle with LinearGradient Transparence background
        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(100, 200, 250, 250);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);
        
        RotateTransition rotateTransition2 = 
            new RotateTransition(Duration.millis(2000), rect2);
        rotateTransition2.setByAngle(360);
        rotateTransition2.setCycleCount(2);
        rotateTransition2.setAutoReverse(true);
        
        rect2.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent event) -> {
            rotateTransition2.play();
        });

        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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


Thursday, May 15, 2014

LinearGradient Transparence background

Example to fill Rectangle with LinearGradient Transparence background.



package javafx8_shape;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Shape extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);
        scene.setFill(Color.WHITE);
        
        //Rectangle with LinearGradient background
        Stop[] stops = new Stop[] { 
            new Stop(0, Color.BLACK), 
            new Stop(1, Color.RED)};
        LinearGradient linearGradient = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stops);
        
        Rectangle rect1 = new Rectangle(60, 60, 350, 300);
        rect1.setFill(linearGradient);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);
        
        //Rectangle with LinearGradient Transparence background
        Stop[] stopsTransparence = new Stop[] { 
            new Stop(0, Color.color(0.0, 1.0, 1.0 ,1.0)), 
            new Stop(1, Color.color(0.0, 1.0, 1.0 ,0.0))};
        LinearGradient linearGradientTransparence = 
                new LinearGradient(0, 0, 1, 0, true, 
                        CycleMethod.NO_CYCLE, stopsTransparence);
        
        Rectangle rect2 = new Rectangle(100, 200, 250, 250);
        rect2.setFill(linearGradientTransparence);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);
        
        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

JavaFX example: Rectangle filled with LinearGradient


package javafx8_shape;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Shape extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);
        
        Stop[] stops = new Stop[] { 
            new Stop(0, Color.BLACK), 
            new Stop(1, Color.RED)};
        LinearGradient linearGradient = 
                new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);

        //LinearGradient rectangle with Stroke
        Rectangle rect1 = new Rectangle(60, 60, 200, 200);
        rect1.setFill(linearGradient);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);
        
        Rectangle rect2 = new Rectangle(160, 160, 300, 300);
        rect2.setFill(linearGradient);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);
        
        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

Wednesday, May 14, 2014

JavaFX Scene Builder 2.0

JavaFX Scene Builder 2.0 - Building an app UI


Download JavaFX Scene Builder 2.0

JavaFX 8 Rectangle example

JavaFX 8 example to draw Rectangles with filled color, transparent with stroke, and no fill color specified, with stroke.

package javafx8_shape;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Shape extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);

        //Filled rectangle
        Rectangle rect1 = new Rectangle(10, 10, 200, 200);
        rect1.setFill(Color.BLUE);
        
        //Transparent rectangle with Stroke
        Rectangle rect2 = new Rectangle(60, 60, 200, 200);
        rect2.setFill(Color.TRANSPARENT);
        rect2.setStroke(Color.RED);
        rect2.setStrokeWidth(10);
        
        //Rectangle with Stroke, no Fill color specified
        Rectangle rect3 = new Rectangle(110, 110, 200, 200);
        rect3.setStroke(Color.GREEN);
        rect3.setStrokeWidth(10);
        
        root.getChildren().addAll(rect1, rect2, rect3);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}






You can still use RectangleBuilder, but it is deprecated and will be removed in the next version.


package javafx8_shape;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.stage.Stage;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Shape extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);

        //Filled rectangle
        Rectangle rect1 = new Rectangle(10, 10, 200, 200);
        rect1.setFill(Color.BLUE);
        
        //Transparent rectangle with Stroke
        Rectangle rect2 = new Rectangle(60, 60, 200, 200);
        rect2.setFill(Color.TRANSPARENT);
        rect2.setStroke(Color.RED);
        rect2.setStrokeWidth(10);
        
        //Rectangle with Stroke, no Fill color specified
        Rectangle rect3 = new Rectangle(110, 110, 200, 200);
        rect3.setStroke(Color.GREEN);
        rect3.setStrokeWidth(10);
        
        //Create Rectangle using deprecated RectangleBuilder.
        Rectangle rect4 = RectangleBuilder.create()
              .fill(Color.WHITESMOKE)
              .x(160)
              .y(160)
              .width(200)
              .height(200)
              .strokeWidth(10)
              .stroke(Color.BLUEVIOLET)
              .build();
        
        root.getChildren().addAll(rect1, rect2, rect3, rect4);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}



Saturday, May 10, 2014

print ZonedDateTime with format



package javazoneddatetime;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @web java-buddy.blogspot.com
 */
public class JavaZonedDateTime {

    public static void main(String[] args) {
        ZonedDateTime zdt = 
            ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");
        
        System.out.println("BASIC_ISO_DATE: \t" + zdt.format(DateTimeFormatter.BASIC_ISO_DATE));
        System.out.println("ISO_LOCAL_DATE: \t" + zdt.format(DateTimeFormatter.ISO_LOCAL_DATE));
        System.out.println("ISO_OFFSET_DATE: \t" + zdt.format(DateTimeFormatter.ISO_OFFSET_DATE));
        System.out.println("ISO_DATE: \t\t" + zdt.format(DateTimeFormatter.ISO_DATE));
        System.out.println("ISO_LOCAL_TIME: \t" + zdt.format(DateTimeFormatter.ISO_LOCAL_TIME));
        System.out.println("ISO_OFFSET_TIME: \t" + zdt.format(DateTimeFormatter.ISO_OFFSET_TIME));
        System.out.println("ISO_TIME: \t\t" + zdt.format(DateTimeFormatter.ISO_TIME));
        System.out.println("ISO_LOCAL_DATE_TIME: \t" + zdt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        System.out.println("ISO_OFFSET_DATE_TIME: \t" + zdt.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
        System.out.println("ISO_ZONED_DATE_TIME: \t" + zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
        System.out.println("ISO_DATE_TIME: \t\t" + zdt.format(DateTimeFormatter.ISO_DATE_TIME));
        System.out.println("ISO_ORDINAL_DATE: \t" + zdt.format(DateTimeFormatter.ISO_ORDINAL_DATE));
        System.out.println("ISO_WEEK_DATE: \t\t" + zdt.format(DateTimeFormatter.ISO_WEEK_DATE));
        System.out.println("ISO_INSTANT: \t\t" + zdt.format(DateTimeFormatter.ISO_INSTANT));
        System.out.println("RFC_1123_DATE_TIME: \t" + zdt.format(DateTimeFormatter.RFC_1123_DATE_TIME));
    }
    
}

Friday, May 9, 2014

Conversion between ZonedDateTime and GregorianCalendar

package javazoneddatetime;

import java.time.ZonedDateTime;
import java.util.GregorianCalendar;

/**
 * @web java-buddy.blogspot.com
 */
public class JavaZonedDateTime {

    public static void main(String[] args) {
        ZonedDateTime zonedDateTime1 = 
            ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");
        GregorianCalendar gregorianCalendar = GregorianCalendar.from(zonedDateTime1);
        ZonedDateTime zonedDateTime2 = gregorianCalendar.toZonedDateTime();
        
        System.out.println("zonedDateTime1: " + zonedDateTime1.toString());
        System.out.println("gregorianCalendar: " + gregorianCalendar);
        System.out.println("zonedDateTime2: " + zonedDateTime2.toString());
    }
    
}


Conversion between ZonedDateTime and GregorianCalendar

Wednesday, May 7, 2014

ZonedDateTime example

Example of using ZonedDateTime:

ZonedDateTime
ZonedDateTime example

package javazoneddatetime;

import java.time.ZonedDateTime;

/**
 * @web java-buddy.blogspot.com
 */
public class JavaZonedDateTime {

    public static void main(String[] args) {
        ZonedDateTime zonedDateTime = 
            ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");
        System.out.println("zonedDateTime: " + zonedDateTime.toString());
        System.out.println("Zone: " + zonedDateTime.getZone());
        System.out.println("Offset: " + zonedDateTime.getOffset());
        System.out.println("Year: " + zonedDateTime.getYear());
        System.out.println("Month: " + zonedDateTime.getMonth());
        System.out.println("DayOfMonth: " + zonedDateTime.getDayOfMonth());
        System.out.println("Hour: " + zonedDateTime.getHour());
        System.out.println("Minute: " + zonedDateTime.getMinute());
        System.out.println("Second: " + zonedDateTime.getSecond());
        System.out.println("MonthValue: " + zonedDateTime.getMonthValue());
        System.out.println("DayOfYear: " + zonedDateTime.getDayOfYear());
        System.out.println("DayOfWeek: " + zonedDateTime.getDayOfWeek());
        System.out.println("Nano: " + zonedDateTime.getNano());
        
    }
    
}


Tuesday, May 6, 2014

Java 8 Launch videos published

Videos of Java 8 Launch uploaded to Youtube.

List available ScriptEngine

The following program list ScriptEngine(s) available in your system.
package javalistscript;

import java.util.List;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaListScript {

    public static void main(String[] args) {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        List<ScriptEngineFactory> listScriptEngineFactory =
                scriptEngineManager.getEngineFactories();
        for(ScriptEngineFactory factory : listScriptEngineFactory){
            System.out.println("ScriptEngine: " + factory.getScriptEngine());
            System.out.println("EngineName: " + factory.getEngineName());
            System.out.println("EngineVersion: " + factory.getEngineVersion());
            System.out.println("LanguageName: " + factory.getLanguageName());
            System.out.println("LanguageVersion: " + factory.getLanguageVersion());
            
            List<String> listExtensions = factory.getExtensions();
            for(String ext : listExtensions){
                System.out.println("Extension: " + ext);
            }
            
            List<String> listMimeTypes = factory.getMimeTypes();
            for(String mimeType : listMimeTypes){
                System.out.println("MimeType: " + mimeType);
            }
        }
    }
    
}



Monday, May 5, 2014

switch-case on String

Example of using String on switch-case statements:

        String toBeChecked = "Einstein";
        switch(toBeChecked){
            case "Einstein":
                //...
                break;
            case "Newton":
                //...
                break;
            case "Edison":
                //...
                break;
            case "Darwin":
                //...
                break;
            case "Watt":
                //...
                break;
            default:
                //...
        }


Sunday, May 4, 2014

Store mixed type of number in List<Number>

Example to store mixed type of number in List<Number>:


package javanumberlist;

import java.util.ArrayList;
import java.util.List;

/**
 * @web java-buddy.blogspot.com
 */
public class JavaNumberList {

    public static void main(String[] args) {
        List<Number> numberList = new ArrayList<>();
        numberList.add(123.456);
        numberList.add(123.456f);
        numberList.add((short)250);
        numberList.add((long)123);
        numberList.add((int)1);
        numberList.add((Integer)2);
        
        numberList.stream().forEach((number) -> {
            System.out.println(number 
                + " : " + number.getClass().toString());
        });

    }
    
}

Saturday, May 3, 2014

Java Performance: The Definitive Guide

Coding and testing are often considered separate areas of expertise. In this comprehensive guide, author and Java expert Scott Oaks takes the approach that anyone who works with Java should be equally adept at understanding how code behaves in the JVM, as well as the tunings likely to help its performance.

You’ll gain in-depth knowledge of Java application performance, using the Java Virtual Machine (JVM) and the Java platform, including the language and API. Developers and performance engineers alike will learn a variety of features, tools, and processes for improving the way Java 7 and 8 applications perform.

  • Apply four principles for obtaining the best results from performance testing
  • Use JDK tools to collect data on how a Java application is performing
  • Understand the advantages and disadvantages of using a JIT compiler
  • Tune JVM garbage collectors to affect programs as little as possible
  • Use techniques to manage heap memory and JVM native memory
  • Maximize Java threading and synchronization performance features
  • Tackle performance issues in Java EE and Java SE APIs
  • Improve Java-driven database application performance