在给定的Shape中移动LinearGradient

时间:2014-09-25 15:03:58

标签: java animation javafx javafx-2 javafx-8

有没有办法动画LinearGradient嵌套到给定Shape的“移动”?比方说让LinearGradient向右或向左移动?我考虑了translateXpropertyPath对象,但我只想移动LinearGradient

编辑28.09.2014

感谢@James_D提供有价值的帮助。但在我的项目中,我需要一个不同的解决方案。我已经解决了,结果可以在下面的工作示例中看到:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
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;

public class LinearGradientAnimation extends Application {

    private Rectangle rect;

    private Timeline timeline;

    private double widthOfOneGradientCycle = 20.0;
    private double gradientSlopeDegree = 45.0;
    private double xStartStatic = 100.0;
    private double yStartStatic = 100.0;
    private double xEndStatic = xStartStatic + (widthOfOneGradientCycle * Math.cos(Math.toRadians(gradientSlopeDegree)));
    private double yEndStatic = yStartStatic + (widthOfOneGradientCycle * Math.sin(Math.toRadians(gradientSlopeDegree)));

    public Parent createContent() {

        /* layout */
        BorderPane layout = new BorderPane();

        /* layout -> Rectangle */
        rect = new Rectangle(0, 0, 200, 200);

        /* layout -> Rectangle -> LinearGradient Animation */

        timeline = new Timeline();
        for (int i = 0; i < 10; i++) {
            int innerIterator = i;
            KeyFrame kf = new KeyFrame(Duration.millis(30 * innerIterator), new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent ae) {

                    double runningRadius = innerIterator * (widthOfOneGradientCycle / 10);
                    double xStartDynamic = xStartStatic + (runningRadius * Math.cos(Math.toRadians(gradientSlopeDegree)));
                    double yStartDynamic = yStartStatic + (runningRadius * Math.sin(Math.toRadians(gradientSlopeDegree)));
                    double xEndDynamic = xEndStatic + (runningRadius * Math.cos(Math.toRadians(gradientSlopeDegree)));
                    double yEnddynamic = yEndStatic + (runningRadius * Math.sin(Math.toRadians(gradientSlopeDegree)));

                    LinearGradient gradient = new LinearGradient(xStartDynamic, yStartDynamic, xEndDynamic, yEnddynamic, 
                            false, CycleMethod.REPEAT, new Stop[] {
                                new Stop(0.0, Color.WHITE),
                                new Stop(0.5, Color.BLACK),
                                new Stop(1.0, Color.WHITE)
                    });
                    rect.setFill(gradient);
                }
            });
            timeline.getKeyFrames().add(kf);
        }
        timeline.setCycleCount(Timeline.INDEFINITE);

        /* return layout */
        layout.setCenter(rect);
        return layout;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent()));
        stage.setWidth(300);
        stage.setHeight(300);
        stage.show();

        timeline.play();
    }

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

https://gist.github.com/bluevoxel/44dcf297ee9503e72114

0 个答案:

没有答案