tornadofx演示分形图的绘制

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

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

1import javafx.application.Application 2import javafx.beans.property.SimpleObjectProperty 3import javafx.geometry.Orientation 4import javafx.scene.Group 5import javafx.scene.paint.Color 6import tornadofx.* 7import kotlin.math.max 8 9fun main() = Application.launch(LearnApp::class.java) 10class LearnApp : App(LearnV::class) 11 12class LearnV : View("分形图的绘制") { 13 val x = doubleProperty() 14 val y = doubleProperty() 15 val widthh = doubleProperty() 16 val heightt = doubleProperty() 17 val depth = intProperty() 18 val maxDepth = intProperty() 19 lateinit var g: Group 20 val color=SimpleObjectProperty(this, "color", MyColor.Indigo) 21 val fractal = SimpleObjectProperty(this, "fractal", FractalShape.Rectangle) 22 23 override val root = borderpane { 24 fun draw(fs:FractalShape){ 25 center?.getChildList()?.clear() 26 fs.drawFractal(g, 27 x.value, 28 y.value, 29 widthh.value, 30 heightt.value, 31 depth.value, 32 maxDepth.value, 33 color.value) 34 } 35 style = "-fx-font-size: 14pt; " 36 top = vbox(5) { 37 form { 38 fieldset(labelPosition = Orientation.VERTICAL) { 39 hbox(5) { 40 field("x:") { 41 textfield(x) { 42 text = "10" 43 } 44 } 45 field("y:") { 46 textfield(y) { 47 text = "10" 48 } 49 } 50 field("widthh:") { 51 textfield(widthh) { 52 text = "800" 53 } 54 } 55 field("heightt:") { 56 textfield(heightt) { 57 text = "400" 58 } 59 } 60 field("depth:") { 61 textfield(depth) { 62 text = "1" 63 } 64 } 65 field("maxDepth:") { 66 textfield(maxDepth) { 67 text = "5" 68 } 69 } 70 } 71 hbox(5) { 72 field("color:") { 73 combobox(property = color, values = MyColor.all) 74 } 75 field("depth:") { 76 combobox(property = depth, values = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) 77 } 78 field("maxDepth:") { 79 combobox(property = maxDepth, values = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) 80 } 81 field("FractalShape:") { 82 combobox( 83 property = fractal, 84 values = FractalShape.all 85 ) { 86 selectionModel.selectedItemProperty().addListener { _, _, _ -> 87 selectionModel.selectedItem?.let { 88 run{ 89 draw(it) 90 } 91 } 92 } 93 } 94 } 95 96 field(""){ 97 togglegroup { 98 FractalShape.values().forEach { fs-> 99 radiobutton(fs.name){ 100 action{ 101 run{ 102 draw(fs) 103 } 104 } 105 } 106 } 107 } 108 } 109 } 110 } 111 prefWidth = 200.0 112 } 113 } 114 center = group { 115 g = this 116 } 117 prefHeight = 800.0 118 prefWidth = 1000.0 119 } 120} 121enum class MyColor(val color:Color) { 122 Red(Color.RED),Blue(Color.BLUE),Indigo(Color.INDIGO),Yellow(Color.YELLOW); 123 companion object { 124 val all by lazy { values().toList() } 125 } 126} 127enum class FractalShape { 128 Rectangle { 129 tailrec override fun drawFractal(g: Group, x: Double, y: Double, w: Double, h: Double, depth: Int, maxDepth: Int,color:MyColor) { 130 if (depth == maxDepth) { 131 g.run { 132 add( 133 rectangle(x, y, w, h) { 134 fill = color.color 135 } 136 ) 137 } 138 return 139 } 140 if ((w <= 1).and(h <= 1)) { 141 g.run { 142 add( 143 rectangle(x, y, max(w, 1.0), max(h, 1.0)) { 144 fill = color.color 145 } 146 ) 147 } 148 return 149 } 150 val w_3 = w / 3 151 val h_3 = h / 3 152 drawFractal(g, x, y, w_3, h_3, depth + 1, maxDepth,color) 153 drawFractal(g, x + 2 * w_3, y, w_3, h_3, depth + 1, maxDepth,color) 154 drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1, maxDepth,color) 155 drawFractal(g, x, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color) 156 drawFractal(g, x + 2 * w_3, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color) 157 return 158 } 159 }, 160 Circle { 161 tailrec override fun drawFractal(g: Group, x: Double, y: Double, w: Double, h: Double, depth: Int, maxDepth: Int,color:MyColor) { 162 if (depth == maxDepth) { 163 g.run { 164 add( 165 circle(x, y, w) { 166 fill = color.color 167 } 168 ) 169 } 170 return 171 } 172 if (w <= 1) { 173 g.run { 174 add( 175 circle(x, y, max(w, 1.0)) { 176 fill = color.color 177 } 178 ) 179 } 180 return 181 } 182 183 val w_3 = w / 3 184 val h_3 = h / 3 185 drawFractal(g, x, y, w_3, h_3, depth + 1, maxDepth,color) 186 drawFractal(g, x + 2 * w_3, y, w_3, h_3, depth + 1, maxDepth,color) 187 drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1, maxDepth,color) 188 drawFractal(g, x, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color) 189 drawFractal(g, x + 2 * w_3, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color) 190 return 191 } 192 }, 193 Ellipse { 194 tailrec override fun drawFractal(g: Group, x: Double, y: Double, w: Double, h: Double, depth: Int, maxDepth: Int,color:MyColor) { 195 if (depth == maxDepth) { 196 with(g) { 197 add( 198 ellipse(x, y, w, h) { 199 fill = color.color 200 } 201 ) 202 } 203 return 204 } 205 if ((w <= 1).and(h <= 1)) { 206 g.run { 207 add( 208 ellipse(x, y, max(w, 1.0), max(h, 1.0)) { 209 fill = color.color 210 } 211 ) 212 } 213 return 214 } 215 216 val w_3 = w / 3 217 val h_3 = h / 3 218 drawFractal(g, x, y, w_3, h_3, depth + 1, maxDepth,color) 219 drawFractal(g, x + 2 * w_3, y, w_3, h_3, depth + 1, maxDepth,color) 220 drawFractal(g, x + w_3, y + h_3, w_3, h_3, depth + 1, maxDepth,color) 221 drawFractal(g, x, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color) 222 drawFractal(g, x + 2 * w_3, y + 2 * h_3, w_3, h_3, depth + 1, maxDepth,color) 223 return 224 } 225 }; 226 227 companion object { 228 val all by lazy { values().toList() } 229 } 230 231 abstract fun drawFractal(g: Group, x: Double, y: Double, w: Double, h: Double, depth: Int, maxDepth: Int,color:MyColor) 232}
点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

java枚举类

使用enum声明,默认直接继承了java.lang.Enum类,而不是Object类;枚举类的对象是固定的,实例个数有限,不可以再new(),枚举对象后可以跟()。枚举元素必须位于枚举类体中的最开始部分,枚举元素后要有分号与其他成员分隔。枚举类的构造方法的权限修饰符默认是private;一旦枚举对象后面加上{},那么该对象实际是枚举匿名内部