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

javafx框架tornadofx画个心形

原创
作者头像
用户6167008
修改2019-09-03 18:35:23
1.4K0
修改2019-09-03 18:35:23
举报
import javafx.animation.AnimationTimer
import javafx.application.Application
import javafx.application.Platform
import javafx.geometry.Pos
import javafx.scene.canvas.GraphicsContext
import javafx.scene.control.RadioButton
import javafx.scene.paint.Color
import tornadofx.*
import java.util.*
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin

fun main(args: Array<String>) {
    Application.launch(TestApp::class.java, *args)
}

class TestApp : App(TestView::class)
class TestView : View("逐个显示图形") {
    val isRun = booleanProperty()
    val pNums = intProperty()
    val aniMate = AniTimer()
    var t = -PI
    var renderedList: MutableList<Point> = LinkedList<Point>()

val step= doubleProperty(0.01)
    lateinit var context: GraphicsContext
    override val root = borderpane {

        top = hbox(10) {
            style {
                alignment = Pos.CENTER
            }
            label("点密度")
            togglegroup {
                listOf(0.01, 0.03, 0.05, 0.07, 0.1).map { v ->
                    radiobutton(v.toString(), this, v) { if (v === 0.01) isSelected = true }
                }
                selectedToggleProperty().addListener { _, _, newValue ->
//                    step.value = (selectedToggle as RadioButton).text.toDouble()
                    step.value = (newValue as RadioButton).text.toDouble()
                }
            }
            button("start") {
                isRun.addListener { _,_,isrun->
                    if (isrun)
                        this.text = "Pause"
                    else
                        this.text = "Start"
                }
                action {
                    if (isRun.value) {
                        aniMate.stop()
                        this.text = "Start"
                        isRun.value = false
                    } else {
                        t = -PI
                        renderedList.clear()
                        pNums.value=0
                        aniMate.start()
                        this.text = "Pause"
                        isRun.value = true
                    }
                }
            }
            label(pNums.stringBinding { "当前点数:$it" })
        }

        center = canvas(800.0, 600.0) {
            style {
                alignment = Pos.CENTER
            }
            context = this.graphicsContext2D
            paddingAll = 30
        }
    }

    inner class AniTimer : AnimationTimer() {
        var lastTime = 0L
        val r = 10.0
        var syncLock = Any()
        override fun handle(now: Long) {
            if ((now - lastTime) > 10000000) {
                lastTime = now
            } else {
                return
            }
            Platform.runLater {
                val y = 200 + (2 * cos(t) - cos(2 * t)) * -80
                val x = 400 + (2 * sin(t) - sin(2 * t)) * 100
                val p = Point(x, y)
                // 锁住,防止其他线程修改
                synchronized(syncLock) {
                    // 添加历史记录
                    renderedList.add(p)
                    // 清屏
                    context.fill = Color.WHITE
                    context.clearRect(0.0, 0.0, 800.0, 600.0)
                    context.fill =  Color.RED
                    // 渲染点
                    for (point in renderedList) {
                        context.fillOval(point.x, point.y, r, r)

                    }
                    pNums.value += 1
                    t += step.value
                    // 控制点的数量
                    if (t > PI) {
                        aniMate.stop()
                        isRun.value=false
                    }
                }
            }
        }
    }
}

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

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

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

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

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

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