Predikshon
asked on
Refreshing the panel
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.get Width()-im g.getIconW idth())/2, (this.getH eight() - img.getIconHeight())/2,nul l);
}
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.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img.getImage()
}
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.
ASKER
In the JFrame constructor i create the menu "view" and add the item "center" in it,
then i add an ActionListener,
centerItem.addActionListen er(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.get Width()-im g.getIconW idth())/2, (this.getH eight() - img.getIconHeight())/2,nul l);
};
}
panel.setOpaque(false);
this.setContentPane(panel3 );
}
i'm implementing the actionListener() and this refers to the frame.
then i add an ActionListener,
centerItem.addActionListen
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()
};
}
panel.setOpaque(false);
this.setContentPane(panel3
}
i'm implementing the actionListener() and this refers to the frame.
ASKER
sorry its,
this.setContentPane(panel) ;
in the last line
cheers
this.setContentPane(panel)
in the last line
cheers
Try this:
...
this.setContentPane(panel3 );
this.revalidate();
}
...
this.setContentPane(panel3
this.revalidate();
}
ASKER
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
cheers
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.get Width()-im g.getIconW idth())/2, (this.getH eight() - img.getIconHeight())/2,nul l);
};
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(getBackground()
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(img.getImage()
};
}
ASKER
thanks a lot man it works..:-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thank you :-)
can u post the code that is invoked when the menu item is pressed?