英文:
How to use graphics 2d in kotlin with paint()
问题
我想开始制作一个2D视频游戏,我在Java中找到了一个教程。然而,我想使用Kotlin,因为我听说它更好,我对Java和Kotlin编程都很新,我需要将这个代码放入:
```kotlin
Graphics2d g2 = Graphics2D(g)
在Kotlin中,我没有找到任何方法通过搜索。
如果你想知道(但我认为这并不重要)JPanel类文件在这里:
import java.awt.Color
import java.awt.Dimension
import java.awt.Graphics
import java.awt.Graphics2D
import javax.swing.JPanel
class GamePanel : JPanel(), Runnable{
    //屏幕设置
    var originalTileSize = 16
    var scale = 3
    var tileSize = originalTileSize * scale
    var maxScreenCol = 16
    var maxScreenRow = 12
    var gameThread = Thread()
    var screenWidth = maxScreenCol * tileSize // 768
    var screenHeight = maxScreenRow * tileSize // 576 TO CHANGE????
    init {
        this.setPreferredSize(Dimension(screenWidth, screenHeight))
        this.setBackground(Color.BLACK)
        this.setDoubleBuffered(true)
    }
    // 游戏线程代码
    public fun startGameThread(){
        gameThread = Thread(this)
        gameThread.start()
    }
    // 运行游戏循环
    public override fun run(){
        while (gameThread != null){
            update()
            repaint()
        }
    }
    public fun update(){
        // 现在没有任何内容
    }
    public override fun paintComponent(g : Graphics) {
        super.paintComponent(g)
        val g2: Graphics2D = g as Graphics2D
    }
}
英文:
I want to start making a 2d video game, and I found a tutorial in java. However I wanted to use kotlin because I heard it is better, I am new to java and kotlin programming and I needed to put this
Graphics2d g2 = Graphics2D(g)
In kotlin, I didn't find anyway by searching.
If you want to know(but I think it doesn't matter) The JPanel class file is here:
import java.awt.Color
import java.awt.Dimension
import java.awt.Graphics
import java.awt.Graphics2D
import javax.swing.JPanel
class GamePanel : JPanel(), Runnable{
    //Screen settings
    var originalTileSize = 16
    var scale = 3
    var tileSize = originalTileSize * scale
    var maxScreenCol = 16
    var maxScreenRow = 12
    var gameThread = Thread()
    var screenWidth = maxScreenCol * tileSize // 768
    var screenHeight = maxScreenRow * tileSize // 576 TO CHANGE????
    init {
        this.setPreferredSize(Dimension(screenWidth, screenHeight))
        this.setBackground(Color.BLACK)
        this.setDoubleBuffered(true)
    }
    // Game Thread code
    public fun startGameThread(){
        gameThread = Thread(this)
        gameThread.start()
    }
    // run the game loop
    public override fun run(){
        while (gameThread != null){
            update()
            repaint()
        }
    }
    public fun update(){
        // Nothing for now
    }
    public override fun paintComponent(g : Graphics) {
        super.paintComponent(g)
        Graphics2D g2 = Graphics2D(g)
    }
}
</details>
# 答案1
**得分**: 1
在Kotlin中,使用`as`关键字执行类型转换,如下所示:
```kotlin
val g2 = g as Graphics2D
您还可以使用安全转换操作符as?,如果转换不可行,它会返回null,而不是抛出异常:
override fun paintComponent(g: Graphics) {
    super.paintComponent(g)
    val g2 = g as? Graphics2D
    if (g2 != null) {
        // 根据需要使用g2...
    }
}
英文:
In Kotlin casting is performed using the as keyword, like:
val g2 = g as Graphics2D
You may also use a safe cast with as?, that returns null if the casting is not possible, instead of throwing an exception:
override fun paintComponent(g: Graphics) {
    super.paintComponent(g)
    val g2 = g as? Graphics2D
    if (g2 != null) {
        // Use g2 as needed...
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论