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.
Loading...
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();
}*/
}
Nishit - February 10, 2009 at 1:09 pm
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)
}
}
Rob Bamforth - February 11, 2009 at 12:59 pm
hey can u help me???
ill do it.. but it cant run!!
it said that illegal image!
caleb - October 6, 2009 at 7:28 am
if you post you’re code i’ll have a look! is the path to your image correct?
Rob Bamforth - October 6, 2009 at 7:50 pm