Graphic to BufferedImage - How to?

Hi,I want to pass a graphic to a buffereImage to save it to file. How can i do that?Cheers,Nuno
[123 byte] By [lpxa] at [2007-11-26 13:45:05]
# 1
You don't pass graphics to BufferedImage.You use graphics to draw on a BufferedImage.BufferedImage image...image.createGraphics().drawLine(...);Will draw a line on image.
Rodney_McKaya at 2007-7-8 1:19:24 > top of Java-index,Security,Cryptography...
# 2

Cool!!

My processing is over an array of int.

int[] pixels

Is there anyway to draw directly above this kind of data?

Another thing. I have done the following to draw in my out buffered image, after the processing:

BufferedImage out = new BufferedImage(imgCols, imgRows, BufferedImage.TYPE_INT_RGB);

out.setRGB(0, 0, imgCols, imgRows, pixels, 0, imgCols);

out.createGraphics().setColor(Color.GREEN);

out.createGraphics().drawLine(0,0,imgCols,imgRows);

In the file i save after this, the line color is white and i choose green.

Do you know why?

Many thx,

Nuno

lpxa at 2007-7-8 1:19:24 > top of Java-index,Security,Cryptography...
# 3

The simplest way to do that is create a BufferedImage like you did and then take the DataBuffer of the BufferedImage like this:

int[] data = ((DataBufferInt) out.getRaster().getDataBuffer()).getData();

And copy your pixels to it:

System.arraycopy(pixels, 0, data, 0, pixels.length);

You have to use the same Graphics object after you set the color.

createGraphics creates a new Graphics object every time:

Graphics g = out.createGraphics();

g.setColor(Color.GREEN);

g.drawLine(0,0,imgCols,imgRows);

Rodney_McKaya at 2007-7-8 1:19:24 > top of Java-index,Security,Cryptography...