0

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);
                }
            }
    });
    }
    
}
Kirby Kode
  • 19
  • 4
  • `.jpg` forgotten? And better, more modern style is to use `ImageIO` for reading. – Joop Eggen Jul 19 '22 at 13:26
  • i want to insert a borderlayout to add the two frames on top of the background image, is that possible? – Kirby Kode Jul 19 '22 at 13:32
  • Add a separate JPanel for your background at the CENTER, leaving the default content pane as it is. Adding to the JFrame is the sane as adding to the content pane. Then add other panels to SOUTH, NORTH or whatever. – Joop Eggen Jul 19 '22 at 13:38
  • I tried to add add(img, BorderLayout.CENTER); but I kept getting this error "no suitable method found for add(Image, string)". Netbeans' suggested fix was to add "Create method "add(Lyrics.java.lang.String) in MainForm". The fix isn't really that necessary and might ruin the codes – Kirby Kode Jul 19 '22 at 13:45
  • 1
    No, you should add a new JPanel with its paintComponent just as it is done now. Alternatively you can use a JLabel and set its icon with an image. – Joop Eggen Jul 19 '22 at 13:54
  • *to add the two frames on top of the background image* - what two frames? An application consists of a single JFrame. You can then add components to the frame. Typically you add a JPanel which contains other Swing components. Use proper terminology when asking a question so we don't have to guess what you are really asking. – camickr Jul 19 '22 at 14:15

1 Answers1

0

according to: How do I draw an image to a JPanel or JFrame? , instead of using

Image img = Toolkit.getDefaultToolkit().getImage("C:\\Users\\xxxx\\Path\\image.jpg");

try to use:

BufferedImage img = ImageIO.read(new File("C:\\Users\\xxxx\\Path\\image.jpg"));

Do not forget to indicate the correct path to the file and the file itself with the extension.

Mio
  • 175
  • 2
  • 11