title: How to put a background image in a JFrame?
body: <p>I'm trying a little <code>JFrame</code> because I have to make the flappy bird project and there's a bug in my code.</p>
  <p>When I run it, it opens at maximized window but the background picture doesn't load. I have to minimize the window then maximize it for it to work.</p>
  <pre><code>package com.company;
  
  import javax.imageio.ImageIO;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  
  public class Main extends JFrame{
      JButton button;
      JLabel text;
      JFrame frame;
      JPanel panel;
  
      public static void main(String[] args) {
          new Main();
      }
  
      public Main(){
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setTitle(&quot;flappyBird&quot;);
          setLocationRelativeTo(null);
          setVisible(true);
          setSize(new Dimension(100,100));
          setExtendedState(JFrame.MAXIMIZED_BOTH);
  
          BufferedImage img = null;
          try {
              img = ImageIO.read(new File(&quot;C:\\Users\\m11\\Documents\\FlappyBird\\spirit\\base.png&quot;));
          } catch (IOException e) {
              e.printStackTrace();
          }
          Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
          ImageIcon imageIcon = new ImageIcon(dimg);
          setContentPane(new JLabel(imageIcon));
      }
  }
  </code></pre>
  <p>How can I fix it?</p>