gpt4 book ai didi

events - 扩展面板中的 Scala Swing react

转载 作者:行者123 更新时间:2023-12-03 01:57:11 25 4
gpt4 key购买 nike

好吧,简单的问题:我有一个扩展 scala.swing.Panel 的单例对象,我想让它对简单的鼠标单击使用react。但是……好吧,这不起作用。由于 Scala 是一门新语言,因此查找特定问题的信息并不那么容易。也许你可以帮忙:

import scala.swing._
import scala.swing.event._
import java.awt.{Graphics2D, Color}

object GamePanel extends Panel {
val map: TileMap = new TileMap(10, 10)({
(x, y) =>
if (x == y) new Wood
else if (x == 5) new Water
else new Grass
})

reactions += {
case MouseClicked(src, pt, mod, clicks, pops) => {
selectedTile = (pt.x / map.tw, pt.y / map.th)
println("Clicked")
repaint
}
}

var selectedTile = (0, 0)

override def paint(g: Graphics2D) = {
map.draw(g)
g.setColor(Color.red)
g.drawRect(selectedTile._1 - 1, selectedTile._2 - 1, 33, 33)
}
}

感谢您的聆听。

最佳答案

出于性能原因,Scala Swing 中默认不处理鼠标事件。根据您的情况,您需要添加

listenTo(mouse.clicks)

到您的对象,但如果您需要跟踪鼠标移动事件,您还可以监听一个事件发布者mouse.moves

关于events - 扩展面板中的 Scala Swing react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4110748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com