Fuv
Fuv

Reputation: 922

Showing an image using JPanel

I have written my own ImagePanel using one of previous topics here:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image = null;

    public ImagePanel(BufferedImage im) {
       image = im;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);            
    }
}

And then I thought it would be nice to add this panel on the normal JPanel(it would be easier to put it on the frame using this all NetBeans stuff). So I added one, NetBeans generated me some code: private javax.swing.JPanel pnlImagePanel; And here it comes the moment when I would like to show image, so:

File selectedFile = new File(path);
try {
       image = ImageIO.read(selectedFile);
} catch(IOException ex) {
       throw new RuntimeException(ex);
}
ImagePanel imPanel = new ImagePanel(image);
this.pnlImagePanel = imPanel;
this.pnlImagePanel.repaint();

Problem is obvious - I got no result. Shouldn't it work? I've overriden the method paintComponent, so polymorphism should fire. Or is something missing to me?

Upvotes: 2

Views: 155

Answers (3)

qi zhang
qi zhang

Reputation: 11

import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.ImageIcon;

/**
 * imagePanel
 * @author zhangqi [email protected]
 * @date Jan 10, 2013 4:45:05 PM
 * @version V1.0
 */
public class ImagePanel extends JPanel
{

    private static final long serialVersionUID = 1L;
    private BorderLayout borderLayout = new BorderLayout();
    private ImageIcon image = null;

    public ImagePanel(ImageIcon image)
    {
        this.image = image;
        this.setLayout(borderLayout);
    }

    protected void paintComponent(Graphics g)
    {
        setOpaque(true);
        super.paintComponent(g);
        if (image != null)
            g.drawImage(image.getImage(), 0, 0, this.getWidth(),
                    this.getHeight(), this);
    }

    public ImageIcon getImage()
    {
        return image;
    }

    public void setImage(ImageIcon image)
    {
        this.image = image;
    }
}

Upvotes: 1

Reimeus
Reimeus

Reputation: 159864

Just assigning an member variable pnlImagePanel to your ImagePanel will not work, you would have to add the panel to the JPanel container:

pnlImagePanel.add(imPanel);

You will need to give imPanel a size so that the image can be seen. The easiest approach would be to use a layout manager that allows the child panel occupy the maximum area. Rather than the default FlowLayout, you could use GridLayout:

pnlImagePanel.setLayout(new GridLayout());

Calling repaint is unnecessary here. The paint chain mechanism will ensure that your panels are painted.

Upvotes: 6

tenorsax
tenorsax

Reputation: 21233

Override getPrefferedSize() in ImagePanel to return image size, ie:

    @Override
    public Dimension getPreferredSize() {
        if (image == null) {
            return super.getPreferredSize();
        }
        return new Dimension(image.getWidth(this), image.getHeight(this));
    }

And, yes, don't forget to add the image panel to the container as suggested by @Reimeus. +1 to him.

Upvotes: 2

Related Questions