Java swing小型2D游戏:如何建模视图?
在小型 java swing 2D 游戏中,创建棋盘视图的最佳解决方案是什么?
- 使用棋盘组件并立即对其和棋盘格的每个方块进行自定义绘制?
- 使用板的组件并创建另一个组件来对正方形进行建模,并使用其自己的绘制组件仅对正方形进行工作。使用布局将每个 Square 实例放置在板上?
我知道这是主观的,我不想为此争论。我只需要一些线索来弄清楚我应该走哪条路。我开始了一个副业项目,并使用了 1),但感觉有什么问题。
In a small java swing 2D game, what is the best solution for creating the board view?
- Use a component for the board and custom paint it and each square of the checker at once?
- Use a component for the board and create another component modelizing the square with its own paint component doing the job for the square only. Use a layout to place each Square instance in the board?
I know this is subjective and I don't want a fight about it. I just need some clues to figure myself which way I should go. I've begun a side project and I've using 1), with the feeling that there is something wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用一个绘图面板/容器即可完成所有操作。
考虑一个基于图块的游戏。使用你的第二个解决方案,每个图块都将是一个对象,内存会飙升,游戏会慢得像爬行一样。
使用第一个选项,您可以灵活选择。你画你想要的东西,你有面板坐标,所以一切都与此相关。使用基于图块的示例,您会知道面板的高度和宽度,根据需要绘制正方形并在 X 和 Y 坐标中增量。
GUI 框架(例如 Swing 或 .NET 的 Winforms)价格昂贵,因为它们有很多游戏不需要的其他东西。因此,要回答您的问题,请选择选项一,而不是为板上的每个检查器使用面板。
将第二种方法与第一种方法结合使用的一个很好的解决方案是 Flyweight 设计模式。您仍然可以使用 OO 对象,但您所拥有的数量只是平常的一小部分。一探究竟。
Just go with one drawing panel/container for everything.
Consider a tile based game. Using your second solution each tile would be an object, memory would skyrocket and the game would slow to a crawl.
Using the first option you are flexiable. You draw what you want, you have the panel coordinates so everything is relative to this. Using the tile based example you'd know the height and width of the panel, draw your square and increment in the X and Y coordinates as appropriate.
GUI frameworks such as Swing or .NET's Winforms are expensive as they have a whole lot of other stuff that a game won't need. So to answer your question, go with option one, rather than say using a panel for every checker on your board.
One nice solution to using the second method, in combination with the first method is the Flyweight Design Pattern. You still get to use OO objects, but you'll have a fraction of the amount you normally would. Check it out.
和 Finglas 一样,我倾向于你的第一种方法。这个基于图块的游戏就是一个具体的例子。相比之下,这款基于组件的游戏使用
GridLayout
JToggleButton 的。前者稍微复杂一些,但即使在大屏幕上也能很好地播放;后者更简单,但对于合理的游戏来说已经足够了。您可能需要尝试游戏的几何形状和逻辑来决定更喜欢哪个。Like Finglas, I lean toward your first approach. This tile-based game is a concrete example. In contrast, this component-based game uses a
GridLayout
ofJToggleButton
. The former is somewhat more complex but plays well even on large screens; the latter is simpler yet sufficient for reasonable games. You may have to experiment with your game's geometry and logic to decide which to prefer.