title: Java - Get an Image of a JPanel
body: <p>I am writing a program in which I paint on a <code>JPanel</code>. How do I get an <code>Image</code> of the <code>JPanel</code> which is painted on it?
  I tried this code but all I get is a blank image with the Background color of my <code>JPanel</code>.
  The <code>BufferedImage</code> does not contain what is painted on my panel.</p>
  
  <pre><code>    private BufferedImage createImage(JPanel panel) {    
          int w = panel.getWidth();
          int h = panel.getHeight();
          BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
          Graphics2D g = bi.createGraphics();
          panel.paint(g);
          return bi;
      }
  </code></pre>
  
  <p>What am I doing wrong?</p>