如何在Kotlin中使用`paint()`绘制2D图形。

huangapple go评论69阅读模式
英文:

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...
    }
}

huangapple
  • 本文由 发表于 2023年6月26日 01:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定