Whenever I run the program, the background image is not visible, it is just a white blank space. I tried following the instructions on this website -> https://www.tutorialspoint.com/how-to-add-background-image-to-jframe-in-java, but is not working. I tried the getImage method and provided the file path towards that image. Can someone please explain what is wrong with my code and how to fix it?
public class MainForm extends JFrame {
Image img = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Jack\\Desktop\\Folder\\Course\\Draft repo");
public MainForm() throws IOException {
this.setContentPane(new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
});
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new MainForm();
} catch (IOException ex) {
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}