How to obtain a Graphics object and paint on it?

I’ve made a class called GFXManager. It contains all the methods that I would be using in my game, such as drawLine, drawHexagon, drawImage, drawBlurred, drawPixelated, etc… GFXManager contains two fields, Graphics g and Component parent. When I want to draw a line, for example, I actually call g.drawLine(…). I can call GFXManager’s methods from anywhere I want, I would make GFXManager’s instance public static final…
This all works fine, except that I cannot obtain the paren’t Graphics object. :-/ Let’s say the parent is a JPanel, then I would create the manager like gfx = new GFXManager(panel, panel.getGraphics()); This doesn’t return errors, however panel.getGraphics() will return null!

After looking around a bit all I managed to find out is that its impossible to obtain a component’s Graphics object. I am forced to use the paint() method, and get the graphics from there. But this would force me to redesign my entire game, dump the GFXManager class and perhaps have a very long paint() method with everything in it. This is just not a good thing to do. :-/

So is there a way for me to obtain the component’s Graphics object? If not, I figured there are two ways to solve this:
[]Have a queue of actions, and when I call drawBlurred(), for example, I add that action to the queue, and in the end I call, from the component’s paint() method, gfxmanager.render() which will do every action that’s in the queue until its empty.
I think this would be a slow and memory-costly solution, not to mention I’d have to invent message identifyers for all the possible actions. I would rather not do like this.
[
]Create a Graphics instance inside the GFXManager, draw everything on it, and in the JPanel’s paint() method I would write g = GFXManager.localG;
I’m not sure if this would work though…
After looking at it, Graphics is an abstract class, so I cannot really do this… >:(
The best possible solution would be to obtain the JPanel’s Graphics object…

So what should I do? ???

of course you can create a Graphics object yourself for let say a JPanel - no problem at all.
Simply use JPanel.getGraphics().

The only limitation you have is that the JPanel must be realized (=added to a visible Windows, JFrame or whatever, otherwise its not realized so there’s no Graphics you can paint on…)

lg Clemens

btw. don’t you think a SingleTon would better fit your needs for your GFManager-Class?

Did you have a look at BufferStrategy ? It is used for active rendering (you don’t rely on repaint events and use a classic “forever game loop”.

example :

In your game, you will paint on a Canvas (AWT), and use

canvas.createBufferStrategy(2);
while (!endOfGame) {
BufferStrategy bs = canvas.getBufferStrategy();
Graphics g = bs.getGraphics();
// insert game logic here
bs.show();
}

there are some tutorials on this theme a google search should help you.

Lilian