0

I am trying to add a background image to a Jpanel. Below is the code I'm using

    private void createUIComponents() {
        JPanel panel1 = new BgPanel();
    } 


class BgPanel extends JPanel {
        Image bg = new ImageIcon("image.png").getImage();
        @Override
        public void paintComponent(Graphics g) {
            g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
        }
}

I am using IntelliJ so most of the GUI code is hidden. Shouldn't the custom created JPanel be a BgPanel and therefore display the image? Where have I gone wrong?

I am getting the following when trying to compile:

Exception in thread "main" java.lang.NullPointerException
    at testGUI.$$$setupUI$$$(testGUI.java)
    at testGUI.<init>(testGUI.java:4)
    at testGUI.main(testGUI.java:11)

Full Code:

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

public class testGUI {
    private JButton button1;
    private JPanel panel1;

    public static void main(String[] args) {
        JFrame frame = new JFrame("testGUI");
        frame.setContentPane(new testGUI().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private void createUIComponents() {
        JPanel panel1 = new BgPanel();
    }

    class BgPanel extends JPanel {
        Image bg = new ImageIcon("imagelink.png").getImage();

        @Override
        public void paintComponent(Graphics g) {
            g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
        }
    }
}
Sam D
  • 1
  • 1
  • Did you add it to anything to display? Why not use an image icon + JLabel? – matt Mar 05 '18 at 18:27
  • @matt I previously tried using a JLabel and icon which displayed the image fine but I need to display a lot of components over it and was having a lot of trouble being able to add them over it. – Sam D Mar 05 '18 at 18:29
  • The exception says `$$$setupUI$$$` is the method where something is going wrong. Can you show us the code for that method? – bcr666 Mar 05 '18 at 18:44
  • @Brianbcr666Ray I believe that's IntelliJs auto generated method for the GUI builder. IntelliJ hides this code. – Sam D Mar 05 '18 at 18:45
  • You never create an instance of `panel1`, so it is `null`. Even if you called `createUIComponents`, the `panel1` instance field would be `null`, because `panel1` is shadowed inside `createUIComponents`. Change `createUIComponents ` to something more like `private void createUIComponents() { panel1 = new BgPanel(); }` and make sure you call it before you use it – MadProgrammer Mar 05 '18 at 18:57
  • Make sure you call `super.paintComponent` before you do any custom painting. You may also want to take a look at [Reading/Loading an Image](https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html) – MadProgrammer Mar 05 '18 at 18:58

1 Answers1

0

Try this:

 class BgPanel extends JPanel {
    private final BufferedImage image;
             public BgPanel() throws IOException {
      image = ImageIO.read(new File("path-to-image"));
         }
    @Override
            public void paintComponent(Graphics g) {
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
        }

Hope it will work Complete code:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class testGUI {
    private JButton button1;
    private JPanel panel1;

    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("testGUI");
        frame.getContentPane().add(new BgPanel());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

 static class BgPanel extends JPanel {
private final BufferedImage image;
         public BgPanel() throws IOException {
             setPreferredSize(new Dimension(400,800));
  image = ImageIO.read(new File("C:\\Users\\Sourav\\Desktop\\fb\\IMG_20171209_181249.jpg"));
     }
@Override
        public void paintComponent(Graphics g) {
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
    }
}
Mamun Kayum
  • 161
  • 7