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.
我正在用java开发一个公平的医院模拟游戏。现在,我的痛苦方法开始看起来有点大,我需要一种方法将它分成不同的部分...我有一个想法,但我不确定这是否是最好的方法。它首先绘制草,然后是医院建筑,然后是任何建筑物,然后是人,然后是建筑物时的任何建筑物预览。草和医院建筑不会改变,所以我只需要画一次。建筑物本身不会经常变化,只有在建造新建筑物时才会变化。
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.
我仍然是java的新手,并且在旅途中学习。
Thanks in advance.
提前致谢。
Rel
3 个解决方案
#1
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.
另一个想法是为必须在屏幕上绘制的所有项目创建超类或接口。让cvall这个类ScreenObject。然后,您可以在ScreenObject类中指定draw(Graphics2d g)方法。接下来,必须绘制的每个对象都实现draw()方法,并且只关注绘制本身。您甚至可以考虑创建一个变量来确定是否应该运行此绘制方法。
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.
在绘制屏幕的主类中,您可以引用ArrayList中的所有ScreenObject,而paint()方法将简单地遍历每个对象上的调用draw()。
#2
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.)
我假设从你的描述中你的场景被分割成了瓷砖。保留一组布尔值是一种很好的方法,可以跟踪下次更新时需要重绘的区块。在某些情况下,LinkedList可能会表现得更好一些。 (我正在考虑一个生命游戏模拟,其中有大量的瓷砖要重绘,你需要检查每个邻居,所以你可能不需要走这条路。)
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.
在没有看到你的代码的情况下,我无法就分割你的绘画方法给出非常具体的建议。我可以告诉你,在精灵动画中,每个精灵对象通常都有自己的绘制方法,它将主要的Graphics对象(或者更可能是缓冲区)作为参数。由于精灵应该知道自己的图像和位置,因此它可以将自己绘制到主图像中。然后,您的绘制方法可以循环遍历需要重绘的精灵列表并调用其绘制方法。
You might look to Killer Game Programming in Java for more detailed information.
您可以查看Java中的Killer Game Programming以获取更多详细信息。
#3
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.
好吧,我不是一个真正的编程专家,但是分开我的绘制方法我总是只做了一个新方法,它接受一个Graphics对象并从paint中调用它,它总是帮助我保持我的代码有条理但我从来没有过这样的大项目听起来你正在努力,所以它可能不适合你的情况。
#1
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.
另一个想法是为必须在屏幕上绘制的所有项目创建超类或接口。让cvall这个类ScreenObject。然后,您可以在ScreenObject类中指定draw(Graphics2d g)方法。接下来,必须绘制的每个对象都实现draw()方法,并且只关注绘制本身。您甚至可以考虑创建一个变量来确定是否应该运行此绘制方法。
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.
在绘制屏幕的主类中,您可以引用ArrayList中的所有ScreenObject,而paint()方法将简单地遍历每个对象上的调用draw()。
#2
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.)
我假设从你的描述中你的场景被分割成了瓷砖。保留一组布尔值是一种很好的方法,可以跟踪下次更新时需要重绘的区块。在某些情况下,LinkedList可能会表现得更好一些。 (我正在考虑一个生命游戏模拟,其中有大量的瓷砖要重绘,你需要检查每个邻居,所以你可能不需要走这条路。)
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.
在没有看到你的代码的情况下,我无法就分割你的绘画方法给出非常具体的建议。我可以告诉你,在精灵动画中,每个精灵对象通常都有自己的绘制方法,它将主要的Graphics对象(或者更可能是缓冲区)作为参数。由于精灵应该知道自己的图像和位置,因此它可以将自己绘制到主图像中。然后,您的绘制方法可以循环遍历需要重绘的精灵列表并调用其绘制方法。
You might look to Killer Game Programming in Java for more detailed information.
您可以查看Java中的Killer Game Programming以获取更多详细信息。
#3
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.
好吧,我不是一个真正的编程专家,但是分开我的绘制方法我总是只做了一个新方法,它接受一个Graphics对象并从paint中调用它,它总是帮助我保持我的代码有条理但我从来没有过这样的大项目听起来你正在努力,所以它可能不适合你的情况。