wood burning stoves*
The moose likes Performance and the fly likes MouseMotionEvent Performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "MouseMotionEvent Performance" Watch "MouseMotionEvent Performance" New topic
Author

MouseMotionEvent Performance

Daniel Croft
Greenhorn

Joined: Feb 16, 2012
Posts: 21

So I'm writing a turn based game that involves scrolling a map made of squares which light up as you mouse over them. Everything is drawn on a frame with a 2 buffer strategy. The rendering is done in a separate thread that loops continuously. When I sleep the render thread for a few milliseconds after each frame to push the fps down to around 30, the squares light up very responsively but the scrolling map jitters pretty bad. If I don't sleep the thread, it sits at around 60 fps and the map scrolls really smoothly, but there is noticeable delay between the mouse passing over a square and it lighting up. The code for identifying which square to light is very simple and quick and the processor doesn't seem taxed, so I don't think that the render thread is slowing things down too much. I'm a little stumped as to what is causing the delay, maybe the mouseEvents aren't spawning fast enough? Any ideas would be greatly appreciated as I'm pretty stumped.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3617
    
  14

Remove the sleep(), and try adding a yield() to the rendering thread. This is *not* a permanent solution, but it may give you an idea of what the problem might be. If yield() solves the problem, then it means your program might be suffering from starvation. The rendering thread is hogging the processor and it's not pausing enough to let the event dispatch thread do its work as well.

Best would be if you could post an SSCCE.
Daniel Croft
Greenhorn

Joined: Feb 16, 2012
Posts: 21

I've tried using Thread.yield(), but that seems to have no effect. I get pretty good results using a sleep of around 14 milliseconds; that seems to keep the fps at 60 and keeps the mouse responsive. It's kind of weird that completely removing the sleep doesn't result in any fps gains though. Also, I'm sorry but I don't know what an SSCCE is :/
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2347
    
  28

Short, Self Contained, Correct (Compilable), Example

Click the link in Stephan's post

Daniel Croft
Greenhorn

Joined: Feb 16, 2012
Posts: 21

Doh! I didn't realize that was a link. I'll try to trim it down somewhat but it might take a while, it's pretty involved already.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2347
    
  28

I think you should do it anyways, not just for us. It will help you analyze non-trivial problems. Sometimes, with complicated problems, you cannot replicate the problem with the SSCCE. In this case, you start looking at the differrences between your code and SSCCE to figure out where it's going wrong. On the other hand, if the problem does happen with the SSCCE, atleast you have a simpler version of the actual code that you can do trial and error with. You can try different solutions to fix the SSCCE to find one that works, and then apply only the actual fix to the actual source.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: MouseMotionEvent Performance