Example usage for javafx.animation Timeline play

List of usage examples for javafx.animation Timeline play

Introduction

In this page you can find the example usage for javafx.animation Timeline play.

Prototype

public void play() 

Source Link

Document

Plays Animation from current position in the direction indicated by rate .

Usage

From source file:com.github.vatbub.tictactoe.view.Main.java

private void addWinLineOnLoose(Board.WinnerInfo winnerInfo) {
    final double strokeWidth = 2;

    Line originLine = new Line(0, 0, 0, 0);
    winLineGroup.getChildren().add(originLine);

    WinLine winLine = new WinLine(winnerInfo);
    winLine.startArc.setFill(Color.TRANSPARENT);
    winLine.startArc.setStroke(Color.BLACK);
    winLine.startArc.setStrokeWidth(strokeWidth);
    winLine.endArc.setFill(Color.TRANSPARENT);
    winLine.endArc.setStroke(Color.BLACK);
    winLine.endArc.setStrokeWidth(strokeWidth);
    winLine.rightLine.setStrokeWidth(strokeWidth);
    winLine.leftLine.setStrokeWidth(strokeWidth);

    winLine.centerLine.setStrokeWidth(0);
    winLineGroup.getChildren().addAll(winLine.getAll());

    winLineGroup.setOpacity(0);/*from ww  w. j  ava 2 s .c o m*/
    GaussianBlur blur = new GaussianBlur(7);
    winLineGroup.setEffect(blur);
    winLineGroup.setVisible(true);
    KeyValue keyValue1 = new KeyValue(winLineGroup.opacityProperty(), 0);
    KeyFrame keyFrame1 = new KeyFrame(Duration.millis(900), keyValue1);
    KeyValue keyValue2 = new KeyValue(winLineGroup.opacityProperty(), 1);
    KeyFrame keyFrame2 = new KeyFrame(Duration.millis(950), keyValue2);

    Timeline timeline = new Timeline();
    timeline.getKeyFrames().addAll(keyFrame1, keyFrame2);
    timeline.play();
}

From source file:com.github.vatbub.tictactoe.view.Main.java

public void flashOpponentsTurnHBox() {
    KeyValue keyValue1Color = new KeyValue(((DropShadow) opponentsTurnAnchorPane.getEffect()).colorProperty(),
            Color.RED);//ww  w  . j a  va 2  s  . c om
    KeyValue keyValue1Width = new KeyValue(((DropShadow) opponentsTurnAnchorPane.getEffect()).widthProperty(),
            30);
    KeyValue keyValue1Height = new KeyValue(((DropShadow) opponentsTurnAnchorPane.getEffect()).heightProperty(),
            30);
    KeyFrame keyFrame1 = new KeyFrame(Duration.seconds(animationSpeed / 2), keyValue1Color, keyValue1Width,
            keyValue1Height);

    KeyValue keyValue2Color = new KeyValue(((DropShadow) opponentsTurnAnchorPane.getEffect()).colorProperty(),
            Color.BLACK);
    KeyValue keyValue2Width = new KeyValue(((DropShadow) opponentsTurnAnchorPane.getEffect()).widthProperty(),
            12);
    KeyValue keyValue2Height = new KeyValue(((DropShadow) opponentsTurnAnchorPane.getEffect()).heightProperty(),
            12);
    KeyFrame keyFrame2 = new KeyFrame(Duration.seconds(animationSpeed), keyValue2Color, keyValue2Width,
            keyValue2Height);

    Timeline timeline = new Timeline(keyFrame1, keyFrame2);
    // play it twice
    timeline.setOnFinished((event -> new Timeline(keyFrame1, keyFrame2).play()));
    timeline.play();
}

From source file:com.github.vatbub.tictactoe.view.Main.java

private void addWinLineOnWin(Board.WinnerInfo winnerInfo, Paint color, Runnable onFinished) {
    Line originLine = new Line(0, 0, 0, 0);
    winLineGroup.getChildren().add(originLine);

    WinLine winLine = new WinLine(winnerInfo);
    double winLineEndX = winLine.endArc.getCenterX();
    double winLineEndY = winLine.endArc.getCenterY();
    winLine.startArc.setFill(color);//w  w w .  j a  va 2  s.  c  om
    winLine.startArc.setStrokeWidth(0);
    winLine.endArc.setFill(color);
    winLine.endArc.setStrokeWidth(0);
    winLine.rightLine.setStrokeWidth(0);
    winLine.leftLine.setStrokeWidth(0);
    winLine.centerLine.setStroke(color);

    winLine.centerLine.strokeWidthProperty().bind(winLine.startArc.radiusXProperty().multiply(2));
    winLineGroup.getChildren().addAll(winLine.getAll());

    blurNode(gamePane, 4);

    winLineGroup.setOpacity(0);
    GaussianBlur blur = new GaussianBlur(100);
    winLineGroup.setEffect(blur);
    winLineGroup.setBlendMode(BlendMode.DARKEN);
    winLineGroup.setVisible(true);

    double winLineAnimationX1 = 0.2;
    double winLineAnimationX2 = 0.5;

    KeyValue stretchKeyValue1x = new KeyValue(winLine.endArc.centerXProperty(), winLine.startArc.getCenterX());
    KeyValue stretchKeyValue1y = new KeyValue(winLine.endArc.centerYProperty(), winLine.startArc.getCenterY());
    KeyFrame stretchKeyFrame1 = new KeyFrame(Duration.millis(0), stretchKeyValue1x, stretchKeyValue1y);

    KeyValue stretchKeyValue2x = new KeyValue(winLine.endArc.centerXProperty(), winLine.startArc.getCenterX(),
            new CustomEaseBothInterpolator(winLineAnimationX1, winLineAnimationX2));
    KeyValue stretchKeyValue2y = new KeyValue(winLine.endArc.centerYProperty(), winLine.startArc.getCenterY(),
            new CustomEaseBothInterpolator(winLineAnimationX1, winLineAnimationX2));
    KeyFrame stretchKeyFrame2 = new KeyFrame(Duration.millis(100), stretchKeyValue2x, stretchKeyValue2y);

    KeyValue stretchKeyValue3x = new KeyValue(winLine.endArc.centerXProperty(), winLineEndX,
            new CustomEaseBothInterpolator(winLineAnimationX1, winLineAnimationX2));
    KeyValue stretchKeyValue3y = new KeyValue(winLine.endArc.centerYProperty(), winLineEndY,
            new CustomEaseBothInterpolator(winLineAnimationX1, winLineAnimationX2));
    KeyFrame stretchKeyFrame3 = new KeyFrame(Duration.millis(800), stretchKeyValue3x, stretchKeyValue3y);

    KeyValue opacityKeyValue1 = new KeyValue(winLineGroup.opacityProperty(), 0.8);
    KeyFrame opacityKeyFrame1 = new KeyFrame(Duration.millis(400), opacityKeyValue1);

    Timeline timeline = new Timeline();
    timeline.getKeyFrames().addAll(stretchKeyFrame1, stretchKeyFrame2, stretchKeyFrame3, opacityKeyFrame1);
    timeline.play();

    timeline.setOnFinished((event) -> onFinished.run());
}

From source file:com.github.vatbub.tictactoe.view.Main.java

private void setLowerRightAnchorPaneDimensions(Labeled nodeToShow, Labeled nodeToHide, boolean noAnimation,
        double widthOffset) {
    final double secondAnimationOffset = 0.1;
    if (widthOffset > 0) {
        nodeToShow.setPrefWidth(nodeToShow.getWidth() + widthOffset);
    }//from  ww  w  .j a  v  a 2  s.c  o m

    if (noAnimation) {
        nodeToShow.setOpacity(1);
        nodeToShow.setVisible(true);
        nodeToHide.setVisible(false);
        playOnlineAnchorPane.setPrefHeight(nodeToShow.getHeight());
        playOnlineAnchorPane.setPrefWidth(nodeToShow.getWidth());
    } else {
        nodeToShow.setOpacity(0);
        nodeToShow.setVisible(true);
        nodeToShow.setPrefWidth(nodeToShow.getWidth());
        nodeToShow.setPrefHeight(nodeToShow.getHeight());

        DoubleProperty firstProperty;
        DoubleProperty secondProperty;
        double firstPropertyValue;
        double secondPropertyValue;
        double secondPropertyInitialValue;

        if (nodeToShow.getHeight() > nodeToShow.getWidth()) {
            firstProperty = playOnlineAnchorPane.prefHeightProperty();
            firstPropertyValue = nodeToShow.getHeight() + AnchorPane.getBottomAnchor(nodeToShow);
            secondProperty = playOnlineAnchorPane.prefWidthProperty();
            secondPropertyValue = nodeToShow.getWidth() + AnchorPane.getRightAnchor(nodeToShow);
            secondPropertyInitialValue = nodeToHide.getWidth();
        } else {
            firstProperty = playOnlineAnchorPane.prefWidthProperty();
            firstPropertyValue = nodeToShow.getWidth() + AnchorPane.getRightAnchor(nodeToShow);
            secondProperty = playOnlineAnchorPane.prefHeightProperty();
            secondPropertyValue = nodeToShow.getHeight() + AnchorPane.getBottomAnchor(nodeToShow);
            secondPropertyInitialValue = nodeToHide.getHeight();
        }

        KeyValue keyValueFirstProperty = new KeyValue(firstProperty, firstPropertyValue,
                Interpolator.EASE_BOTH);
        KeyValue keyValueNodeToHideOpacity1 = new KeyValue(nodeToHide.opacityProperty(), 0,
                Interpolator.EASE_IN);
        KeyFrame keyFrame1 = new KeyFrame(Duration.seconds(animationSpeed), keyValueFirstProperty,
                keyValueNodeToHideOpacity1);

        KeyValue keyValueSecondProperty1 = new KeyValue(secondProperty, secondPropertyInitialValue);
        KeyValue keyValueNodeToShowOpacity1 = new KeyValue(nodeToShow.opacityProperty(),
                nodeToShow.getOpacity());
        KeyFrame keyFrame2 = new KeyFrame(Duration.seconds(secondAnimationOffset * animationSpeed),
                keyValueSecondProperty1, keyValueNodeToShowOpacity1);

        KeyValue keyValueSecondProperty2 = new KeyValue(secondProperty, secondPropertyValue,
                Interpolator.EASE_BOTH);
        KeyValue keyValueNodeToShowOpacity2 = new KeyValue(nodeToShow.opacityProperty(), 1,
                Interpolator.EASE_OUT);
        KeyFrame keyFrame3 = new KeyFrame(Duration.seconds((1 + secondAnimationOffset) * animationSpeed),
                keyValueSecondProperty2, keyValueNodeToShowOpacity2);

        Timeline timeline = new Timeline(keyFrame1, keyFrame2, keyFrame3);
        timeline.setOnFinished((event) -> nodeToHide.setVisible(false));
        timeline.play();
    }
}

From source file:com.github.vatbub.tictactoe.view.Main.java

public void updateOpponentsTurnHBox(boolean noAnimation, boolean isShown) {
    double destinationTranslate;
    double animationOffsetInSeconds = 0;
    if (!isShown) {
        destinationTranslate = opponentsTurnHBox.getHeight() + 3;
        double timeSinceLastShownInMillis = Calendar.getInstance().getTime().getTime()
                - opponentsTurnLabelLastShown.getTime().getTime();
        double timeSinceLastShownInSeconds = timeSinceLastShownInMillis / 1000;
        animationOffsetInSeconds = Math
                .max(opponentsTurnLabelShownForAtLeastSeconds - timeSinceLastShownInSeconds, 0);
    } else {//from w w  w  .j  a  v a  2 s.  co  m
        destinationTranslate = 0;
        String opponentsName;
        if (board.getCurrentPlayer() == null || board.getOpponent(board.getCurrentPlayer()) == null) {
            opponentsName = "Opponent";
        } else {
            Player player;
            if (board.getCurrentPlayer().getPlayerMode() == PlayerMode.localHuman) {
                player = board.getOpponent(board.getCurrentPlayer());
            } else {
                player = board.getCurrentPlayer();
            }
            opponentsName = player.getName();
        }
        opponentsTurnLabel.setText(opponentsName + "'s turn...");
    }

    if (noAnimation) {
        opponentsTurnHBox.setTranslateY(destinationTranslate);
    } else {
        KeyValue keyValue1 = new KeyValue(opponentsTurnHBox.translateYProperty(),
                opponentsTurnHBox.getTranslateY());
        KeyFrame keyFrame1 = new KeyFrame(Duration.seconds(animationOffsetInSeconds), keyValue1);

        KeyValue keyValue2 = new KeyValue(opponentsTurnHBox.translateYProperty(), destinationTranslate);
        KeyFrame keyFrame2 = new KeyFrame(Duration.seconds(animationSpeed + animationOffsetInSeconds),
                keyValue2);
        Timeline timeline = new Timeline(keyFrame1, keyFrame2);
        timeline.setOnFinished((event) -> {
            if (isShown) {
                opponentsTurnLabelLastShown = Calendar.getInstance();
            }
        });
        timeline.play();
    }
}

From source file:com.github.vatbub.tictactoe.view.Main.java

private void blurNode(Node node, double toValue, @SuppressWarnings("SameParameterValue") Runnable onFinish) {
    guiAnimationQueue.submit(() -> {/* w  ww .  j a  v  a 2 s .c om*/
        GaussianBlur blur = (GaussianBlur) node.getEffect();
        if (blur == null) {
            blur = new GaussianBlur(0);
            node.setEffect(blur);
        }
        node.setEffect(blur);
        Timeline timeline = new Timeline();
        KeyValue keyValue = new KeyValue(blur.radiusProperty(), toValue);
        KeyFrame keyFrame = new KeyFrame(Duration.seconds(animationSpeed), keyValue);
        timeline.getKeyFrames().add(keyFrame);

        timeline.setOnFinished((event) -> {
            if (toValue == 0) {
                node.setEffect(null);
            }
            if (onFinish != null) {
                onFinish.run();
            }
        });

        timeline.play();
    });
}

From source file:com.github.vatbub.tictactoe.view.Main.java

private void showTie() {
    guiAnimationQueue.submitWaitForUnlock(() -> {
        double endX = tiePane.getWidth() - 230;
        double endY = 90;

        AnchorPane.clearConstraints(bowTie);
        bowTie.setX(endX);/*w  ww .  j a v a 2  s . com*/
        bowTie.setY(-150);

        blurGamePane();
        tieMessage.setOpacity(0);
        tiePane.setOpacity(1);
        tiePane.setVisible(true);
        bowTie.setVisible(true);

        Timeline timeline = new Timeline();
        double S4 = 1.45;
        double x0 = 0.33;
        KeyValue keyValue1x = new KeyValue(bowTie.xProperty(), endX, new CustomEaseOutInterpolator(S4, x0));
        KeyValue keyValue1y = new KeyValue(bowTie.yProperty(), endY, new CustomEaseOutInterpolator(S4, x0));
        KeyFrame keyFrame1 = new KeyFrame(Duration.seconds(1), keyValue1x, keyValue1y);
        timeline.getKeyFrames().add(keyFrame1);

        timeline.setOnFinished((event) -> fadeNode(tieMessage, 1, () -> {
            AnchorPane.setRightAnchor(bowTie, tiePane.getWidth() - bowTie.getFitWidth() - endX);
            AnchorPane.setTopAnchor(bowTie, endY);
        }));

        timeline.play();
    });
}

From source file:com.github.vatbub.tictactoe.view.Main.java

public void updateCurrentPlayerLabel(boolean noAnimation, boolean setBlockedValueAfterAnimation) {
    Platform.runLater(() -> stage.setTitle(getWindowTitle()));
    if (board.getCurrentPlayer() != null) {
        if (!board.getCurrentPlayer().getLetter().equals(currentPlayerLabel.getText())) {
            if (noAnimation) {
                setCurrentPlayerValue();
            } else {
                guiAnimationQueue.submitWaitForUnlock(() -> {
                    guiAnimationQueue.setBlocked(true);

                    GaussianBlur blur = (GaussianBlur) currentPlayerLabel.getEffect();
                    if (blur == null) {
                        blur = new GaussianBlur(0);
                    }//from ww w  .  j av a 2  s . c o  m

                    Calendar changeLabelTextDate = Calendar.getInstance();
                    changeLabelTextDate.add(Calendar.MILLISECOND, (int) (animationSpeed * 1000));
                    runLaterTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            Platform.runLater(() -> setCurrentPlayerValue());
                        }
                    }, changeLabelTextDate.getTime());

                    currentPlayerLabel.setEffect(blur);
                    Timeline timeline = new Timeline();
                    KeyValue keyValue1 = new KeyValue(blur.radiusProperty(), 20);
                    KeyFrame keyFrame1 = new KeyFrame(Duration.seconds(animationSpeed), keyValue1);

                    KeyValue keyValue2 = new KeyValue(blur.radiusProperty(), 0);
                    KeyFrame keyFrame2 = new KeyFrame(Duration.seconds(2 * animationSpeed), keyValue2);

                    timeline.getKeyFrames().addAll(keyFrame1, keyFrame2);

                    timeline.setOnFinished((event) -> {
                        currentPlayerLabel.setEffect(null);
                        guiAnimationQueue.setBlocked(false);
                        setBlockedForInput(setBlockedValueAfterAnimation);
                    });

                    timeline.play();
                });
                return;
            }
        }
    }

    guiAnimationQueue.setBlocked(false);
    setBlockedForInput(setBlockedValueAfterAnimation);
}

From source file:com.github.vatbub.tictactoe.view.Main.java

private void showWinnerWithTwoHumanPlayers(Board.WinnerInfo winnerInfo) {
    guiAnimationQueue.submitWaitForUnlock(() -> addWinLineOnWin(winnerInfo, Color.YELLOW, () -> {
        twoHumansWinnerText.setText(winnerInfo.winningPlayer.getName() + " won :)");
        double endX = twoHumansWinnerImage.getX();
        double endY = twoHumansWinnerImage.getY();

        AnchorPane.clearConstraints(twoHumansWinnerImage);
        twoHumansWinnerImage.setX(endX);
        twoHumansWinnerImage.setY(twoHumansWinnerImage.getY() + 300);

        // blurGamePane();
        blurGamePane(10.0);/*from www  .  ja v a 2  s.c  om*/
        twoHumansWinMessage.setOpacity(0);
        twoHumansWinnerImage.setOpacity(0);
        twoHumansWinnerPane.setOpacity(1);
        twoHumansWinnerPane.setVisible(true);
        twoHumansWinnerImage.setVisible(true);

        Timeline timeline = new Timeline();
        double S4 = 1.45;
        double x0 = 0.33;

        KeyValue twoHumansWinnerImageKeyValue1x = new KeyValue(twoHumansWinnerImage.xProperty(), endX,
                new CustomEaseOutInterpolator(S4, x0));
        KeyValue twoHumansWinnerImageKeyValue1y = new KeyValue(twoHumansWinnerImage.yProperty(), endY,
                new CustomEaseOutInterpolator(S4, x0));
        KeyValue twoHumansWinnerImageKeyValue1Opacity = new KeyValue(twoHumansWinnerImage.opacityProperty(), 1);
        KeyFrame twoHumansWinnerImageKeyFrame1 = new KeyFrame(Duration.millis(600),
                twoHumansWinnerImageKeyValue1x, twoHumansWinnerImageKeyValue1y,
                twoHumansWinnerImageKeyValue1Opacity);
        timeline.getKeyFrames().addAll(twoHumansWinnerImageKeyFrame1);

        timeline.setOnFinished((event) -> fadeNode(twoHumansWinMessage, 1, () -> {
            AnchorPane.setLeftAnchor(twoHumansWinnerImage, 0.0);
            AnchorPane.setBottomAnchor(twoHumansWinnerImage, 0.0);
        }));

        timeline.play();
    }));
}

From source file:com.github.vatbub.tictactoe.view.Main.java

private void showLooser(Board.WinnerInfo winnerInfo) {
    String looserName = board.getOpponent(winnerInfo.winningPlayer).getName();
    guiAnimationQueue.submitWaitForUnlock(() -> {
        ShakeTransition anim = new ShakeTransition(gamePane, null);
        anim.playFromStart();/*  ww w. j av  a 2s  .co  m*/

        Timeline timeline = new Timeline();

        Circle c1 = new Circle((452 / 600.0) * looserPane.getWidth(), (323 / 640.0) * looserPane.getHeight(),
                0);
        GaussianBlur circleBlur = new GaussianBlur(30);
        c1.setEffect(circleBlur);
        looseImage.setClip(c1);
        addWinLineOnLoose(winnerInfo);

        KeyValue keyValue1 = new KeyValue(c1.radiusProperty(), 0);
        KeyFrame keyFrame1 = new KeyFrame(Duration.millis(800), keyValue1);
        KeyValue keyValue2 = new KeyValue(c1.radiusProperty(), (500 / 640.0) * looserPane.getHeight());
        KeyFrame keyFrame2 = new KeyFrame(Duration.millis(900), keyValue2);

        timeline.getKeyFrames().addAll(keyFrame1, keyFrame2);

        looseMessage.setOpacity(0);
        looserText.setText(looserName + " lost :(");
        looserPane.setVisible(true);
        looserPane.setOpacity(1);

        timeline.setOnFinished((event) -> {
            looseImage.setClip(null);
            winLineGroup.setClip(null);
            blurGamePane();
            PauseTransition wait = new PauseTransition();
            wait.setDuration(Duration.seconds(1));
            wait.setOnFinished((event2) -> {
                FadeTransition looseMessageTransition = new FadeTransition();
                looseMessageTransition.setNode(looseMessage);
                looseMessageTransition.setFromValue(0);
                looseMessageTransition.setToValue(1);
                looseMessageTransition.setDuration(Duration.millis(500));
                looseMessageTransition.setAutoReverse(false);
                looseMessageTransition.play();
            });

            wait.play();
        });

        timeline.play();
    });

}