title: Background image in a JFrame
body: <p>This question has been asked a lot but everywhere the answers fall short. I can get a JFrame to display a background image just fine by extending JPanel and overriding paintComponent, like so:</p>
  
  <pre><code>class BackgroundPanel extends JPanel {
      private ImageIcon imageIcon;
      public BackgroundPanel() {
          this.imageIcon = Icons.getIcon("foo");
    }
  
      @Override
      protected void paintComponent(Graphics g) {
         super.paintComponent(g);
          g.drawImage(imageIcon.getImage(), 0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight(),this);
      }
  }
  </code></pre>
  
  <p>But now, how do you add a component on top of that background? 
  When I go</p>
  
  <pre><code>JFrame w = new JFrame() ;
  Container cp = w.getContentPane();
  cp.setLayout(null);
  
  BackgroundPanel bg = new BackgroundPanel();
  cp.add(bg);
  
  JPanel b = new JPanel();
  b.setSize(new Dimension(30, 40));
  b.setBackground(Color.red);
  cp.add(b);
  w.pack()
  w.setVisible(true)
  </code></pre>
  
  <p>It shows the little red square (or any other component) and not the background, but when I remove <code>cp.setLayout(null);</code>, the background shows up but not my other component. I'm guessing this has something to do with the paintComponent not being called by the null LayoutManager, but I'm not at all familiar with how LayoutManagers work (this is a project for college and the assignment specifically says not to use a LayoutManager).</p>
  
  <p>When i make the image the background has to display null (and so, transparant (??)) the red square shows up so it might be that the background is actually above my other components.</p>
  
  <p>Does anyone anyone have any ideas?</p>
  
  <p>Thanks</p>