title: Java - JPanel with background image AND normal functionality
body: <p>I've been looking around and working on making a <code>JPanel</code> able to have an image background. I know there is a ton of information reguarding this ( <a href="https://stackoverflow.com/questions/4051408/jpanel-with-image-background">example 1</a> and <a href="https://stackoverflow.com/questions/1064977/setting-background-images-in-jframe">example 2</a> ). Neither of these are exactly what I'm looking for.</p>
  
  <p>What I am looking for, is a way to just <em>add</em> a function or the functionality to set a background image on a <code>JPanel</code> while maintaining everything else. The problem that I have right now is that I have a few classes that extend <code>JPanel</code> and when I change them to extend my new <code>ImagePanel</code> class, the constructor fails (obviously, the parameters are different). I have the constructor for one of those files going <code>super( new BorderLayout() );</code> and the only way I can get it to function so far is to ditch either the layout, or the background image. I can't get both to work together. </p>
  
  <p>Here's the latest desperate attempt:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.JPanel;
  
  class ImagePanel extends JPanel {
  
    private Image img;
  
    public ImagePanel(Image img, LayoutManager layout) {
      this.img = img;
      Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
      setPreferredSize(size);
      setMinimumSize(size);
      setMaximumSize(size);
      setSize(size);
      setLayout(null);
      super.setLayout(layout);
    }
  
    public void paintComponent(Graphics g) {
      g.drawImage(img, 0, 0, null);
    }
  
  }
  </code></pre>
  
  <p>If anyone could help me out, or point me towards what I'm looking for, that would be great. I don't need any super crazy functionality like background scaling or anything like that. I just want an image to be the background, and for it to maintain its functionality in all other ways.</p>
  
  <p>Thanks.</p>