tornadofx往canvas中动态添加图形

以下代码通过Timer实现动画,无法暂停、继续


import javafx.application.Application
import javafx.application.Platform
import javafx.geometry.Pos
import javafx.scene.canvas.GraphicsContext
import javafx.scene.paint.Color
import tornadofx.*
import java.util.*

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

class TestApp:App(TestView::class)
class TestView : View("逐个显示图形") {
    val timer = Timer()
    val isRun= booleanProperty()
    val pNums= intProperty()
    lateinit var context: GraphicsContext
    override val root = borderpane {

        top = hbox(10) {
            style{
                alignment= Pos.CENTER
            }
            button("start") {
                action {
                    if(isRun.value){
                        timer.cancel()
                        this.text="Start"
                        isRun.value=false
                    }else{
                        timer.schedule(task, 0, 100)
                        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
        }
        primaryStage.setOnCloseRequest { timer.cancel() }
    }
    val task = object : TimerTask() {

        var renderedList: MutableList<Point> = LinkedList<Point>()

        var syncLock = Any()

        override fun run() {
            Platform.runLater {
                val random = Random()
                val x = 100 + random.nextDouble() * 500
                val y = 100 + random.nextDouble() * 500
                val p = Point(x, y,Color.RED)
                // 锁住,防止其他线程修改
                synchronized(syncLock) {
                    // 添加历史记录
                    renderedList.add(p)
                    // 清屏
                    context.fill = Color.WHITE
                    context.clearRect(0.0, 0.0, 800.0, 600.0)
                    context.fill = p.color
                    // 渲染点
                    for (point in renderedList) {
                        context.fillOval(point.x, point.y, 20.0, 20.0)

                    }
                    pNums.value+=1
                    // 控制点的数量
                    if (renderedList.size > 120) {
                        renderedList.clear()
                        pNums.value=0
                    }
                }
            }
        }
    }
}

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

以下代码通过AnimationTimer实现动画,可以暂停、继续


import javafx.animation.AnimationTimer
import javafx.application.Application
import javafx.application.Platform
import javafx.geometry.Pos
import javafx.scene.canvas.GraphicsContext
import javafx.scene.paint.Color
import tornadofx.*
import java.util.*

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

class TestApp:App(TestView::class)
class TestView : View("逐个显示图形") {
    val timer = Timer()
    val isRun= booleanProperty()
    val pNums= intProperty()
    val aniMate=AniTimer()
    lateinit var context: GraphicsContext
    override val root = borderpane {

        top = hbox(10) {
            style{
                alignment= Pos.CENTER
            }
            button("start") {
                action {
                    if(isRun.value){
                        aniMate.stop()
                        this.text="Start"
                        isRun.value=false
                    }else{
                        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
        }
        primaryStage.setOnCloseRequest { timer.cancel() }
    }
    inner class AniTimer : AnimationTimer() {
        var lastTime = 0L
        var renderedList: MutableList<Point> = LinkedList<Point>()

        var syncLock = Any()
        override fun handle(now: Long) {
            if ((now - lastTime) > 10000000) {
                lastTime = now
            } else {
                return
            }
            Platform.runLater {
                val random = Random()
                val x = 100 + random.nextDouble() * 500
                val y = 100 + random.nextDouble() * 500
                val p = Point(x, y,Color.RED)
                // 锁住,防止其他线程修改
                synchronized(syncLock) {
                    // 添加历史记录
                    renderedList.add(p)
                    // 清屏
                    context.fill = Color.WHITE
                    context.clearRect(0.0, 0.0, 800.0, 600.0)
                    context.fill = p.color
                    // 渲染点
                    for (point in renderedList) {
                        context.fillOval(point.x, point.y, 20.0, 20.0)

                    }
                    pNums.value+=1
                    // 控制点的数量
                    if (renderedList.size > 120) {
                        renderedList.clear()
                        pNums.value=0
                    }
                }
            }
        }
    }
}

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

 

转载于:https://my.oschina.net/u/3820046/blog/3101104

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值