I noticed some interesting difference between fillRect and clearRect when I want to initialize a BufferedImage with a half-transparent background color:
using fillRect:
Color someHalfTransparentColor = new Color(Integer.parseInt("77affe07", 16), true);
BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = bi.createGraphics();
g.setColor(someHalfTransparentColor);
g.fillRect(0, 0, 10, 10);
g.dispose();
System.out.println(Integer.toString(bi.getRGB(0, 0), 16));
// WORKS NOT AS EXPECTED: 77b0ff06 != 77affe07
using clearRect:
Color someHalfTransparentColor = new Color(Integer.parseInt("77affe07", 16), true);
BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = bi.createGraphics();
g.setBackground(someHalfTransparentColor);
g.clearRect(0, 0, 10, 10);
g.dispose();
System.out.println(Integer.toString(bi.getRGB(0, 0), 16));
// WORKS AS EXPECTED: 77affe07 == 77affe07
It seems when using fillRect the color values are slightly changed. Can someone explain this behavior?