Help us understand the problem. What is going on with this article?

kotlinでGUI

More than 3 years have passed since last update.

kotlinでawtとswingを使った描画

まずはAWTで線の出力

line.kt
import java.awt.*

internal class MyCanvas : Canvas() {
    override fun paint(g: Graphics) {
        g.drawLine(1, 1, 500, 500)
    }
}
internal class LineTest : Frame("LineTest") {
    init {
        setSize(500, 500)
        layout = BorderLayout()
        val mc1 = MyCanvas()
        add(mc1, BorderLayout.CENTER)
        show()
    }
    companion object {
        @JvmStatic fun main(args: Array<String>) {
            LineTest()
        }
    }
}

次にswingで線の描写

linetest.kt
import java.*
import java.awt.*
import javax.*
import javax.swing.*

internal class MCanvas : Canvas() {
    override fun paint(g: Graphics) {
        g.drawLine(1, 1, 500, 500)
    }
}
internal class JLineTest : JFrame("Jlinetest") {
    init {
        setSize(500, 500)
        layout = BorderLayout()
        defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        val mc = MCanvas()
        add(mc, BorderLayout.CENTER)
        show()
    }

    companion object {
        @JvmStatic fun main(args: Array<String>) {
            JLineTest()
        }
    }
}

スクリーンショット (160).png

swingはAWTをベースに、強化されたものらしい
IntelliJ IDEA を使えば簡単に画像を表示できる。

nimani76
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away