Refreshing the panel

Predikshon
Predikshon used Ask the Experts™
on
i have a simple paint component method to display an image middle of the frame.
public void paintComponent(Graphics g)
{
   super.paintComponent(g);
   g.drawImage(img.getImage(),(this.getWidth()-img.getIconWidth())/2,(this.getHeight() - img.getIconHeight())/2,null);
}
This method is called when a menu item is clicked and drawn into a panel. The picture doesnt appear immediately when the menu option is clicked. It appears only when i resize the frame. The other problem is that when i resize the frame the picture gets displayed in the new center with the old one still remaining. How do i remove the old one and make the picture appear when i click the menu option itself.

                   
                     
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
You should not call paintComponent() directly.
can u post the code that is invoked when the menu item is pressed?

Author

Commented:
In the JFrame constructor i create the menu "view" and add the item "center" in it,
then i add an ActionListener,
centerItem.addActionListener(this);

overriding the actionPerformed i did this,


if (source == centerItem)
{
  JPanel panel = new JPanel(){
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    g.drawImage(img.getImage(),(this.getWidth()-img.getIconWidth())/2,(this.getHeight() - img.getIconHeight())/2,null);
      };
  }
  panel.setOpaque(false);
  this.setContentPane(panel3);
}

i'm implementing the actionListener() and this refers to the frame.

Author

Commented:
sorry its,
this.setContentPane(panel);
in the last line

cheers
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Mick BarryJava Developer
Top Expert 2010

Commented:
Try this:

...
this.setContentPane(panel3);
this.revalidate();
}

Author

Commented:
the revalidate method helped me in displaying the image when i clicked on the menu item. thx. but still the image doesnt go when i resize, the old image in the old position is still there. I need the old image to go and the new position in its new center.

cheers
Mick BarryJava Developer
Top Expert 2010

Commented:
try clearing the background of your panel when it repaints.

 public void paintComponent(Graphics g)
 {
   super.paintComponent(g);
   g.setColor(getBackground());
   g.fillRect(0, 0, getWidth(), getHeight());
   g.drawImage(img.getImage(),(this.getWidth()-img.getIconWidth())/2,(this.getHeight() - img.getIconHeight())/2,null);
     };
 }

Author

Commented:
thanks a lot man it works..:-)
Java Developer
Top Expert 2010
Commented:
If you are happy that my comments have answered your question then please accept my comment as an answer.
(I see you are new at ee).
Mick BarryJava Developer
Top Expert 2010

Commented:
Thank you :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial