title: Cannot draw transparent Component backgrounds
body: <p>I have tried several tutorials and searches to figure out how to accomplish what I am trying to do. Basically I have a JLayeredPane with two Jpanels inside it. One for my game's drawing surface and one for my gui, like a pause menu. I have a png file with transparencies that I want to be the background of my gui panel that popups when the user hits escape. No matter what I do, the background of the panel (even tried making it just a component) is always grey with my png file drawn over it.</p>
  
  <p>I have tried what others have recommended such as the following.</p>
  
  <pre><code>setBackground(new Color(0,0,0,0)); 
  </code></pre>
  
  <p>and </p>
  
  <pre><code>setOpaque(false);
  </code></pre>
  
  <p>Neither of these has seemed to help and perhaps I am failing to do something else after these. I have traditionally done them after the constructor or within the constructor of a class that extends jpanel.</p>
  
  <p>I am almost to the point where I am going to have one panel and draw everything myself but I would much rather use the built in java functions like boxlayouts, etc.</p>
  
  <p>Edit Adding Working Example:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Canvas;
  import java.awt.Color;
  import java.awt.Cursor;
  import java.awt.Dimension;
  import java.awt.DisplayMode;
  import java.awt.Font;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import java.awt.GraphicsDevice;
  import java.awt.GraphicsEnvironment;
  import java.awt.Image;
  import java.awt.Point;
  import java.awt.Toolkit;
  import java.awt.event.ComponentAdapter;
  import java.awt.event.ComponentEvent;
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.event.WindowAdapter;
  import java.awt.event.WindowEvent;
  import java.awt.image.BufferStrategy;
  import java.awt.image.BufferedImage;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.URL;
  
  import javax.imageio.ImageIO;
  import javax.swing.BoxLayout;
  import javax.swing.JFrame;
  import javax.swing.JLayeredPane;
  import javax.swing.JPanel;
  
  
  public class Example {
  
  private MyWindow gWindow;
  
  public static void main(String argv[]) {
      Example g = new Example();
  
      g.gameLoop();
  }
  
  public Example() {
      gWindow = new MyWindow();
  
      // Initialize the keyboard listener
      gWindow.frame().addKeyListener(new KeyListener() {
          public void keyPressed(KeyEvent e) {
              if(e.getKeyCode() == KeyEvent.VK_ESCAPE) // escape key, show menu
              {
                  System.exit(0);
              }
          }
  
          @Override
          public void keyReleased(KeyEvent e) {           
          }
  
          @Override
          public void keyTyped(KeyEvent e) {
          }
      });
  }
  
  public void gameLoop() {
      long lastLoopTime = System.currentTimeMillis();
      while(true) {
          // Used to calculate movement of sprites
          long delta = System.currentTimeMillis() - lastLoopTime;
          lastLoopTime = System.currentTimeMillis();
  
          // Clear the canvas
          Graphics2D g = (Graphics2D) gWindow.getBufferStrategy().getDrawGraphics();
          g.setColor(Color.black);
          g.fillRect(0,0,gWindow.frame().getWidth(), gWindow.frame().getHeight());
  
          // Clean up graphics and flip buffer
          g.dispose();
          gWindow.getBufferStrategy().show();
  
          // Small delay before next cycle
          try { Thread.sleep(10); } catch (Exception e) {}
      }
  }
  
  public class MyWindow {
      private JFrame frame;
      private JLayeredPane container;
      private MyPanel gui;
      private JPanel surface;
  
      private Canvas canvas;
  
      private GraphicsDevice vc;
      private Dimension dm;
  
      BufferedImage menuImg = null;
      BufferedImage menuImgHighlight = null;
      BufferedImage gSettings = null;
      Font font = null;
  
      public MyWindow() {
          frame = new JFrame("Jumper");
  
          vc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  
          DisplayMode display = vc.getDisplayMode();
          dm = new Dimension(display.getWidth(), display.getHeight());
  
          container = new JLayeredPane();     
          gui = new MyPanel();
          gui.setLayout(new BoxLayout(gui, BoxLayout.Y_AXIS));
          surface = new JPanel(new BorderLayout(0,0));
  
          frame.add(container, BorderLayout.CENTER);
          container.add(surface, new Integer(0), 0);
          container.add(gui, new Integer(1), 0);
  
          init_resources();
  
          canvas = new Canvas();
          surface.add(canvas);    
  
          gui.setBackground(new Color(0,0,0,0));
          gui.setVisible(true);
          gui.setOpaque(false);
  
          surface.setVisible(true);
  
          setFullScreen(display);
  
          frame.setResizable(false);
          frame.setVisible(true);
          frame.pack();
          frame.addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                  System.exit(0);
              }
          });
  
          frame.addComponentListener(new ComponentAdapter() {
  
              @Override
              public void componentResized(ComponentEvent e) {
                  setScreen(new Dimension(frame.getWidth(), frame.getHeight()));
                  frame.repaint();
              }
          });
  
          canvas.setIgnoreRepaint(true);
          canvas.createBufferStrategy(2);
          canvas.setFocusable(false);
      }
  
      public JFrame frame() {
          return frame;
      }
  
      public BufferStrategy getBufferStrategy () {
          return canvas.getBufferStrategy();
      }
  
      public void setScreen(Dimension dim) {
          int width = (int) dim.getWidth();
          int height = (int) dim.getHeight();
          this.dm = dim;
  
          container.setPreferredSize(dm);
          gui.setPreferredSize(dm);
          surface.setPreferredSize(dm);
  
          canvas.setBounds(0,0,width,height);
          if(gSettings == null) {
              gui.setBounds((int) ((dm.getWidth() - 200) / 2),
                      (int) ((dm.getHeight() - 200) / 2),
                      200,
                      200);
          }
          else {
              gui.setBounds((int) ((dm.getWidth() - gSettings.getWidth()) / 2),
                      (int) ((dm.getHeight() - gSettings.getHeight()) / 2),
                      gSettings.getWidth(),
                      gSettings.getHeight());
          }
          gui.setBackground(gSettings);
          surface.setBounds(0,0,width,height);
          container.setBounds(0,0,width,height);
          frame.validate();
      }
  
      public void setFullScreen(DisplayMode display) {
          setScreen( Toolkit.getDefaultToolkit().getScreenSize());
          frame.setUndecorated(true);
          vc.setFullScreenWindow(frame);
          if(dm != null &amp;&amp; vc.isDisplayChangeSupported()) {
              try {
                  vc.setDisplayMode(display);
              }
              catch(Exception e) {}
          }   
          frame.validate();
      }
  
      private void init_resources() {
          try {
              gSettings = ImageIO.read(getClass().getResourceAsStream("/gui/settingsWindow.png"));
          }
          catch(Exception e)
          {
              System.out.print("Failed to load resources");
              System.out.println();
          }       
      }
  
  }
  
  public class MyPanel extends JPanel {
  
      BufferedImage img = null;
      public MyPanel() {
          super();
          setOpaque(false);
      }
  
      public void setBackground(BufferedImage img) {
          this.img = img;
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if(img != null) {
              Graphics2D g2d = (Graphics2D) g;
              g2d.drawImage(img, 0, 0, null);
          }
      }
  }
  }
  </code></pre>