title: Component with transparent background to BufferedImage?
body: <p>I have a component extends JPanel. It saves itself as bufferedimage on every call of paintComponent method. Component is not completely transparent, only its background. Problem is background is not transparent. I am using setOpaque(false);</p>
  
  <p>Here is my relevant code;</p>
  
  <pre><code>private BufferedImage bufImage = null;
  
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
  
      // if first time call
      if (bufImage == null) {
          int w = this.getWidth();
          int h = this.getHeight();
          bufImage = (BufferedImage)this.createImage(w, h);
      }
  
      g2.drawImage(bufImage, null, 0, 0);
  
      // draw sth
      g2.draw(sth);
  }
  </code></pre>
  
  <p>--</p>
  
  <p>I also tried </p>
  
  <pre><code>bufImage =  new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>bufImage = (BufferedImage)this.createImage(w, h);
  </code></pre>
  
  <p>When i do that; background transperancy works but i can only draw with white color. I have no idea what causes that.</p>
  
  <p>Note:
  I used that code to check if it is working;</p>
  
  <pre><code>File outputfile = new File("saved.png");
  ImageIO.write(bufImage, "png", outputfile);
  </code></pre>
  
  <p>saved.png had transparent background but drawings were only white.</p>
  
  <hr>
  
  <p>This is the component, only lets drawing rectangle with mouse;</p>
  
  <pre><code>class PaintPanel extends JPanel implements MouseListener, MouseMotionListener {
      private BufferedImage _bufImage = null;
      private boolean dragging = false;
      private Point _start = null, _end = null;
  
      public PaintPanel() {
          setOpaque(false);
          this.addMouseListener(this);
          this.addMouseMotionListener(this);
      }
  
      @Override
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
  
          if (_bufImage == null) {
              int w = this.getWidth();
              int h = this.getHeight();
              _bufImage =  new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
              //_bufImage = (BufferedImage)this.createImage(w, h);
          }
  
          g2.drawImage(_bufImage, null, 0, 0);
  
          if (dragging) {
              drawCurrentShape(g2);
          }
      }
  
      private void drawCurrentShape(Graphics2D g2) {
          int startx = (int) _start.getX();
          int starty = (int) _start.getY();
          int stopx = (int) _end.getX();
          int stopy = (int) _end.getY();
  
          int width = Math.abs(startx - stopx);
          int height = Math.abs(starty - stopy);
          int x = startx, y = starty;
          if(x &gt; stopx)
              x = stopx;
          if(y &gt; stopy)
              y = stopy;
  
          Rectangle r = new Rectangle(x, y, width, height);
          g2.draw(r);
      }
  
      public void mousePressed(MouseEvent e) {
          dragging = true;       
          _start = e.getPoint();
          _end   = _start;
      }
  
      public void mouseDragged(MouseEvent e) {
          _end = e.getPoint();
          this.repaint();
      }
  
      public void mouseReleased(MouseEvent e) {
          _end = e.getPoint();
          if (dragging) {
              dragging = false;
              drawCurrentShape(_bufImage.createGraphics());  
              this.repaint();
          }
      }
  
      public void mouseMoved  (MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited (MouseEvent e) {}
      public void mouseClicked(MouseEvent e) {}
  }
  </code></pre>