Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
kotlinとJavaFXでバウンスボックスとカウンター表示をするサンプル01
// http://krr.blog.shinobi.jp/javafx/javafx%203d%20%E5%BA%A7%E6%A8%99%E5%A4%89%E6%8F%9B%EF%BC%88%E5%B9%B3%E8%A1%8C%E7%A7%BB%E5%8B%95%E3%83%BB%E5%9B%9E%E8%BB%A2%E3%83%BB%E6%8B%A1%E7%B8%AE%EF%BC%89
// https://gamedevelopment.tutsplus.com/ja/tutorials/introduction-to-javafx-for-game-development--cms-23835
import javafx.animation.AnimationTimer
import javafx.application.Application
import javafx.geometry.Point2D
import javafx.scene.Group
import javafx.scene.Scene
import javafx.scene.canvas.Canvas
import javafx.scene.canvas.GraphicsContext
import javafx.scene.paint.Color
import javafx.scene.text.Font
import javafx.stage.Stage
fun random(min: Double = 0.0, max: Double = 1.1) = (Math.random() * (max - min + 1)) + min
fun random(min: Int = 0, max: Int = 1) = ((Math.random() * (max - min + 1)) + min).toInt()
fun randomASCII(upper: Boolean = true) = (if (upper) random(65, 90) else random(97, 122)).toChar()
fun randomString(n: Int = 5) = (1..n).map { randomASCII() }.joinToString(separator = "")
const val answer = "CAT"
var x = 0.0
var vx = 5.0
var y = 0.0
var vy = 4.0
var count = 0
var foundFlag = false
var degrees = 0.0
fun draw(ctx:GraphicsContext) {
ctx.fill = Color(1.0, 1.0, 1.0, 0.9)
//ctx.clearRect(0.0,0.0, ctx.canvas.width, ctx.canvas.height)
ctx.fillRect(0.0,0.0, ctx.canvas.width, ctx.canvas.height)
ctx.font = Font("Meiryo", 30.0)
ctx.fill = Color.BLACK
ctx.fillText("こんにちは二重世界!!${count++}", 60.0, 60.0)
x += vx
vx *= (if (0 <= x && x <= ctx.canvas.width-50.0) 1 else -1)
y += vy
vy *= (if (0 <= y && y <= ctx.canvas.height-50.0) 1 else -1)
ctx.fill = Color.BLUE
ctx.fillRect(x, y, 50.0, 50.0)
if (foundFlag) {
ctx.fill = Color.BLACK
ctx.fillText("FOUND : $answer", 60.0, 90.0)
} else {
val s = randomString(3)
val msg = if (s == answer) "FOUND : $s" else "NOT FOUND: $s"
ctx.fill = Color.BLACK
ctx.fillText("$msg", 60.0, 90.0)
if (s == answer) {
foundFlag = true
}
}
ctx.save()
//ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2) // move to origin.
ctx.translate(0.0, 0.0) // 原点(origin)を移動。
ctx.scale(1.0,1.0)
ctx.rotate(degrees)
ctx.translate(-50.0, -50.0) // 高さと幅の半分を戻す。
degrees++;
//ctx.translate(-100.0, -50.0)
ctx.fill = Color.RED
ctx.fillRect(0.0, 0.0, 100.0, 100.0)
ctx.restore()
val kr = KRect(ctx.canvas.width/2, ctx.canvas.height/2, 100.0, 100.0)
kr.scale = Point2D(2.0, 2.0)
kr.degrees = degrees
kr.display(ctx)
}
class KRect(var x: Double = 0.0, var y: Double = 0.0, var w: Double = 0.0, var h: Double = 0.0) {
var degrees: Double = 0.0
var scale: Point2D = Point2D(1.0, 1.0)
var color: Color = Color.BLACK
fun display(ctx: GraphicsContext): KRect {
ctx.save()
//ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2) // move to origin.
ctx.translate(x, y) // 原点(origin)を移動。
ctx.scale(scale.x, scale.y)
ctx.rotate(degrees)
ctx.translate(-w/2, -h/2) // 高さと幅の半分を戻す。
//ctx.translate(-100.0, -50.0)
ctx.fill = color
ctx.fillRect(0.0, 0.0, w, h)
ctx.restore()
return this
}
}
class App1 : Application() {
lateinit var canvas: Canvas
lateinit var ctx: GraphicsContext
override fun start(primaryStage: Stage) {
canvas = Canvas(640.0, 480.0)
ctx = canvas.graphicsContext2D
val root = Group()
root.children.add(canvas)
// 無名クラスは、Kotlinではオブジェクト式を使う
object : AnimationTimer() {
override fun handle(now: Long) {
draw(ctx)
}
}.start()
primaryStage.title = "KotlinでJavaFX"
primaryStage.scene = Scene(root, canvas.width, canvas.height)
primaryStage.show()
}
// コンパニオンオブジェクトと@JvmStaticで、staticメソッドを作成
companion object {
@JvmStatic
fun main(args: Array<String>) {
Application.launch(App1::class.java, *args) // *で可変長配列に変換
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment