tornadofx演示分形图的绘制

Easter79
• 阅读 447

枚举类中有抽象方法、有构造函数,每个值继承该方法实现自己的业务行为逻辑。 combobox绑定枚举类。 togglegroup包裹radiobutton,利用枚举类的值批量生成radiobutton

查看演示:https://www.bilibili.com/video/av60656281/

import javafx.application.Application
import javafx.beans.property.SimpleObjectProperty
import javafx.geometry.Orientation
import javafx.scene.Group
import javafx.scene.paint.Color
import  tornadofx.*
import kotlin.math.max

fun main() = Application.launch(LearnApp::class.java)
class LearnApp : App(LearnV::class)

class LearnV : View("分形图的绘制") {
    val x = doubleProperty()
    val y = doubleProperty()
    val widthh = doubleProperty()
    val heightt = doubleProperty()
    val depth = intProperty()
    val maxDepth = intProperty()
    lateinit var g: Group
    val color=SimpleObjectProperty(this, "color", MyColor.Indigo)
    val fractal = SimpleObjectProperty(this, "fractal", FractalShape.Rectangle)

    override val root = borderpane {
        fun draw(fs:FractalShape){
            center?.getChildList()?.clear()
            fs.drawFractal(g,
                x.value,
                y.value,
                widthh.value,
                heightt.value,
                depth.value,
                maxDepth.value,
                color.value)
        }
        style = "-fx-font-size: 14pt; "
        top = vbox(5) {
            form {
                fieldset(labelPosition = Orientation.VERTICAL) {
                    hbox(5) {
                        field("x:") {
                            textfield(x) {
                                text = "10"
                            }
                        }
                        field("y:") {
                            textfield(y) {
                                text = "10"
                            }
                        }
                        field("widthh:") {
                            textfield(widthh) {
                                text = "800"
                            }
                        }
                        field("heightt:") {
                            textfield(heightt) {
                                text = "400"
                            }
                        }
                        field("depth:") {
                            textfield(depth) {
                                text = "1"
                            }
                        }
                        field("maxDepth:") {
                            textfield(maxDepth) {
                                text = "5"
                            }
                        }
                    }
                    hbox(5) {
                        field("color:") {
                            combobox(property = color, values = MyColor.all)
                        }
                        field("depth:") {
                            combobox(property = depth, values = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
                        }
                        field("maxDepth:") {
                            combobox(property = maxDepth, values = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
                        }
                        field("FractalShape:") {
                            combobox(
                                property = fractal,
                                values = FractalShape.all
                            ) {
                                selectionModel.selectedItemProperty().addListener { _, _, _ ->
                                    selectionModel.selectedItem?.let {
                                        run{
                                            draw(it)
                                        }
                                    }
                                }
                            }
                        }

                        field(""){
                            togglegroup {
                                FractalShape.values().forEach { fs->
                                    radiobutton(fs.name){
                                        action{
                                            run{
                                                draw(fs)
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                prefWidth = 200.0
            }
        }
        center = group {
            g = this
        }
        prefHeight = 800.0
        prefWidth = 1000.0
    }
}
enum class MyColor(val color:Color) {
    Red(Color.RED),Blue(Color.BLUE),Indigo(Color.INDIGO),Yellow(Color.YELLOW);
    companion object {
        val all by lazy { values().toList() }
    }
}
enum class FractalShape {
    Rectangle {
        tailrec override fun drawFractal(g: Group, x: Double, y: Double, w: Double, h: Double, depth: Int, maxDepth: Int,color:MyColor) {
            if (depth == maxDepth) {
                g.run {
                    add(
                        rectangle(x, y, w, h) {
                            fill = color.color
                        }
                    )
                }
                return
            }
            if ((w <= 1).and(h <= 1)) {
                g.run {
                    add(
                        rectangle(x, y, max(w, 1.0), max(h, 1.0)) {
                            fill = color.color
                        }
                    )
                }
                return
            }
            val w_3 = w / 3
            val h_3 = h / 3
            drawFractal(g, x, y, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + 2 * w_3, y, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + 2 * w_3, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color)
            return
        }
    },
    Circle {
        tailrec override fun drawFractal(g: Group, x: Double, y: Double, w: Double, h: Double, depth: Int, maxDepth: Int,color:MyColor) {
            if (depth == maxDepth) {
                g.run {
                    add(
                        circle(x, y, w) {
                            fill = color.color
                        }
                    )
                }
                return
            }
            if (w <= 1) {
                g.run {
                    add(
                        circle(x, y, max(w, 1.0)) {
                            fill = color.color
                        }
                    )
                }
                return
            }

            val w_3 = w / 3
            val h_3 = h / 3
            drawFractal(g, x, y, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + 2 * w_3, y, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + 2 * w_3, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color)
            return
        }
    },
    Ellipse {
        tailrec override fun drawFractal(g: Group, x: Double, y: Double, w: Double, h: Double, depth: Int, maxDepth: Int,color:MyColor) {
            if (depth == maxDepth) {
                with(g) {
                    add(
                        ellipse(x, y, w, h) {
                            fill = color.color
                        }
                    )
                }
                return
            }
            if ((w <= 1).and(h <= 1)) {
                g.run {
                    add(
                        ellipse(x, y, max(w, 1.0), max(h, 1.0)) {
                            fill = color.color
                        }
                    )
                }
                return
            }

            val w_3 = w / 3
            val h_3 = h / 3
            drawFractal(g, x, y, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + 2 * w_3, y, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color)
            drawFractal(g, x + 2 * w_3, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color)
            return
        }
    };

    companion object {
        val all by lazy { values().toList() }
    }

    abstract fun drawFractal(g: Group, x: Double, y: Double, w: Double, h: Double, depth: Int, maxDepth: Int,color:MyColor)
}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
java枚举类
使用enum声明,默认直接继承了java.lang.Enum类,而不是Object类;枚举类的对象是固定的,实例个数有限,不可以再new(),枚举对象后可以跟()。枚举元素必须位于枚举类体中的最开始部分,枚举元素后要有分号与其他成员分隔。枚举类的构造方法的权限修饰符默认是private;一旦枚举对象后面加上{},那么该对象实际是枚举匿名内部
Wesley13 Wesley13
2年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k