Jump to content

How to set the background color of an output image file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
cliveceps

cliveceps

    Newbie

  • Members
  • Pip
  • 6 posts
I have an icon that I want to write to an ouput file. The icon is a circle - specifically, it is JOptionPane.errorIcon. So I turn it into a BufferedImage like this:

int w = icon.getIconWidth();
int h = icon.getIconHeight();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage buffImage = gc.createCompatibleImage(w, h);

//Now I have to paint it

Graphics2D g = buffImage.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();

//Now write to file

File file = new File("C:\\location");
try{
ImageIO.write(buffImage, "bmp", file);
}catch(Exception e){
e.printStackTrace();}

The problem is that I turned my icon into a rectangle, and now it has a black background around the circle (this is bad). I either just want the circle, or I want to paint the background grey instead of black, how can I do this?

I have tried a couple different things such as setBackground(color) but that doesn't seem to work.

#2
cliveceps

cliveceps

    Newbie

  • Members
  • Pip
  • 6 posts
I figured it out by added a gray rectangle to the background, then adding my circle on top of it.