Link to home
Start Free TrialLog in
Avatar of dex11
dex11

asked on

stop from re painting the Panel.

hi experts,
                I created a java Frame application .Frame has a panel in it and when ever when i minimize the window or focus is lost on the frame it keeps going back to paintComponent(Graphics g) method and repaints the whole panel is there a way to stop this as i dont want it to repaint as it creates something that i dont want to do.

Thanks in advance.
dex11


      
Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

If you want paintComponent() method to be executed only once you could do this by using one additional boolean variable:

...

private boolean alreadyPainted;

public void paintComponent(Graphics g) {
      if (!alreadyPainted) {
             alreadyPainted = true;
             // code for painting...
      }
}

...
Avatar of Mick Barry
no sorry thats how it works.
And if you don't repaint it then your panel will be blank :)

paintComponent() should paint always paint your component as you want it to appear.
Yes, unfortunately, it will be blank. :-)

You can override update method:

public void update(Graphics g) {
      paint(g);
}

However, if you minimize or resize window everything that was painted will be lost, so this isn't what you need, either.

================================

Only solution as objects said is to make sure your paintComponent() method do always the same thing. If all external parameters that paint method uses for drawing stay the same then it will always draw the same thing. If this is not the case, you can buffer somewhere what you've painted first time and then draw it again every time paintComponent() method is called.
>>... as it creates something that i dont want to do.

What do you mean by this?

Avatar of dex11
dex11

ASKER

What i have written in paintcomponent() is to shuffle a group of numbers and throw on the panel in a grid fashion.Everytime i minimize it or when focus is lost and  gained it would  reshuffle the numbers again and draw a completely different version than the prior one(panel before minimize) .So that is what i meant when i said "creates something that i dont want".Is there a way to stop this?Also  i am also using repaint method at different points so I dont want to use something like

public void paintComponent(Graphics g) {
      if (!alreadyPainted) {
             alreadyPainted = true;
             // code for painting...
      }
}

that would stop repaint method i.e. reshuffle numbers and create number grid when i want it.The problem here is it does it everytime i minmize or focus lost and gained.

thanks.
It seems to me that you need to cache the data and simply control when those data change. paintComponent would simply represent the cache, which would change only when you tell it to
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
UrosVidojevic, can you tell me please how the accepted answer differs from what i'd already suggested?
Hi CEHJ,

I'm not the author of the question, so I didn't accept solution :-).
Maybe you have confused me with author of this question?
>>I'm not the author of the question, so I didn't accept solution :-).

Sorry UrosVidojevic - i blame the poor layout, where the most important name, i.e. the name of the questioner in the very first posting gets one of the least prominent styles and is badly positioned ;-)

So, dex11, can you tell me please how the accepted answer differs from what i'd already suggested?