次のコードは、2 つのグラデーションの間で四角形を滑らかにフェードする必要があります。
public class FXTest extends Application {
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Rectangle rect = new Rectangle(200, 100, new LinearGradient(0, 0, 1, 1, true, CycleMethod.REPEAT, new Stop(0, Color.BLACK), new Stop(1, Color.RED)));
root.getChildren().add(rect);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
getFillTimeline(rect, new LinearGradient(0, 0, 1, 1, true, CycleMethod.REPEAT, new Stop(0, Color.RED), new Stop(1, Color.BLACK))).play();
}
private Timeline getFillTimeline(Shape node, Paint newPaint) {
Timeline t = new Timeline();
t.getKeyFrames().add(new KeyFrame(Duration.ZERO, new KeyValue(node.fillProperty(), node.getFill())));
t.getKeyFrames().add(new KeyFrame(Duration.seconds(3), new KeyValue(node.fillProperty(), newPaint)));
return t;
}
}
上記の例で単色を指定すると、フェードは問題なくスムーズに発生します。ただし、(上記のコードのように) グラデーションを使用すると、フェードは発生せず、グラデーションは 3 秒後 (またはタイムラインの長さに関係なく) 突然切り替わります。
単純な色ではなくグラデーションを使用する場合、どのように段階的なフェードを実現できますか? なぜこれが私が考えているように機能することを意図していないのか、それともバグなのでしょうか?
どちらの場合でも、この効果を達成するための最善の回避策は何でしょうか?