views:

436

answers:

3

I'm developing a fair sized hospital simulation game in java. Right now, my pain method is starting to look a little big, and I need a way to split it up into different sections... I have an idea, but I'm not sure if this is the best way. It starts by painting the grass, then the hospital building, then any buildings, then people, then any building previews when building. The grass and hospital building will not change, so I only need to paint this once. The buildings themselves won't change very often, only when new ones are built.

I was thinking, use boolean values to determine which sections need repainting? Ideal, id like to be able to split up the paint method, and then call each one when needed, but I'm unsure how to physically split it up.

I am still quite new to java, and learning on the go.

Thanks in advance.

Rel

+1  A: 

I'm assuming from your description that your scene is split up into tiles. Keeping an array of booleans is a good way to keep track of which tiles need redrawn on the next update. A LinkedList might perform a little better in some situations. (I'm thinking of a Game of Life simulation where there are tons of tiles to redraw and you need to check each neighbor, so you may not need to go this route.)

Without seeing your code I can't give very specific advice on splitting up your paint method. I can tell you that in sprite animations, each sprite object typically has its own draw method that takes the main Graphics object (or more likely a buffer) as a parameter. Since the sprite should know its own image and location, it can then draw itself into the main image. Your paint method can then just loop through your list of sprites that need to be redrawn and call their draw method.

You might look to Killer Game Programming in Java for more detailed information.

Bill the Lizard
THATS it. I planned to do pretty much that, but id forgotten! You assume correctly. Each sprite object should indeed have its own paint method. I'm unsure how to use sprites and animations as of yet, and im just working on getting the game basics atm, with simple swing graphics and simple movement animation. Book looks awesome thanks! :)
Relequestual
A: 

Well I am not really an expert at programming but to split up my paint method Ive always just made a new method that takes a Graphics object and call that from paint, it has always helped me to keep my code organized but I have never had a big project like it sounds you are working on so it might not work for your situation.

Nagrom_17
+2  A: 

Another idea is to create a super class or interface for all items that must be drawn on the screen. Lets cvall this class ScreenObject. You can then have a draw(Graphics2d g) method specified in the ScreenObject class. Next, each object that must be drawn implements the draw() method and is only concerned about drawing itself. You can even consider creating a variable that determines whether this draw method should be run at all.

In the main class that paints the screen you can have a reference to all ScreenObjects in an ArrayList and your paint() method will simply iterate over this calling draw() on each object.

Vincent Ramdhanie