• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing: Slow paint

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic