import java.util.Date;
import com.sun.javafx.runtime.async.AbstractAsyncOperation;
import com.sun.javafx.runtime.async.AsyncOperationListener;
public class LongLongTaskImpl extends AbstractAsyncOperation<Date> {
public LongLongTaskImpl(AsyncOperationListener<Date> listener) {
super(listener);
}
// 非同期にコールされるメソッド
@Override
public Date call() {
try {
// 10秒スリープ
Thread.sleep(10000L);
} catch (InterruptedException ex) {
// 非同期処理のキャンセルが行われると、
// InterruptedException例外が発生する
// このため、InterruptedException例外が発生した時に
// 速やかにcallメソッドを抜け出る必要がある
}
// 現在時刻を返す
return new Date();
}
}
import javafx.async.AbstractAsyncOperation;
import java.lang.Exception;
import java.lang.Object;
import java.util.Date;
import com.sun.javafx.runtime.async.AsyncOperationListener;
public class LongLongTask extends AbstractAsyncOperation {
// 非同期処理の結果
public var result: Date;
// 非同期処理を行うJavaクラス
var task: LongLongTaskImpl;
public override function start(): Void {
// 非同期処理の開始
task = new LongLongTaskImpl(listener);
task.start();
}
public override function cancel(): Void {
// 非同期処理のキャンセル
task.cancel();
}
public override function onCompletion(value: Object): Void {
// valueがLongLongTaskImpl#callメソッドの戻り値
result = value as Date
}
}
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.lang.Object;
import java.util.Date;
// 非同期処理の結果を保持させる変数
var result: Date = new Date();
// 非同期処理を5秒ごとに行うタイムライン
Timeline {
repeatCount: Timeline.INDEFINITE
autoReverse: true
keyFrames: [
KeyFrame {
time: 0s
action: function() {
// 非同期処理クラスを生成し、非同期処理を開始する
var task: LongLongTask;
task = LongLongTask {
// 非同期処理後にコールされる関数
onDone: function(flag: Boolean) : Void {
result = task.result;
}
}
}
},
KeyFrame {
time: 5s
}
]
}.play();
// ボール
var ball = Circle {
centerX: 0
centerY: 0
radius: 20
fill: RadialGradient {
centerX: 0.25
centerY: 0.25
stops: [
Stop {
offset: 0.0
color: Color.WHITE
},
Stop {
offset: 0.6
color: Color.RED
}
]
}
}
// ボールの水平方向のアニメーション
Timeline {
repeatCount: Timeline.INDEFINITE
autoReverse: true
keyFrames: [
KeyFrame {
time: 0s
values: ball.translateX => 20.0
},
KeyFrame {
time: 2s
values: ball.translateX => 280.0
},
]
}.play();
// ボールの垂直方向のアニメーション
Timeline {
repeatCount: Timeline.INDEFINITE
autoReverse: true
keyFrames: [
KeyFrame {
time: 0s
values: ball.translateY => 20.0
},
KeyFrame {
time: 1.3s
values: ball.translateY => 180.0
},
]
}.play();
Stage {
title: "Long Long Task"
scene: Scene {
width: 300
height: 200
content: [
ball,
Text {
font : Font {
size: 20
}
x: 10,
y: 100
content: bind "{result}"
}
]
}
}