renew graphics context

Hi,

In tutorials they always seem to draw graphics by doing getGraphics().drawimage, what would be wrong with getting the graphics context once at startup and use that one like this:

private Canvas drawArea;
private Graphics gfx;

in the init:
drawArea = new Canvas();
gfx = drawArea.getGraphics();

And then use gfx.drawImage in the paint routine instead of grabbing the context over and over? (I’ve never encountered any problems with using a fixed graphics context)…

I cant see a problem.

I dont encounter any problem neither, it’s just that all the tutorials out there seem to do getGraphics for each draw. Im curious if there’s any specific reason why they call getGraphics each time they draw an image. It looks to me that getting the graphics context one time at startup is better performance wise…

Just a guess… perhaps if something about the system changes, display depth for example. Or if the user drags your window to a second monitor, etc. Then maybe the Graphics object that is handed back from getGraphics() will be different.

Also if you are drawing to an image, and it gets accelerated automatically behind the scenes, maybe the graphics object changes… It seems to make sense that these are a few possible reasons that the graphics object might be different from one call to the next. Perhaps doing the getGraphics every time enables possible optimizations, should it be more efficient to return a Graphics object with different rendering characteristics…

I don’t know that any of these things actually do happen… I’m just throwing out ideas.

Are we talkling about active rendering is passive rendering.

I think in the passive case, where AWT is in charge, things can change out from under you…