Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 660c2d7 | 2020-02-14 13:18:37 +0300 | [diff] [blame] | 2 | * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | // This file was automatically generated from coroutines-guide-ui.md by Knit tool. Do not edit. |
Roman Elizarov | 660c2d7 | 2020-02-14 13:18:37 +0300 | [diff] [blame] | 6 | package kotlinx.coroutines.javafx.guide.exampleUiBasic02 |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 7 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 8 | import kotlinx.coroutines.* |
| 9 | import kotlinx.coroutines.channels.* |
| 10 | import kotlinx.coroutines.javafx.JavaFx as Main |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 11 | import javafx.application.Application |
| 12 | import javafx.event.EventHandler |
| 13 | import javafx.geometry.* |
| 14 | import javafx.scene.* |
| 15 | import javafx.scene.input.MouseEvent |
| 16 | import javafx.scene.layout.StackPane |
| 17 | import javafx.scene.paint.Color |
| 18 | import javafx.scene.shape.Circle |
| 19 | import javafx.scene.text.Text |
| 20 | import javafx.stage.Stage |
| 21 | |
| 22 | fun main(args: Array<String>) { |
| 23 | Application.launch(ExampleApp::class.java, *args) |
| 24 | } |
| 25 | |
| 26 | class ExampleApp : Application() { |
| 27 | val hello = Text("Hello World!").apply { |
| 28 | fill = Color.valueOf("#C0C0C0") |
| 29 | } |
| 30 | |
| 31 | val fab = Circle(20.0, Color.valueOf("#FF4081")) |
| 32 | |
| 33 | val root = StackPane().apply { |
| 34 | children += hello |
| 35 | children += fab |
| 36 | StackPane.setAlignment(hello, Pos.CENTER) |
| 37 | StackPane.setAlignment(fab, Pos.BOTTOM_RIGHT) |
| 38 | StackPane.setMargin(fab, Insets(15.0)) |
| 39 | } |
| 40 | |
| 41 | val scene = Scene(root, 240.0, 380.0).apply { |
| 42 | fill = Color.valueOf("#303030") |
| 43 | } |
| 44 | |
| 45 | override fun start(stage: Stage) { |
| 46 | stage.title = "Example" |
| 47 | stage.scene = scene |
| 48 | stage.show() |
| 49 | setup(hello, fab) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fun setup(hello: Text, fab: Circle) { |
Roman Elizarov | dc29b07 | 2018-09-11 18:42:03 +0300 | [diff] [blame] | 54 | GlobalScope.launch(Dispatchers.Main) { // launch coroutine in the main thread |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 55 | for (i in 10 downTo 1) { // countdown from 10 to 1 |
| 56 | hello.text = "Countdown $i ..." // update text |
| 57 | delay(500) // wait half a second |
| 58 | } |
| 59 | hello.text = "Done!" |
| 60 | } |
| 61 | } |