File source

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

/** show image in JPanel */
public class ImagePanel extends JPanel{

    private static final long serialVersionUID = -8197349378844968222L;
    private BufferedImage image = null;
    private int width;
    private int height;
    
    public ImagePanel(int w, int h) {
        width = w;
        height = h;
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(image!=null){
            g.drawImage(image, 0, 0, null);
        }
    }
    
    public void setImage(BufferedImage image){
        this.image = image;
        repaint();
    }
    
    @Override
    public Dimension getSize() {
        return new Dimension(width, height);
    }
    
    @Override
    public Dimension getPreferredSize() {
        return getSize();
    }
    
    @Override
    public Dimension getMinimumSize() {
        return getSize();
    }
}