Mongooseman 0 Light Poster

This question is more from curiosity than necessity. I've been playing with the Java2D API, and I noticed a weird quirk with using Active Rendering. The Graphics 2D object doesn't appear to do anything the first time I use it. My code basically looks like this (I know it isn't the proper way to do drawing; I'm just testing stuff with some code I threw together in a few minutes):

public class GraphicsTest
{
  private JFrame window;
  public void init()
  {
    window = new JFrame("test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setPreferredSize(new Dimension(800, 600));
    window.setIgnoreRepaint(true);
    window.pack();
    window.setLocationRelativeTo(null);
    window.setVisible(true);
  }

  public void render(int x, int y)
  {
    Graphics2D g = window.getGraphics();
    g.drawLine(0, 0, x, y);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
  }

  public static void main(String args[])
  {
    GraphicsTest gt = new GraphicsTest();
    gt.init();
    gt.render(100, 100);
  }
}

It displays the window, but it's blank. However, if I add another call to gt.render() right after the first one in main, the new line shows up, but the old one does not. If I add subsequent calls, the new ones shows up, but only the first one does not.

I've looked through the Java2D tutorials and API doc, but I haven't found an answer to the question, "Why does it not draw on the first call?" Does anyone know the reason?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.