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.
Mouse events are not handled by default in Scala Swing for performance reasons. In your case you need to add a
listenTo(mouse.clicks)
to your object
but there is also an event publisher mouse.moves
you can listen to if you need to track the mouse move events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With