用Java实现一个简单的游戏(图形相关)
我对 Java 游戏编程很陌生,尤其是在图形方面,因此我想寻求一些关于如何在 Java 中以图形方式实现以下游戏的建议。
游戏非常简单,它显示一个正方形,该正方形被进一步分成2x2的盒子,游戏的玩法是将总共44个筹码放入这4个盒子中,并且用户应该能够从其中拖放筹码盒子到另一个。
就是这样!我的问题:
- 是否有现成的库可以用来绘制由 4 个盒子和芯片组成的正方形?
- 如果 1) 的答案是否定的,那么我可以遵循任何教程来自己对它们进行编程吗?
- 如何以图形方式实现拖放部分?
非常感谢。
I am new to game programming in Java, especially on the graphics front, hence I would like to seek some advice on how to implement the following game graphically in Java.
The game is very simple, it displays a square which is further divided into a 2x2 boxes, and the game playing is to put a total of 44 chips into these 4 boxes, and the user should be able to drag and drop the chips from one box to another.
That's it! My questions:
- is there ready-made library I can use for drawing the square consists of the 4 boxes as well as the chips?
- if the answer to 1) is no, then is there any tutorial I can follow to program them myself?
- How to implement the drag and drop part graphically?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
芯片可以通过添加到 JLabel 的图标来表示。
正方形可以用 JPanel 来表示。
首先阅读 Swing 教程中有关如何使用图标。还有其他感兴趣的部分:如何使用面板、使用布局管理器、如何编写 MouseListener、可能是有关拖放的部分。
Chips can be represented by an Icon added to a JLabel.
Squares can be represented by a JPanel.
Start by reading the section from the Swing tutorial on How to Use Icons. There are other section of interest as well: How to Use Panels, Using Layout Managers, How to Write a MouseListener, The section on drag and drop maybe.
我将使用画布并覆盖油漆(图形g)并使用它绘制各种元素。然后您可以使用计时器或游戏循环调用 repaint()。
I would use a Canvas and override paint(Graphics g) and draw your various elements using that. Then you can call repaint() with a timer or a game loop.