Last active
March 27, 2017 17:57
-
-
Save timyates/11253519 to your computer and use it in GitHub Desktop.
Observable Animation Timer with JavaFX 8 and RxJava
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.animation.AnimationTimer; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Label; | |
import javafx.scene.paint.Color; | |
import rx.Observable; | |
import rx.subscriptions.Subscriptions; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
Observable<Long> timer ; | |
@FXML | |
Label label ; | |
@Override | |
public void initialize( URL url, ResourceBundle resourceBundle ) { | |
timer = Observable.create( subscriber -> { | |
final AnimationTimer timer = new AnimationTimer() { | |
@Override | |
public void handle( long l ) { | |
subscriber.onNext( l ) ; | |
} | |
} ; | |
timer.start() ; | |
return Subscriptions.create( () -> { | |
timer.stop(); | |
subscriber.onCompleted() ; | |
} ) ; | |
} ) ; | |
// Change the timer text | |
timer.subscribe( time -> label.setText( Long.toString( time ) ) ) ; | |
// And the color based on time | |
timer.map( time -> (int)( time % 255 ) ) | |
.map( limited -> Color.rgb( limited, limited, limited ) ) | |
.subscribe( color -> label.setTextFill( color ) ) ; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This version makes 2 Observables, one for system time and | |
// one for the deltas since last animation update | |
package sample; | |
import javafx.animation.AnimationTimer; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.Label; | |
import javafx.scene.paint.Color; | |
import rx.Observable; | |
import rx.subscriptions.Subscriptions; | |
import rx.util.functions.Func1; | |
import java.net.URL; | |
import java.util.ResourceBundle; | |
public class Controller implements Initializable { | |
Observable<Long> timer ; | |
Observable<Long> deltas ; | |
@FXML | |
Label label ; | |
@Override | |
public void initialize( URL url, ResourceBundle resourceBundle ) { | |
// Make an Observable of the current system time from AnimationTimer | |
timer = Observable.create( subscriber -> { | |
final AnimationTimer timer = new AnimationTimer() { | |
@Override | |
public void handle( long time ) { | |
subscriber.onNext( time ) ; | |
} | |
} ; | |
timer.start() ; | |
return Subscriptions.create( () -> { | |
timer.stop(); | |
subscriber.onCompleted() ; | |
} ) ; | |
} ) ; | |
// And make an observable of the deltas based on this system time | |
deltas = timer.map( new Func1<Long, Long>() { | |
long previous = 0 ; | |
@Override | |
public Long call( Long time ) { | |
long delta = time - previous ; | |
previous = time ; | |
return delta ; | |
} | |
} ) ; | |
// Subscribe to timer to change the timer text | |
timer.subscribe( time -> label.setText( Long.toString( time ) ) ) ; | |
// And the color based on deltas | |
deltas.map( time -> (int)( ( time / 1000 ) % 255 ) ) | |
.map( limited -> Color.rgb( limited, 0, 255 - limited ) ) | |
.subscribe( color -> label.setTextFill( color ) ) ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment