2

i wanted to ask, if somebody might have a solution about a problem i face. I am working at an application, which draws an animation - for instance a map with objects moving onto. My problem is, that on top of the drawing, a Jtable, Jlist as well as other Components are also placed.

In my particular example all of those components have been added to the Panel, which holds the map. In result each component gets redrawn as often as good my fps is. Therefore making one of the tables invisible reduces the already high cpu usage of sometimes around 50% to less than 30%.

My question is, how can i avoid calling somewhat static visual contents paintComponent() method, without having the "background" - the map - whited out the menu.

Since the animation redraws permanently the menu is not shown at all, if its separated from the corresponding JPanel.

First thoughts move into following directions:

Clipping - actually not as good as i would like to, since id like to enable moving around the menus. JLayeredPane - already tried but seemed to turn out, that the paintComponent method of menus still gets called frequently. JWindow/Internal Frame - had that thought a couple of minutes ago. Having a complete independent container shall be able to handle my regard, or?

I am looking forward, if somebody has an elegant idea, how to fix that and reduce the cpu usage significantly.

Thanks!! Best regards.

2 Answers2

3

For painting over JComponent(s) placed into JPanel you have look at

  1. JLayer (Java7) based on JXLayer(Java6)

  2. GlassPane, notice all JComponents must be lightweight, otherwise is GlassPane behind heavyweight (J)Components

  3. is possible painting to the JViewport,

EDIT

  1. you have to use Swing Timer for moving with Icon (in the JLabel) placed into JXLayer, GlassPane or JViewport, don't use Runnable#Thread and never, not by using plain Thread.sleep(int)
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
3

I would create a custom Shape for clip. Use Area class and subtract from the Area all the children components' bounds.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Hm the problem seems to be, that since any of these components are layed into the content pane of the applet, all paintComponent() methods become called. Therefore having a clipping area to avoid drawing "behind" these components seems to be only the half of it. I have also have given a try to GlassPane but actually also using the different levels to make sure, which component is drawn in front of the others, results in having still a paintComponent call on any in the glasspane added components. – user1514195 Sep 04 '12 at 08:22
  • Well - since the map has been extending from a couple of components, i had to set the clipping at a proper point. Therefore the redraw of the map does not happen on a given area. For some reason - one of my menu components is drawn once or twice - as id liked to, but where the menu is meant to be, is just a grey area. Even nothing has been drawn onto that part of the applet, except the initial draw of the menu. – user1514195 Sep 04 '12 at 08:36