Saving an image froma a Canvas

Hello,

I'm a beginner. I've created an application wich uses a native procedure, myFunc(), wich paints an image over a Canvas.

But now I want to create an Image from each frame that is painted over this Canvas. The paint method is like this one:

publicvoid paint(Graphics g){

Rectangle r = this.getBounds();

Image image = this.createImage(r.width, r.height);

Graphics g = image.getGraphics();

this.paint(g);

try{

ImageIO.write((RenderedImage)image,"png",new File(name));

}catch(IOException ioe){

// ...

}

}

It works, but it just saves a gray image, such if no image were rendered to the Canvas. What's going wrong? Thank you

[1102 byte] By [neztola] at [2007-11-26 18:12:29]
# 1
You're running in an infinite loop. Your paint method calls itself.
Jasprea at 2007-7-9 5:45:20 > top of Java-index,Desktop,Core GUI APIs...
# 2

Sorry, I've changed some parts of the code for making it more comprenhensive. The correct one is:

public void paint(Graphics g) {

Rectangle r = this.getBounds();

Image image = this.createImage(r.width, r.height);

Graphics g = image.getGraphics();

this.myFunc(g);

try {

ImageIO.write((RenderedImage)image, "png", new File(name));

} catch(IOException ioe) {

// ...

}

}

myFunc(g) is the native funcion

neztola at 2007-7-9 5:45:20 > top of Java-index,Desktop,Core GUI APIs...
# 3
Sorry, we cannot possibly know what is happening in your native code so there's no way to for us to help you. Not to mention that the above code would not even compile because you're redeclaring 'g'.Message was edited by: Jaspre
Jasprea at 2007-7-9 5:45:20 > top of Java-index,Desktop,Core GUI APIs...