java 画一个树,画一颗树丶Java教程网-IT开发者们的技术天堂

b34ab0ad445536d0fe4728c470f1c772.png

class RecursiveCallTree : Fragment() {

override val root = group {

val root1 = this

val start = Point(400.0, 600.0)

val level= intProperty()

val levelList= listOf(1,3,5,10,15,20)

hbox {

button("draw tree") {

action {

run {

drawTree(root1, start, 90.0, 10)

}

}

}

combobox(level,levelList)

}

}

fun drawTree(group: Group, p0: Point, angle: Double, level: Int) {

if (level <= 0) {

return

}

val destination = p0.getDestination(10F, angle, level)

val line = Line(p0.x, p0.y, destination.x, destination.y)

line.setStrokeWidth(level.toDouble())

if (level > 7) {

line.setStroke(Color.BROWN)

} else if (level > 3) {

line.setStroke(Color.GREEN)

} else {

line.setStroke(Color.LIMEGREEN)

}

group.add(line)

drawTree(group, destination, angle - 20.0 - Math.random() * 10, level - 1)

drawTree(group, destination, angle - 5, level - 1)

drawTree(group, destination, angle + 20.0 + Math.random() * 20, level - 1)

}

}

class Point(var x: Double=0.0,var y: Double=0.0) {

fun getDestination(distance: Float, angle: Double, level: Int): Point {

val p = Point()

p.x = x + Math.cos(Math.toRadians(angle)) * level.toDouble() * distance.toDouble()

p.y = y - Math.sin(Math.toRadians(angle)) * level.toDouble() * distance.toDouble()

return p

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值