Tuesday, April 2, 2013

Java Swing - how to add an image to a JPanel?

How to add image inside jPanel ?


import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
       try {                
          image = ImageIO.read(new File("image name and path"));
       } catch (IOException ex) {
            // handle exception...
       }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters            
    }

}



--------------------------------------------------
How to add image inside jPanel ?
Java Swing - how to add image in north of Jpanel
java - how to put Image on JPanel using Netbeans 
background image on JPanel Swing
Add an image to JFrame
Adding multiple images to JPanel
Adding picture to Jframe on click‎ 
Add image to jframe‎ 
JPanel background picture?‎
How do i add images to java Jframe? 
How to set a background image for a JFrame using Jlabel 

1 comment:

  1. I'll try it. When I finished downloaded the netbeans :))
    hopefully no errors ..

    ReplyDelete