title: Swing JLayeredPane not painting all elements on paintAll call
body: <p>I have <code>JLayeredPane</code> that contains <code>Canvas</code> on level 0(fills itself yellow color in <code>Paint</code> method) and <code>JPanel</code> on level 1(set's it background as red in constructor). </p>
  
  <p>On button click in <code>paintAllToImage</code> method I create <code>BufferedImage</code> and draw <code>JLayerePane</code> on this image using <code>component.paintAll(image.getGraphics());</code> <br> The problem is, that image has only <code>Canvas</code> painted (it is completely filled in yellow). Please see image attached. </p>
  
  <p><a href="https://i.stack.imgur.com/8egWQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8egWQ.png" alt="enter image description here"></a> </p>
  
  <p>(part above button is what is actually drawn, part below button is image, created from <code>JLayeredPane</code>)</p>
  
  <p>Here is the full code:</p>
  
  <pre><code>public class LayeredPaneEx extends JPanel {
  
      private JLayeredPane layeredPane;
      public LayeredPaneEx()    {
          setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  
          layeredPane = new JLayeredPane();
          layeredPane.setPreferredSize(new Dimension(300, 310));
          layeredPane.setLayout(null);
  
          Canvas panel = new CustomCanvas();
          panel.setSize(300, 400);
          CustomPanel customPanel = new CustomPanel();
          layeredPane.add(panel, new Integer(0));
          layeredPane.add(customPanel, new Integer(1));
  
          add(layeredPane);
  
          JButton paintBtn = new JButton("Paint All");
          paintBtn.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  ImageIcon icon = new ImageIcon(paintAllToImage(layeredPane));
                  JLabel imageLabel = new JLabel(icon);
                  add(imageLabel);
              }
          });
          add(paintBtn);
  
          JLabel paintLabel = new JLabel();
          paintLabel.setPreferredSize(new Dimension(300, 300));
      }
  
      private class CustomCanvas extends Canvas  {
          @Override
          public void paint(Graphics g) {
              g.setColor(Color.YELLOW);
              g.fillRect(0, 0, getWidth(), getHeight());
          }
      }
  
      private class CustomPanel extends JPanel {
          CustomPanel() {
              setSize(100, 100);
              setBackground(Color.RED);
          }
      }
  
      private static void createAndShowGUI() {
          JFrame frame = new JFrame("LayeredPaneDemo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          JComponent newContentPane = new LayeredPaneEx();
          newContentPane.setOpaque(true);
          frame.setContentPane(newContentPane);
  
          frame.pack();
          frame.setVisible(true);
      }
  
      public static void main(String[] args) {
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI();
              }
          });
      }
  
      public static BufferedImage paintAllToImage(Component component) {
          BufferedImage image;
          image = new BufferedImage(
                  component.getWidth(),
                  component.getHeight(),
                  BufferedImage.TYPE_INT_RGB
          );
          component.paintAll(image.getGraphics());
          return image;
      }
  }
  </code></pre>