title: How to scale a Graphics2D image with transparent padding
body: <p>I'm trying to write a small method which takes a <code>BufferedImage</code> image and a new width and height and scales the image keeping the aspect ratio by adding transparent border to the left/right or top/bottom depending on the image.  The scaling works fine but for the life of me I can't get the border to be transparent.</p>
  
  <p>So far I have the <a href="http://pastebin.com/eFarSKqi" rel="nofollow">following code posted on pastebin.com</a> which does the scaling well.</p>
  
  <p>I've read a lot of manuals and other SO questions to no avail.   I've tried numerous permutations of fills, composite types, image types, etc..  Sometimes I get a blue background, sometimes white but it never seems to be transparent.</p>
  
  <pre><code>BufferedImage newImg = new BufferedImage(newWidth, newHeight, img.getType());
  Graphics2D g = newImg.createGraphics();
  g.setColor(Color.WHITE);
  g.fillRect(0, 0, newWidth, newHeight);
  g.drawImage(img, x, y, x + scaledWidth, y + scaledHeight, 0, 0,
      currentWidth, currentHeight, Color.WHITE, null);
  g.dispose();
  return newImg;
  </code></pre>
  
  <p>Any idea what Graphics2D call I need to make to have the <code>Color.WHITE</code> background be transparent <em>and</em> draw the old image over the new?  Thanks for any help.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>It turned out that the problem I was having was that I was trying to generate a transparent color with a JPEG image.  JPEGs do not support transparency.  Duh.</p>