Skip to content

Commit 3979e99

Browse files
author
Dominik Liebler
committed
step1
1 parent 6e4a595 commit 3979e99

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/main/kotlin/Main.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,46 @@ import javafx.scene.Scene
44
import javafx.scene.control.Label
55
import javafx.scene.layout.Pane
66
import javafx.stage.Stage
7+
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.GlobalScope
9+
import kotlinx.coroutines.delay
10+
import kotlinx.coroutines.launch
711

8-
class MyApp : Application() {
12+
class MillisElapsedCounter(private val observer: Observer) {
13+
fun start() {
14+
GlobalScope.launch(Dispatchers.Default) {
15+
var millisElapsed = 0
16+
17+
while (true) {
18+
delay(1000)
19+
millisElapsed += 1000
20+
observer.notify(millisElapsed)
21+
}
22+
}
23+
}
24+
25+
interface Observer {
26+
fun notify(millisElapsed: Int)
27+
}
28+
}
29+
30+
class MyApp : Application(), MillisElapsedCounter.Observer {
931
private val button = Label().also { it.text = "waiting for 0s" }
1032

1133
override fun start(primaryStage: Stage?) {
34+
MillisElapsedCounter(this).start()
35+
1236
val pane = Pane().also {
1337
it.children.add(button)
1438
}
1539

16-
primaryStage?.scene = Scene(pane,200.0,200.0)
40+
primaryStage?.scene = Scene(pane, 200.0, 200.0)
1741
primaryStage?.show()
1842
}
43+
44+
override fun notify(millisElapsed: Int) {
45+
button.text = "waiting for ${millisElapsed / 1000}s"
46+
}
1947
}
2048

2149
fun main() {

0 commit comments

Comments
 (0)