扩展面板中的 Scala Swing 反应
好吧,简单的问题:我有一个扩展 scala.swing.Panel 的单例对象,我想让它对简单的鼠标单击做出反应。但是……好吧,这不起作用。由于 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)
}
}
感谢您的聆听。
Well, simple question: I have a singleton object that extends scala.swing.Panel
, and I want to have it react on a simple mouse click. But... well, it doesn't work. Since Scala's such a new language, finding infos to specific problems is not so easy. Maybe you can help:
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)
}
}
Thanks for listening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出于性能原因,Scala Swing 中默认不处理鼠标事件。
在您的情况下,您需要
向您的
object
添加 a ,但如果您需要跟踪鼠标移动事件,您还可以监听一个事件发布者mouse.moves
。Mouse events are not handled by default in Scala Swing for performance reasons.
In your case you need to add a
to your
object
but there is also an event publishermouse.moves
you can listen to if you need to track the mouse move events.