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() + ");");
}
}