0

I want to display an image inside a panel. So I pass the path of the image to this method, open image file and pass it to the method of a private class dedicated to draw image inside the panel. The problem is the panel remains empty all the time and doesn't display anything.

Here is the code:

JPanel ImagePane; // I want to add image to this

public void getImagePath(String Path)
{       
    BufferedImage image = null;

    try 
    {
        image=ImageIO.read(new File(Path));
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    DisplayImage display= new DisplayImage();
    display.getImage(image);
}

private class DisplayImage extends JPanel
{
    private BufferedImage image=null;

    public void getImage(BufferedImage im)
    {
        image=im;
        repaint();
    }

    public void paintComponents(Graphics g)
    {
        g.drawImage(image, 0, 0, image.getWidth() /2, image.getHeight()/2,ImagePane);
    }
}

What am I missing?

Umer Farooq
  • 31
  • 1
  • 6

3 Answers3

5

paintComponents is a method of the Container which is used to paint each of the components in the container. Instead you need paintComponent to paint this single component.

Change your

public void paintComponents(Graphics g)

method to

@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
   g.drawImage(image, 0, 0, image.getWidth() /2, image.getHeight()/2,ImagePane);
}

Notice the use of the @Override annotation to help with method signature checking.

Also calling

super.paintComponent(g);

will update child components.


In your method getImagePath you don't appear to add the DisplayImage to any container. Instead you create a local DisplayImage, but don't use it.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Could be a size issue. What is the layout of the parent container of your `DisplayImage`? – Reimeus Jan 22 '13 at 12:52
  • In your method `getImagePath` you don't appear to add the `DisplayImage` to any container... – Reimeus Jan 22 '13 at 13:03
  • When I add `image` to panel, it says add a container. What should I add then? – Umer Farooq Jan 22 '13 at 13:12
  • 1
    You then need to add the panel to your container. Would look _something like_ `add(display);`. As you're using `GroupLayout` this will not be the exact syntax. Consider posting an [SSCCE](http://sscce.org/) – Reimeus Jan 22 '13 at 13:26
  • Actually I am learning Windows Builder. So I already created a JPanel there to which I want to add this image. – Umer Farooq Jan 22 '13 at 13:36
3

You have to override paintComponent

 protected void paintComponent(Graphics g)

But in your code you are creating public void paintComponents(Graphics g) which is not correct

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
2

There's the use of @Override annotation. If you make it a practice to use it whenever you're overriding a method, this issue can be resolved at compile-time. You need to use this:

    @Override 
    public void paintComponent(Graphics g)
Swapnil
  • 8,201
  • 4
  • 38
  • 57