double buffering woes

Can anyone tell me why this ain’t double buffering?

public void paint(Graphics g) {
       
        Graphics2D g2d = (Graphics2D) screen.getGraphics();
       
        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, 640,480);
   
        for(int i = 0; i < stars.length; i++){
                g2d.setColor(new Color(stars[i].r, stars[i].g, stars[i].b));
                g2d.fillRect(stars[i].x, stars[i].y, (int) stars[i].size, (int) stars[i].size);
        }
       
        g.drawImage(screen, 0, 0, null);
    }

It looks like it is… Your drawing to an image, and then drawing that image to the screen. At least that’s what it looks like to me. Why are you using paint() instead of paintComponent() though?

Perhaps he is using Canvas instead of Component.

Isn’t double buffering only applied to Graphics object that you get from the object that you’re using to draw?

True I didn’t think about the fact that he could be using AWT, my bad.

You mean when you use setDoubleBuffered()? Yea I think it only applies to graphics objects you get from the component, not to images and what not. But that’s not what CyanPrime is doing. He is drawing to an image, then drawing that image onto his component. That sounds like double buffering to me.

Did you override update(Graphics g)?


public void update(Graphics g) {
  paint(g);
}

What’s the point of that? update(Graphics) already calls paint(Graphics).

And it also calls other things, which you are avoiding.

What other things would update(Graphics) be calling? O_o

Canvas clears itself in update() before calling paint().

Oh yeah! Forgot about that :stuck_out_tongue: