1

Is there a way to animate a "move" of LinearGradient nested into a given Shape? Let's say for example make the LinearGradient move to right or to left? I thought about the translateXproperty or the Path object, but I want to move only the LinearGradient.

EDIT 28.09.2014

Thank you @James_D for the valueable help. But in the context of my project I've needed a different solution. I've worked it out, and the result can be seen below on the working example:

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

bluevoxel
  • 4,978
  • 11
  • 45
  • 63
  • You can do this using this technique: http://stackoverflow.com/questions/22817897/javafx-is-it-possible-to-set-css-by-code/22818408#22818408 – James_D Sep 25 '14 at 15:08
  • Thank you, but do you see any other solutions for this issue? – bluevoxel Sep 25 '14 at 17:39
  • You have to animate some property, and you want to update the linear gradient based on that changing property, so all other ways to do it will be some variant of that. What is your objection to this solution? – James_D Sep 25 '14 at 18:30
  • Basically I had to have the posibility of changing the velocity of the animation, the slope degree of the gradient and it's spatial frequency on the run, so I needed to use "java-only" solution. – bluevoxel Sep 28 '14 at 19:23

0 Answers0