Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to clear the image from jpanel

Reply
Thread Tools

how to clear the image from jpanel

 
 
jimgardener
Guest
Posts: n/a
 
      10-28-2010
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
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      10-28-2010
On 10/28/2010 6:30 AM, jimgardener wrote:
> 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.

--

Knute Johnson
email s/nospam/knute2010/

 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      10-28-2010
In article <ddhyo.3138$(E-Mail Removed)>,
Knute Johnson <(E-Mail Removed)> wrote:

> On 10/28/2010 6:30 AM, jimgardener wrote:
> > 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>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      10-28-2010
On 10/28/2010 11:50 AM, John B. Matthews wrote:
> In article<ddhyo.3138$(E-Mail Removed)>,
> Knute Johnson<(E-Mail Removed)> wrote:
>
>> On 10/28/2010 6:30 AM, jimgardener wrote:
>>> 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.

--

Knute Johnson
email s/nospam/knute2010/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
JPanel inside JPanel inside JFrame t1m1976 Java 1 11-07-2010 09:47 PM
[JPanel] Drop Down JPanel Raphael Jolivet Java 1 07-18-2008 12:30 AM
Add a JPanel to a JPanel... Andreas Beresko Java 6 07-16-2004 12:10 AM
Drag GIF image on JPanel terry Java 1 01-31-2004 04:32 AM



Advertisments