Add A Background Image To A JPanel

How to add a backrgound image to jPanel.

Firstly, create a method to override the paintComponent method of the jPanel:

import javax.swing.*;
import java.awt.*;

class BackgroundPanel extends JPanel
{
Image image;
public BackgroundPanel()
{
try
{
image = (new ImageIcon(getClass().getResource(“/icons/logo.jpg”))).getImage();
}
catch(Exception e){/*handled in paintComponent()*/}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(image != null) g.drawImage(image, (this.getWidth()/2) – (image.getWidth(this) / 2),(this.getHeight()/2) – (image.getHeight(this) / 2),image.getWidth(this),image.getHeight(this),this); //(image,location x, location y, size x, size y)
}
}

Secondly, when greating a jPanel use the folling code to implement the new BackgroundPanel() method:

JPanel panel = new BackgroundPanel();

This example will display the image in the centre of the jPanel.

The image logo.jpg is located in a folder called icons within the source code.

About these ads
Add A Background Image To A JPanel

5 thoughts on “Add A Background Image To A JPanel

  1. Nishit says:

    I m trying 2 run this code but i m not able 2 run it pls do help as i hav the following code and i want to add image on back of this panel so do help me what changes should be made in this coding:

    package p1;

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class Classical implements ItemListener
    {
    //JFrame f;

    public JPanel p;
    JCheckBox cb1,cb2,cb3,cb4;

    public static JButton button;

    public Classical()
    {
    //f=new JFrame(“Frame”);

    p=new JPanel();
    p.setLayout(null);

    cb1=new JCheckBox(“Mohammed Rafi”);
    cb2=new JCheckBox(“cb2ita Krishnamurthy”);
    cb3=new JCheckBox(“cb3 Mangeshkar”);
    cb4=new JCheckBox(“Kishor kumar”);

    button=new JButton(“Add to the Cart”);

    p.setBounds(150,50,1000,1000);
    p.setBackground(Color.black);
    p.setSize(1000,1000);

    cb1.setBounds(40,50,20,25);
    cb2.setBounds(40,100,20,25);
    cb3.setBounds(40,150,20,25);
    cb4.setBounds(40,200,20,25);

    Font myfont=new Font(“Courier New”,Font.BOLD,25);

    Classical.button.setBounds(400,300,180,30);

    p.add(cb1);
    p.add(cb2);
    p.add(cb3);
    p.add(cb4);

    p.add(Classical.button);
    //p.addActionListen(Classical.button);

    /*f.add(p);
    f.setSize(1000,500);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)*/;
    }
    public void itemStateChanged(ItemEvent e)
    {

    Object o=e.getSource();

    }

    /*public static void main(String args[])
    {
    Classical obj=new Classical();
    obj.launchPanel();
    }*/
    }

    1. Rob Bamforth says:

      Hi Nishit

      When you declare you’re JPanel() you need to use the BackgroundPanel() method described above.

      In you’re code you need to replace:
      p=new JPanel();

      with:
      p=new BackgroundPanel();

      In addition you will need to create a class called BackgroundPanel(), which must look like this:

      import javax.swing.*;
      import java.awt.*;

      class BackgroundPanel extends JPanel
      {
      Image image;
      public BackgroundPanel()
      {
      try
      {
      image = (new ImageIcon(getClass().getResource(”/icons/logo.jpg”))).getImage();
      }
      catch(Exception e){/*handled in paintComponent()*/}
      }
      public void paintComponent(Graphics g)
      {
      super.paintComponent(g);
      if(image != null) g.drawImage(image, (this.getWidth()/2) – (image.getWidth(this) / 2),(this.getHeight()/2) – (image.getHeight(this) / 2),image.getWidth(this),image.getHeight(this),this); //(image,location x, location y, size x, size y)
      }
      }

  2. Rick says:

    Thanks for publishing this solution. It answered a question one of my students had.

    Now if I can just get the paths working to files in other folders…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s