在javafx画布中淡出和淡出一个圆圈

时间:2015-03-12 15:43:07

标签: animation canvas javafx javafx-8 fade

我想在javafx画布中淡出和淡出一个圆圈。 我可以将一个圆圈从屏幕的一个部分移动到另一个部分,但我似乎无法让这个对象淡入淡出。

以下是我用于将圆圈从屏幕的一部分移动到另一部分的代码

public class AnimatedCircleOnCanvas extends Application {
    public static final double W = 200; // canvas dimensions.
    public static final double H = 200;

    public static final double D = 20;  // diameter.

    @Override public void start(Stage stage) {
        DoubleProperty x  = new SimpleDoubleProperty();
        DoubleProperty y  = new SimpleDoubleProperty();

        Timeline timeline = new Timeline(
            new KeyFrame(Duration.seconds(0),
                    new KeyValue(x, 0),
                    new KeyValue(y, 0)
            ),
            new KeyFrame(Duration.seconds(3),
                    new KeyValue(x, W - D),
                    new KeyValue(y, H - D)
            )
        );
        timeline.setAutoReverse(true);
        timeline.setCycleCount(Timeline.INDEFINITE);

        final Canvas canvas = new Canvas(W, H);
        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                GraphicsContext gc = canvas.getGraphicsContext2D();
                gc.setFill(Color.CORNSILK);
                gc.fillRect(0, 0, W, H);
                gc.setFill(Color.FORESTGREEN);
                gc.fillOval(
                    x.doubleValue(),
                    y.doubleValue(),
                    D,
                    D
                );
            }
        };

        stage.setScene(
            new Scene(
                new Group(
                    canvas
                )
            )
        );
        stage.show();

        timer.start();
        timeline.play();
    }

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

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

为不透明度设置DoubleProperty的动画,范围从1到0,与动画xy的属性的方式完全相同,然后使用{{3}根据更改的属性设置颜色:

import javafx.animation.AnimationTimer;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FadeCircleOnCanvas extends Application {
    public static final double W = 200; // canvas dimensions.
    public static final double H = 200;

    public static final double D = 20;  // diameter.

    @Override public void start(Stage stage) {
        DoubleProperty opacity  = new SimpleDoubleProperty();

        Timeline timeline = new Timeline(
            new KeyFrame(Duration.seconds(0),
                    new KeyValue(opacity, 1)
            ),
            new KeyFrame(Duration.seconds(3),
                    new KeyValue(opacity, 0)
            )
        );
        timeline.setAutoReverse(true);
        timeline.setCycleCount(Timeline.INDEFINITE);

        final Canvas canvas = new Canvas(W, H);
        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                GraphicsContext gc = canvas.getGraphicsContext2D();
                gc.setFill(Color.CORNSILK);
                gc.fillRect(0, 0, W, H);
                gc.setFill(Color.FORESTGREEN.deriveColor(0, 1, 1, opacity.get()));
                gc.fillOval(
                    W/2,
                    H/2,
                    D,
                    D
                );
            }
        };

        stage.setScene(
            new Scene(
                new Group(
                    canvas
                )
            )
        );
        stage.show();

        timer.start();
        timeline.play();
    }

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

一般情况下,我不想为此使用Canvas,只是使用放置在Shape中的Pane个实例。然后,您可以直接更改Shape的预定义属性,或者在许多情况下使用特定的预定义动画(例如Color.deriveColor(...)TranslateTransition)。

以下是使用此技术的相同示例:

import javafx.animation.FadeTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

    public class FadeCircleOnCanvas extends Application {
        public static final double W = 200; // canvas dimensions.
        public static final double H = 200;

        public static final double D = 20;  // diameter.

        @Override public void start(Stage stage) {

            Circle circle = new Circle(W/2, H/2, D, Color.FORESTGREEN);

            FadeTransition fade = new FadeTransition(Duration.seconds(3), circle);
            fade.setFromValue(1);
            fade.setToValue(0);
            fade.setAutoReverse(true);
            fade.setCycleCount(Timeline.INDEFINITE);

            Pane pane = new Pane(circle);

            stage.setScene(
                new Scene(
                    pane, W, H, Color.CORNSILK
                )
            );
            stage.show();

            fade.play();
        }

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