views:

120

answers:

2

Just started with JavaFX today and can't figure out how to do a discrete color flip on an object. I can set up a Timeline with two keyframes and have it set a color value that an object binds to it's fill variable... but I get interpolated colors when what I want is a discrete flip flop.

A: 

This may be more what you're looking for, but it doesn't repeat indefinitely - might be a bug? Also, I suspect that there are neater ways of doing this.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.animation.transition.PauseTransition;
import javafx.animation.transition.SequentialTransition;

import javafx.animation.Timeline;

var fillColor: Color;

Stage {
    title : "MyApp"
    scene: Scene {
        width: 400
        height: 200
        content: [
                Rectangle {
                x: 10, y: 10
                width: 140, height: 90
                fill: bind fillColor
            }
            ]
    }
}

var seqTransition = SequentialTransition {
    repeatCount: Timeline.INDEFINITE
      content: [
        PauseTransition {
            duration: 1s
            action: function():Void {
                fillColor = Color.BLUE;
            }
        },
        PauseTransition {
            duration: 1s
            action: function():Void {
                fillColor = Color.RED;
            }
        }
      ]
    }
    seqTransition.play();
Matthew Hegarty
+1  A: 

Or you could use a DISCRETE interpolator on KeyFrame's key value for fillColor.

Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        at(0s) { fillColor => Color.BLUE tween Interpolator.DISCRETE; }
        at(1s) { fillColor => Color.RED tween Interpolator.DISCRETE; }
    ]
}
JimClarke