前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一颗心到一个圆的演变,javafx框架tornadofx动画演示

一颗心到一个圆的演变,javafx框架tornadofx动画演示

原创
作者头像
用户6167008
修改2019-09-16 11:11:54
6170
修改2019-09-16 11:11:54
举报

采用runAsync和Thread.sleep(10)实现动画效果

代码语言:txt
复制
import javafx.geometry.Pos
import javafx.scene.canvas.GraphicsContext
import javafx.scene.control.RadioButton
import javafx.scene.paint.Color
import javafx.scene.text.FontWeight
import tornadofx.*
import java.util.*
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin


class TestApp : App(TestView::class)
class TestView : View("一颗心到一个圆的演变") {
    val isRun = booleanProperty()
    var t = -PI
    val r = 10.0
    var renderedList: MutableList<Point> = LinkedList<Point>()
    val iip = intProperty(2)
    val step = doubleProperty(0.1)
    lateinit var context: GraphicsContext
    override val root = borderpane {
        top = vbox(10) {
            style { borderColor += box(Color.TRANSPARENT, Color.TRANSPARENT, Color.LIGHTGREY, Color.TRANSPARENT) }
            alignment = Pos.CENTER
            hbox(10) {
                label("点密度:")
                togglegroup {
                    listOf(0.01, 0.03, 0.05, 0.07, 0.1).map { v ->
                        radiobutton(v.toString(), this, v) { if (v === 0.1) isSelected = true }
                    }
                    selectedToggleProperty().addListener { _, _, newValue ->
                        step.value = (newValue as RadioButton).text.toDouble()
                        runn()
                    }
                }
            }
            hbox(10) {
                label("花瓣数:")
                togglegroup {
                    val tg = this
                    vbox(10) {
                        hbox(10) {
                            listOf(2, 3, 4, 5, 6, 7, 8, 9, 10, 11).map { v ->
                                radiobutton(v.toString(), tg, v) { if (v === 2) isSelected = true }
                            }
                        }
                        hbox(10) {
                            listOf(12, 13, 14, 15, 16, 17, 18, 19, 20).map { v ->
                                radiobutton(v.toString(), tg, v)
                            }
                        }
                        hbox(10) {
                            listOf(21, 22, 23, 24, 25, 26, 27, 28, 150).map { v ->
                                radiobutton(v.toString(), tg, v)
                            }
                        }
                    }
                    selectedToggleProperty().addListener { _, _, newValue ->
                        iip.value = (newValue as RadioButton).text.toInt()
                        runn()
                    }
                }
                button("start") {
                    isRun.addListener { _, _, isrun ->
                        if (isrun)
                            this.text = "Pause"
                        else
                            this.text = "Start"
                    }
                    action {
                        runn()
                    }
                }
            }
        }

        center = canvas(700.0, 550.0) {
            style {
                alignment = Pos.CENTER
            }
            context = this.graphicsContext2D
            paddingAll = 30
        }
        bottom {
            add<StatusView>()
        }
    }

    fun runn() {
        // 清屏
        t=-PI
        var i=0
        context.clearRect(0.0, 0.0, 1000.0, 700.0)
        runAsync {
            while (t < PI) {
                val y = 250 + (iip.value * cos(t) - cos(iip.value * t)) * -200 / iip.value
                val x = 300 + (iip.value * sin(t) - sin(iip.value * t)) * 200 / iip.value
                val p = Point(x, y)
                renderedList.add(p)
                with(context) {
                    fill = Color.RED
                    fillOval(p.x, p.y, r, r)
                    t += step.value
                    i++
                    updateTitle(i.toString())
                    Thread.sleep(10)
                }
            }
        }
    }
}
class StatusView : View() {
    val status: TaskStatus by inject()
    override val root = vbox(4) {
        style { borderColor += box(Color.LIGHTGREY, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT) }
        label(status.title.stringBinding { "点数:$it" }).style { fontWeight = FontWeight.BOLD }
    }
}

class Point(val x: Double, val y: Double)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档