BufferedImage dissappears

I am working on a rendering engine that can use either OpenGL or Java2D depending on the machine. I am running into trouble with the Java2D version. In my canvas implementation I have a method that reads in an XML file and draws the contents to a BufferedImage, then I draw the image to a java.awt.Canvas. Then I add that canvas to a JFrame. The trouble I have is that the image dissappears! I put a sleep after the call to render() and it is visible only as long as the thread is asleep! Here is my JFrame code and my render() code! Does anyone have any ideas?


public class FactoryTest extends JFrame
{
	static CanvasFactory cf;
	private Canvas canvas;
	
	public static void main(String[] args)
	{
		FactoryTest app = new FactoryTest();
		app.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				cleanup();
				System.exit(0);
			}
		});
		app.setSize(800, 800);
		app.setResizable(false);
		app.setVisible(true);
	}

	public FactoryTest()
	{
		super("Canvas Factory Test");
		Container box = getContentPane();
		box.setLayout(new BorderLayout());

		try
		{
			cf = CanvasFactory.newInstance();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}		
		cf.createLayer("conus_sector_high.xml");
		canvas = (Canvas) cf.getCanvas();
		box.add(canvas, BorderLayout.CENTER);
	}
	
	public void paint(Graphics g)
	{
		System.out.println("Paint");
		cf.render();
		try
		{
			Thread.sleep(1000);
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	
	private static void cleanup()
	{
		cf.destroy();
	}
}


public void render()
{
	System.out.println("Render");
	Graphics g = canvas.getGraphics();
	for(J2DLayer layer : layers)
	{
		g.drawImage(layer.layerImg, 0, 0, null);
		g.drawRect(0,0,layer.layerImg.getWidth(), layer.layerImg.getHeight());
		System.out.println("g.drawRect(0,0," + layer.layerImg.getWidth() + ", " + layer.layerImg.getHeight() + ");");
	}
}

Perhaps the problem is that you’re trying to render actively? Try subclassing the canvas and do the rendering in its paint(Graphics) method. It will be called implicitly from the event thread whenever the canvas needs drawing.

OR turn off active rendering.

There is some info on that here…

http://java.sun.com/docs/books/tutorial/extra/fullscreen/

In the render method you are drawing the image then drawing a rect over it?
Unless you are doing something special, doesnt that black out the image?

I think I’ve figured it out! I extended the canvas as suggested by mudman! Thanks for the replies!

@Tzan, I put that there to see if it was haiving trouble with the image, or drawing in general! :wink:

Thanks!!!