枚举类中有抽象方法、有构造函数,每个值继承该方法实现自己的业务行为逻辑。 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}