It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Swing: Slow paint Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing: Slow paint" Watch "Swing: Slow paint" New topic
Author

Swing: Slow paint

Janus Engstrøm
Ranch Hand

Joined: Nov 01, 2006
Posts: 44
Hi Ranchers,

I'm creating an application where I need "Snap-component-to-grid" functionality. To show the grid and paint the component that needs to be snapped to the grid, I'm using a coding structure as below:



When showGrid = true the repainting of the JPanel is really slow! How do I increase the speed of the repainting (decrease the CPU load) ?


Thanks in advance,

Janus
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Janus Engstr�m:




When showGrid = true the repainting of the JPanel is really slow! How do I increase the speed of the repainting (decrease the CPU load) ?


You are drawing the entire grid every time the mouse moves.
There are ways not to do that. I would start by replacing

mouseLocation = e.getPoint();
repaint();


with

repaint(mouseLocation.x, mouseLocation.y, 51, 51);
mouseLocation = e.getPoint();
repaint(mouseLocation.x, mouseLocation.y, 51, 51);


This doesn't reduce the number of calls to drawLine() but
it should run much faster anyway because of clipping and
such. It may be good enough for your purposes.


bitguru blog
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
mouseMoved is a busy method and trying to get it to do too much can bog
things down. I can think of three ways to reduce the workload in your app.
Two of these are traditional:
1 — limited redrawing as shown by Brian, and
2 — drawing the grid on an image which you draw as a background.
Janus Engstrøm
Ranch Hand

Joined: Nov 01, 2006
Posts: 44
First of all thank you very much for your responses! I haven't been using this forum that much, but it seems as if people in here are quite pleasant company Too, I've been trying to help out other users in dire need of guidance, but I seem to stumble across questions out of my area of knowledge or I'm too late with my reply, someone else has already helped the poor fellow out. Hopefully some day I can give back to the forum what it (or it's users to be more precise) has given me.


Back to Topic!

To Brian Cole: I fully understand your idea, but unfortunately by limiting the scope of the repainting, I get trailing debris from the Box the pointer is carrying around. Too, in the "real" application this object can be of various sizes and include alignment lines crossing from one side of the screen to the other. So to make your solution work in all situations, the area needed to be repainted is in worst case scenario:






To Craig Wood: This is EXCELLENT! Thank you so very much! I had no idea that images are updated that fast! I understand that drawing the grid using drawLine() is quite a lot of painting, and I was looking for an idea that made the Graphics object capable of only updating the drawLine()'s needed. Your solution does (afaik) exactly that: Only repaints the areas of the image that are smothered by debris from moving the pigs skin colored box around.

I'll implement your solution into the "real" application tomorrow, I hope it'll work as much as a charm as it did in my mock-up example.


Thanks to both of you!


Regards,

Janus
 
jQuery in Action, 2nd edition
 
subject: Swing: Slow paint
 
Similar Threads
JTextField/JPanel with round edged corners
a Jbutton with a rounded border
Graphics and JVMs
checkbox problem
Creating Arrow Icon Help