jvmMain.graphics.AwtGraphics.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of konapi-jvm Show documentation
Show all versions of konapi-jvm Show documentation
Kotlin Native for Raspberry Pi
The newest version!
@file:OptIn(ExperimentalUnsignedTypes::class)
package ch.softappeal.konapi.graphics
import ch.softappeal.konapi.tryFinally
import java.awt.BorderLayout
import java.awt.Canvas
import java.awt.Color
import java.awt.Frame
import java.awt.Graphics
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import ch.softappeal.konapi.graphics.Graphics as KGraphics
public fun drawImage(width: Int, height: Int, draw: Graphics.(image: BufferedImage) -> R): R {
val image = BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY)
val graphics = image.createGraphics()
return tryFinally({
graphics.draw(image)
}) {
graphics.dispose()
}
}
public class AwtGraphics internal constructor(private val zoom: Int, display: Display) : KGraphics(display) {
init {
require(zoom > 0) { "zoom=$zoom must be > 0" }
}
override val buffer: UByteArray = UByteArray(width * height * 3)
private fun index(x: Int, y: Int) = (x + y * width) * 3
override fun setColorImpl() {}
override fun setPixelImpl(x: Int, y: Int) {
val b = index(x, y)
buffer[b] = color.red.toUByte()
buffer[b + 1] = color.green.toUByte()
buffer[b + 2] = color.blue.toUByte()
}
private fun Graphics.draw() {
for (x in 0..
draw()
ImageIO.write(image, "png", File(path))
}
}
}
public fun AwtGraphics(zoom: Int, dimension: Dimension): AwtGraphics =
AwtGraphics(zoom, object : Display(dimension.width, dimension.height) {
override fun update(buffer: UByteArray) {}
})