Java paintComponent not display background image on JFrame

28 views Asked by At

I am trying to display an image that have in a subdirectory of my project src, and from the testing that I have done, the path reference to utilize the image is correct, however, when I run the application I do not see the background image that I am trying to set for the JFrame. Below is my code.

public class Main extends javax.swing.JFrame {    
    private BufferedImage dbImage = null;

    public Main() {
        initComponents();
        try {
            URL urlToImage = this.getClass().getResource("/resources/images/background.jpg");
            dbImage = ImageIO.read(urlToImage);
            //dbImage = ImageIO.read(new File("/resources/images/background.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        
        g.drawImage(dbImage, 0, 0, getWidth(), getHeight(), this);
    }
}
0

There are 0 answers