【问题标题】:fading in and fading out of an circle in javafx canvas在javafx画布中淡入和淡出一个圆圈
【发布时间】:2015-03-12 15:43:07
【问题描述】:

我想在 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); }
}  

非常感谢您的帮助

【问题讨论】:

标签: animation canvas javafx javafx-8 fade


【解决方案1】:

以与为xy 的属性设置动画的方式完全相同的方式为从1 到0 的不透明度设置动画DoubleProperty,然后使用Color.deriveColor(...) 根据改变属性:

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); }
}  

一般来说,我不喜欢为此使用画布,而只是使用放置在Pane 中的Shape 实例。然后可以直接更改Shape的预定义属性,或者在很多情况下使用特定的预定义动画(例如TranslateTransitionFadeTransition)。

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

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); }
    }  

【讨论】:

  • 谢谢。但我想知道,你怎么能修改这个方法,让一个圆圈淡入一秒钟,然后另一个圆圈又淡入。或者使用窗格更容易实现这一点。
  • 使用窗格更容易实现。但无论哪种方式都应该很容易适应。
  • 我还希望能够一次淡入一个对象。例如,使用 for 循环遍历每个节点并在窗格中淡入并等待一两秒钟,然后再淡入下一个
  • 然后写代码做Lycon,StackOverflow不是编码服务。考虑调整 James 的基于窗格/节点的解决方案并使用 SequentialTransition。如果在尝试编写代码后遇到问题,您可以随时提出新问题。
猜你喜欢
  • 2016-02-07
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 2014-01-20
  • 2012-12-18
  • 2011-09-17
  • 2014-07-20
  • 1970-01-01
相关资源
最近更新 更多