I recently discovered, that my scala swing applications behave very strangely. The following test application draws a line which moves over the screen. When I ran the program the first time I was shocked on how laggy swing seems to be, since the line doesnt move smoothely in the least. BUT as soon as swing seems to recognize events, triggered by for example a mouse-hover or pressed keys, everything seems to run smoothly and as expected. However as soon as the mouse leaves the swing window,or no more keys are pressed swing is laggy again. I am not working on a slow machine and since I dont have similar problems with python or so I still think that the scala.swing library seems to very bad, to me. Is there a major mistake I am not seeing here? Am I using scala.swing incorrectly? What is it, that makes scala.swing so laggy but only ever when there are no events triggered by the user?
Here is the very small test app I have created. Please copy it and try it yourself.
object PerformanceTest {
def main(args:Array[String]): Unit ={
var i =0
val (x1, y1) = (0,0)
val (x2, y2) = (400,300)
val frame = new MainFrame{
title = "performance test"
centerOnScreen()
contents = new BorderPanel{
layout += new Panel{
override def paint(g:Graphics2D): Unit ={
g.drawLine(x1+i, y1,x2-i, y2)
}
} -> BorderPanel.Position.Center
listenTo()
}
size = new Dimension(400, 300)
visible = true
}
while(true){
frame.repaint()
i += 1
Thread.sleep(20)
}
}
}