How can I split the paint swing method in java?
I am developing a large size game simulator in java. Right now, my pain method is starting to look a little big and I need a way to split it into different sections ... I have an idea, but I'm not sure if this is the best way. It starts by drawing grass, then a hospital building, then any buildings, then people, and then any building preview during construction. The grass and hospital building will not change, so I only need to paint it. The buildings themselves won't change very often, only when new ones are built.
I was thinking, use booleans to determine which sections to repaint? Ideal, id to be able to split the drawing method and then call each one when needed, but I'm not sure how to physically split it.
I'm still pretty new to java and learning on the go.
Thanks in advance.
Rel
a source to share
Another idea is to create a superclass or interface for all the elements that need to be drawn on the screen. Lets you use this ScreenObject class. Then you can use the draw (Graphics2d g) method provided in the ScreenObject class. Then each object that needs to be drawn implements the draw () method and only concerns the drawing itself. You might even consider creating a variable that determines if this drawing method should be run at all.
In the main class that draws the screen, you can have a reference to all ScreenObjects in the ArrayList, and your paint () method will simply iterate over that draw () call for each object.
a source to share
I am assuming from your description that your scene is tiled. Saving an array of booleans is a good way to keep track of which chunks need to be redrawn on the next update. In some situations, LinkedList can improve slightly. (I'm thinking of simulating a Life of Life game where there are tons of tiles to redraw and you have to check every neighbor so you might not need to go that route.)
Without seeing my code, I can't provide very specific guidelines for splitting your draw method. I can tell you that in sprite animation, each sprite object usually has its own draw method, which carries the main Graphics object (or rather a buffer) as a parameter. Since the sprite needs to know its own image and location, it can then paint itself in the main image. Then your paint method can simply loop through the list of sprites to be redrawn and call their paint method.
You can watch Programming Killer Game in Java for more details.
a source to share
Well, I am not a programming expert, but I share my drawing method. I've always just created a new method that takes a Graphics object and calls this because of the paint, it always helped me organize my code, but I've never had a project as big as it seems, you are working so that it doesn't work for your situations.
a source to share