Repainting Panels When Necessary

R

Raous

Hello,
I created a program which draws elipses on some panels.

When I activate another window such as a Web Browser and
return(re-activate) to my program,
the graphic objects of mine remain disappeared.

However, I found the other objects such as JButton and JLabel are
recovered and shown.

I am finding an event like WM_PAINT (in Visual C++), which occured when
the object is needed to refresh.

Although I put command such as validate() / repaint() in
"ComponentShown" of each Panel and its parent Pane, and in
"AncestorShown", I couldn't find appropriate command and position yet.

Could anyone help me?
 
N

Norb

In which method are you doing your painting?

If you use Swing, put your painting code in paintComponent, and you
don't have to care about repainting when the window is hidden,
minimized or whatever. Just call repaint() when your data changes.

Hope that helps
Norbert
 
Z

zero

Hello,
I created a program which draws elipses on some panels.

When I activate another window such as a Web Browser and
return(re-activate) to my program,
the graphic objects of mine remain disappeared.

However, I found the other objects such as JButton and JLabel are
recovered and shown.

I am finding an event like WM_PAINT (in Visual C++), which occured when
the object is needed to refresh.

Although I put command such as validate() / repaint() in
"ComponentShown" of each Panel and its parent Pane, and in
"AncestorShown", I couldn't find appropriate command and position yet.

Could anyone help me?

You don't need to bother with componentShown, unless you want to do
something funky with your component (as a stupid example, if you want to
play a sound file every time a component is shown, you will need to
implement ComponentListener). Failing that, just trust java to call the
correct method to paint your component.

For example, you can write a simple subclass of JPanel that will draw an
oval on itself like this:

class OvalPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawOval(10, 10, getWidht()-10, getHeight()-10);
}
}

If you're using awt, the above code becomes

class OvalPanel extends Panel
{
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.BLUE);
g.drawOval(10, 10, getWidht()-10, getHeight()-10);
}
}

As you can see, you don't need to call repaint() or bother with the
ComponentListener interface. You only need repaint() if you want to
change what is drawn on the panel.

Also, I noticed you said "each Panel", while you were talking about
JButton and JLabel earlier. Panel is an awt component, while JButton and
JLabel are swing. While it is theoretically perfectly ok to mix swing
and awt, it's usually not a great idea. So instead of Panel, use JPanel.

As an aside, WM_PAINT is part of the Windows API, and not specifically
for VC++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,800
Messages
2,569,657
Members
45,416
Latest member
MyraTrotte
Top