Drawing an Image to a JPanel within a JFrame

66,820

Solution 1

I'd like to suggest a more simple way,

  image = ImageIO.read(new File(path));
  JLabel picLabel = new JLabel(new ImageIcon(image));

Yayy! Now your image is a swing component ! add it to a frame or panel or anything like you usually do! Probably need a repainting too , like

  jpanel.add(picLabel);
  jpanel.repaint(); 

Solution 2

You can use the JLabel.setIcon() to place an image on the JPanel as shown here.

On the other hand, if you want to have a panel with a background, you can take a look at this tutorial.

Solution 3

There is no need to manually invoke paintComponent() from a constructor. The problem is that you passing null for a Graphics object. Instead, override paintComponent() and you use the Graphics object passed in to the method you will be using for painting. Check this tutorial. Here is an example of JPanel with image:

class MyImagePanel extends JPanel{ 
    BufferedImage image;
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(image != null){
            g.drawImage(image, 0, 0, this);
        }
    }
}
Share:
66,820
MattTheHack
Author by

MattTheHack

Updated on July 17, 2022

Comments

  • MattTheHack
    MattTheHack almost 2 years

    I am designing a program that contains two JPanels within a JFrame, one is for holding an image, the other for holding GUI components(Searchfields etc). I am wondering how do I draw the Image to the first JPanel within the JFrame?

    Here is a sample code from my constructor :

    public UITester() {
        this.setTitle("Airplane");
        Container container = getContentPane();
        container.setLayout(new FlowLayout());
        searchText = new JLabel("Enter Search Text Here");
        container.add(searchText);
        imagepanel = new JPanel(new FlowLayout());
        imagepanel.paintComponents(null);
       //other constructor code
    

    }

    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.drawImage(img[0], -50, 100, null);
    }
    

    I was trying to override the paintComponent method of JPanel to paint the image, but this causes a problem in my constructor when I try to write :

    imagepanel.paintComponents(null);
    

    As it will only allow me to pass the method null, and not Graphics g, anybody know of a fix to this method or another method i can use to draw the image in the JPanel? Help is appreciated! :)

    All the best and thanks in advance! Matt

  • Andrew Thompson
    Andrew Thompson over 12 years
    It is best to link to the latest version of the JavaDocs. I have edited your answer to point to J2SE 7. For tips on getting a link to the latest docs, see point 2 of advantages.
  • Andrew Thompson
    Andrew Thompson over 12 years
    One small note. Given this sounds very much like an application-resource, it would normally be accessed by URL rather than File.
  • nIcE cOw
    nIcE cOw over 12 years
    @AndrewThompson : Pardon Me, but isn't J2SE now known as Java SE. Regards
  • Andrew Thompson
    Andrew Thompson over 12 years
    @GagandeepBali What, this minute? Sun used to change names of things so regularly that I got sick of keeping up with the latest buzz-phrases. :(
  • nIcE cOw
    nIcE cOw over 12 years
    @AndrewThompson : Hehe, too true, just someone edited my answer once regarding this, that's why I remember it till now, else no doubt such things are hard to remember. Regards
  • Yeshwin Verma The Programmer
    Yeshwin Verma The Programmer over 3 years
    what if i want to draw a picture on particular x and y position