title: Set background image for JPanel in Java Breakout Game
body: <p>Okay, this seems like a really simple task but I can't seem to get it. All I want is to have an image (jpg) as the background to my breakout game. Here is my code:</p>
  
  <p>Main Class:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  
  
  public class Breakout extends JFrame {
  
      public Breakout()
      {             
          add(new BreakBoard());
          setTitle("Breakout");
          setSize(BreakCommons.WIDTH, BreakCommons.HEIGTH);
          setLocationRelativeTo(null);
          setIgnoreRepaint(true);
          setResizable(false);
          setVisible(true);
      }
  
      public static void main(String[] args) {
          new Breakout();
      }
  }
  </code></pre>
  
  <p>Board Class:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Font;
  import java.awt.FontMetrics;
  import java.awt.Graphics;
  import java.awt.Point;
  import java.awt.Toolkit;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.util.Timer;
  import java.util.TimerTask;
  import javax.swing.ImageIcon;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  
  public class BreakBoard extends JPanel implements BreakCommons {
  
      ImageIcon icon = new ImageIcon("../pacpix/love.jpg"); 
      Timer timer;
      String message = "Game Over";
      BreakBall ball;
      BreakPaddle paddle;
      BreakBrick bricks[];
      boolean ingame = true;
      int timerId;
  
  
      public BreakBoard() {
          setOpaque(false);
          addKeyListener(new TAdapter());
          setFocusable(true);
          //setBackground(Color.white);
          bricks = new BreakBrick[30];
          setDoubleBuffered(true);
          timer = new Timer();
          timer.scheduleAtFixedRate(new ScheduleTask(), 1000, 10);
      }
  
      public void paint(Graphics g) {
          super.paint(g);
          g.drawImage(icon.getImage(), 10, 10, this);
  
          if (ingame) {
              g.drawImage(ball.getImage(), ball.getX(), ball.getY(),
                          ball.getWidth(), ball.getHeight(), this);
              g.drawImage(paddle.getImage(), paddle.getX(), paddle.getY(),
                          paddle.getWidth(), paddle.getHeight(), this);
  
              for (int i = 0; i &lt; 30; i++) {
                  if (!bricks[i].isDestroyed())
                      g.drawImage(bricks[i].getImage(), bricks[i].getX(),
                                  bricks[i].getY(), bricks[i].getWidth(),
                                  bricks[i].getHeight(), this);
              }
          } else {
  
              Font font = new Font("Verdana", Font.BOLD, 18);
              FontMetrics metr = this.getFontMetrics(font);
  
              g.setColor(Color.BLACK);
              g.setFont(font);
              g.drawString(message,
                           (BreakCommons.WIDTH - metr.stringWidth(message)) / 2,
                           BreakCommons.WIDTH / 2);
          }
  
  
          Toolkit.getDefaultToolkit().sync();
          g.dispose();
      }
  </code></pre>
  
  <p>So the image is stored in variable <code>icon</code> and is located in my source files (along with my classes) so I know that the path is right. I tried setting the frame and panel to <code>setOpaque(false)</code> but that just changed the background to default grey. I can easily set the background color by <code>setBackground(Color.white);</code>, but how do I set an image?</p>
  
  <p>I tried putting the image in a JLabel and then adding it to the JPanel, but that produced no results. I would appreciate any help, and thanks in advance! I can provide more information as needed.</p>