Repainting a JPanel
I'm updating some code that extends a Canvas (myCanvas) which is used for drawing. Unfortunately, the Canvas flickers so I'd like to change it.
I've tried changing the myCanvas so that it extends a JPanel. This removes the flickering however if I move the Frame containing the JPanel behind another window and then show it, the JPanel doesn't get redrawn.
I've placed the Canvas' drawing code in a paintComponent method.
How to I get the JPanel to be redrawn when it's brought to the front?
Cheers,
Chris
[548 byte] By [
cpsmusica] at [2007-11-27 10:45:46]

# 1
> I'm updating some code that extends a Canvas
> (myCanvas) which is used for drawing. Unfortunately,
> the Canvas flickers so I'd like to change it.
it flickers because you didn't override the repaint method.
overriding the repaint method:
public void repaint(){
try{
Image img = this.createImage(getSize().width , getSize().height);
Graphics g = img.getGraphics();
g.setColor(this.getBackground());
g.fillRect(0, 0, getSize().width, getSize().height);
g.setColor(this.getForeground());
paint(g);
this.getGraphics().drawImage(img, 0, 0, null);
}catch(NullPointerException e){}
}
# 2
Wouldn't it be easier to use a JPanel which is double-buffered?
# 3
> Wouldn't it be easier to use a JPanel which is
> double-buffered?
actually it's your choice, I'm just explaining to you why the canvass flickers.
did you try to use repaint in the jpanel?
# 4
I understand why the Canvas is flickering.
What I don't get is why the JPanel is not getting redrawn when it moves to the front.
# 5
> What I don't get is why the JPanel is not getting redrawn when it moves to the front.
It does get redrawn. You have a bug in your code.
If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",
see http://homepage1.nifty.com/algafield/sscce.html,
that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the "Code Formatting Tags",
see http://forum.java.sun.com/help.jspa?sec=formatting,
so the posted code retains its original formatting.
# 6
I've decided to have a go at using the traditional method of drawing to an offscreen buffer first.
I can't get this to draw without flickering either, however as I'm not using a Swing component anymore I'll move my questions over to the AWT forum.
Yes, I'll post some code too.
Cheers,
Chris
# 7
> I've decided to have a go at using the traditional method of drawing to an offscreen buffer first.
Who says its traditional. Maybe that was the way AWT worked.
This is Swing and Swing is double buffered by default so there should be no need to use an offscreen buffer. Do you think Swing would use an old approach or a new approach?
# 8
you we're ask to post your code so that we can help you with you JPanel problem but you didn't.
>I've decided to have a go at using the traditional method of drawing to an offscreen buffer >first.
Tired of figuring out with the JPanel problem?
# 9
Yes! I thought I'd try drawing to an offscreen buffer in the Canvas, rather than changing to a JPanel.
# 10
I think I've found the problem.
The JPanel that wasn't being repainted was a chile of another JPanel. The parent JPanel was still using paint rather than paintComponent. I guess that this means that even though the parent JPanel was being painted, the message wasn't being sent to the child.
Thanks to all who helped with this problem.
Cheers,
Chris