[java] drawing to the buffer dynamically

Started by
2 comments, last by m_wherrett 21 years, 10 months ago
With any applet I make, I have a GameManager class that performs all the displaying and double buffering. If I create something like a Sprite object for the player I need to ensure that that classes paint method is called by the GameManagers painting method, passing it the buffer and then retieving it. This means I have to specify from the very start what things are going to need drawing so I can put in the necessary paint calls for each and every object I implement. Is there a way of getting something to paint itself without having the main class tell it to do so? That way I can create things, like explosions / particles etc, have them paint themselves and let them deal with it. Otherwise I need a mass of paint calls for everything that would ever exist and and then filter out those that aren''t needed at the time. I hope that makes sense. Any suggestions?
Advertisement
It sounds to me that you have an Image or BufferedImage, maybe called backbuffer and you are passing the image to your objects so they can draw themselves on it then you want to pass the image back. That is a bit obtuse. What you want to do is pass the Graphics of the image to the objects then they can draw on it (the image) and it (the image) is not passed anywhere. You would use for the above:

Graphics backbuffergc = backbuffer.getGraphics();

then in your game managers paint method you would use something like

spritemanager.paint(backbuffergc);

Graphics is like a Device Context in windows programming if your familiar with that. I hope this helps.



[edited by - nonnus29 on June 3, 2002 8:38:29 PM]
i think there is another problem in the question: how to
deal with lots of dynamic (at runtime) created sprite-objects.

one solution is to make an interface for all your sprites. this
interface (e.g. named Entity) would declare a method
void paint(Graphics g). then all your sprite-classes must
implement this interface and of course define this method.

now at runtime you can create whatever specific sprite-object you want to and store it in a list (array, vector, etc.).
your gamemanager can traverse this list, cast every entry to Entity and call its paint-method.

hope this helps


[edited by - Heliosphere on June 4, 2002 3:05:37 AM]
Nice one people.

I take my hat off to you both. Well written answers that should hopefully improve my code.

cheers.

This topic is closed to new replies.

Advertisement