how to clear the image from jpanel

Discussion in 'Java' started by jimgardener, Oct 28, 2010.

  1. jimgardener

    jimgardener Guest

    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
     
    jimgardener, Oct 28, 2010
    #1
    1. Advertising

  2. 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/
     
    Knute Johnson, Oct 28, 2010
    #2
    1. Advertising

  3. In article <ddhyo.3138$>,
    Knute Johnson <> 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>
     
    John B. Matthews, Oct 28, 2010
    #3
  4. On 10/28/2010 11:50 AM, John B. Matthews wrote:
    > In article<ddhyo.3138$>,
    > Knute Johnson<> 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/
     
    Knute Johnson, Oct 28, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    27,161
  2. David

    Response.Clear() doesn't clear

    David, Jan 31, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,129
    Mark Fitzpatrick
    Jan 31, 2008
  3. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,634
    John B. Matthews
    Jul 18, 2008
  4. t1m1976
    Replies:
    1
    Views:
    2,526
    t1m1976
    Nov 7, 2010
  5. InvalidLastName

    Unrecognized element 'add' after <clear></clear>

    InvalidLastName, Feb 26, 2007, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    1,077
    Steven Cheng[MSFT]
    Mar 6, 2007
Loading...

Share This Page