how to clear the image from jpanel

J

jimgardener

I have an ImagePanel subclassed from JPanel ,where I can set an image

class ImagePanel extends JPanel{
private BufferedImage bi;
public ImagePanel(){
super();
}
public void setImage(String imagefilename) throws IOException{
bi = ImageIO.read(new File(imagefilename));
this.setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));

this.revalidate();
this.repaint();
}
@Override
public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.drawImage(bi,0,0,null);
}
}

I can set the image by giving an image filename.But I wish to clear
the previous image from the panel,thus showing a blank image panel,if
a user supplies an invalid image name.
How can I do this?Can someone please tell me?
thanks

jim
 
K

Knute Johnson

I have an ImagePanel subclassed from JPanel ,where I can set an image

class ImagePanel extends JPanel{
private BufferedImage bi;
public ImagePanel(){
super();
}
public void setImage(String imagefilename) throws IOException{
bi = ImageIO.read(new File(imagefilename));
this.setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));

this.revalidate();
this.repaint();
}
@Override
public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.drawImage(bi,0,0,null);
}
}

I can set the image by giving an image filename.But I wish to clear
the previous image from the panel,thus showing a blank image panel,if
a user supplies an invalid image name.
How can I do this?Can someone please tell me?
thanks

jim

If no image gets loaded, set the reference to null and test for that in
the paintComponent() method and don't draw the image just clear the
background.
 
J

John B. Matthews

Knute Johnson said:
I have an ImagePanel subclassed from JPanel ,where I can set an image

class ImagePanel extends JPanel{
private BufferedImage bi;
public ImagePanel(){
super();
}
public void setImage(String imagefilename) throws IOException{
bi = ImageIO.read(new File(imagefilename));
this.setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));

this.revalidate();
this.repaint();
}
@Override
public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.drawImage(bi,0,0,null);
}
}

I can set the image by giving an image filename.But I wish to clear
the previous image from the panel,thus showing a blank image
panel,if a user supplies an invalid image name. How can I do
this?Can someone please tell me?
[...]

If no image gets loaded, set the reference to null and test for that
in the paintComponent() method and don't draw the image just clear
the background.

Knute's right; and if you forget the test, as I have sometimes done, the
drawImage() method "does nothing if img is null."

<http://download.oracle.com/javase/6/docs/api/java/awt/Graphics.html>
 
K

Knute Johnson

Knute Johnson said:
I have an ImagePanel subclassed from JPanel ,where I can set an image

class ImagePanel extends JPanel{
private BufferedImage bi;
public ImagePanel(){
super();
}
public void setImage(String imagefilename) throws IOException{
bi = ImageIO.read(new File(imagefilename));
this.setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));

this.revalidate();
this.repaint();
}
@Override
public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.drawImage(bi,0,0,null);
}
}

I can set the image by giving an image filename.But I wish to clear
the previous image from the panel,thus showing a blank image
panel,if a user supplies an invalid image name. How can I do
this?Can someone please tell me?
[...]

If no image gets loaded, set the reference to null and test for that
in the paintComponent() method and don't draw the image just clear
the background.

Knute's right; and if you forget the test, as I have sometimes done, the
drawImage() method "does nothing if img is null."

<http://download.oracle.com/javase/6/docs/api/java/awt/Graphics.html>

I never noticed that before. I must have been trying to protect against
a NullPointerException that never gets thrown.
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top