Reputation: 105
I am trying to create a BufferedImage instance which contains a rounded rectangle of a certain colour and is transparent everywhere else.
I am using the following code to create the image
private BufferedImage createChromImage() {
BufferedImage I = new BufferedImage(350, 20, ColorSpace.TYPE_RGB);
Graphics2D g2 = I.createGraphics();
g2.setPaint(new GradientPaint(0, 0, Color.DARK_GRAY, 100,
100, Color.BLUE, false));
g2.fillRoundRect(0, 0, 350, 20, 10, 10);
return I;
}
I end up with a rounded rectangle on a black background, is there a way in which I can get it on a transparent background. I suspect it will require a different ColorSpace setting, but I not sure which.. any help is much appreciated.
Upvotes: 0
Views: 141
Reputation: 20862
You can't have a transparent background in an image that does not support transparency. RGB is a 24-bit image with no transparency. Instead, you want to use BufferedImage.TYPE_INT_ARGB as the argument to BufferedImage's constructor: that will give you an alpha channel to play with, which will allow transparency.
Upvotes: 2