java 我应该用什么来显示游戏图形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6247025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:00:24  来源:igfitidea点击:

What should I use to display game graphics?

javagraphics

提问by Will

I have a system in place for a game yet I don't know what I should use to display it. I am making a vertical shooter game and I have written the methods for all the classes controlling enemies and players, but I have not idea how to efficiently display the game. I was thinking a Canvas, that would repaint every frame, but is that really the most efficient method?

我有一个游戏系统,但我不知道我应该用什么来显示它。我正在制作一个垂直射击游戏,我已经为所有控制敌人和玩家的类编写了方法,但我不知道如何有效地显示游戏。我在想一个 Canvas,它会重新绘制每一帧,但这真的是最有效的方法吗?

Important details:

重要细节:

  1. ideal framerate: 25fps
  2. It is a 2d game
  3. There are anywhere between 25-100 objects on the screen at any one time, all of which are moving
  4. All objects being displayed are images, all in PNG format
  5. The window is 640px by 480px
  6. Right now all the images are loaded as BufferedImage, although I could easily change this
  1. 理想帧率:25fps
  2. 这是一个2d游戏
  3. 屏幕上同时有 25-100 个物体,所有物体都在移动
  4. 显示的所有对象均为图像,均为 PNG 格式
  5. 窗口为 640 像素 x 480 像素
  6. 现在所有的图像都作为 BufferedImage 加载,虽然我可以很容易地改变它

7. I need a coordinate plane. This is the only fundamental part that cannot be changed without completely restructuring my code.

7. 我需要一个坐标平面。这是唯一不能在不完全重构我的代码的情况下更改的基本部分。

Most importantly the way I have everything set up, every frame all of the objects move and interact in a coordinate plane I devised (deals with collision detection and movement, no graphical component), then everything should get painted to the screen, simply by going through the ArrayLists which keep track of all moving objects and painting them one by one.

最重要的是我设置好所有东西的方式,每一帧所有对象都在我设计的坐标平面中移动和交互(处理碰撞检测和移动,没有图形组件),然后一切都应该被绘制到屏幕上,只需通过通过 ArrayLists 跟踪所有移动的对象并一一绘制它们。

采纳答案by trashgod

If Swing is acceptable, JPanelis double-buffered by default, and a javax.swing.Timerwith a period of 40 ms will give you ~25 Hz updates. This exampleshows the basic approach, while this exampleshows a number of images in motion.

如果 Swing 是可以接受的,JPanel默认情况下是双缓冲的,javax.swing.Timer周期为 40 ms 的 a 将为您提供 ~25 Hz 的更新。此示例显示了基本方法,而此示例显示了许多运动图像。

I need a coordinate plane.

我需要一个坐标平面。

It's not unusual to have the the model and view use different coordinates; all that's needed are functions to map one system to the other. In this game, the view relies on four methodsthat map tiles to pixels and vice-versa. The same approach is outlined herefor map tiles.

模型和视图使用不同的坐标并不罕见;所需要的只是将一个系统映射到另一个系统的函数。在这个游戏中,视图依赖于将图块映射到像素的四种方法,反之亦然。此处为地图图块概述相同的方法。

回答by Crollster

You have a number of options available to you:

您有多种选择:

Firstly, you could use one of the existing Java game frameworks:

首先,您可以使用现有的 Java 游戏框架之一:

  1. JMonkeyEngine (http://jmonkeyengine.com/)
  2. Slick (http://slick.cokeandcode.com/index.php)
  1. JMonkeyEngine (http://jmonkeyengine.com/)
  2. 光滑 (http://slick.cokeandcode.com/index.php)

(Slick is aimed at 2D graphics, while JMonkey is aimed at 3D and uses OpenGL - While I have looked into their use, I've not actually used them myself)

(Slick 的目标是 2D 图形,而 JMonkey 的目标是 3D 并使用 OpenGL——虽然我研究了它们的使用,但我自己并没有实际使用它们)

Alternatively you can code everything yourself. From the sounds of things this is your first (graphical) game, so you may want to read up on a technique known as double buffering, whereby you write each frame off-screen and the just paint the whole thing to screen, as this can lead to smoother animation.

或者,您可以自己编写所有代码。从事物的声音来看,这是您的第一个(图形)游戏,因此您可能想了解一种称为双缓冲的技术,即您在屏幕外写入每一帧,然后将整个内容绘制到屏幕上,因为这可以导致更流畅的动画。

To get you into games development a bit more, I would highly recommend reading this site, Killer Game Programming in Javaby Dr Andrew Davison, as he gives some good pointers, and also provides a good progressive learning path for new game developers, and moving them into 2D and then 3D development.

为了让您更多地了解游戏开发,我强烈建议您阅读此网站,Andrew Davison 博士的Java 杀手游戏编程,因为他提供了一些很好的指导,并且还为新游戏开发人员提供了一个良好的渐进式学习路径,并且将它们转化为 2D,然后再进行 3D 开发。

HTH

HTH

回答by rHymans

The answer really depends on whether this game is 2D or 3D.

答案实际上取决于这款游戏是 2D 还是 3D。

If your game is 2D, an easy way to do what you want is to use Java's own 2D Graphics API. A good tutorial to start you off (at least in my opinion) can be found at The Java Tutorials. In my experience I have found that Java's graphics API is easy to learn, and is more efficient than one might expect. The basic technique is to have your game code keep track of the positions of all your objects, then translate those coordinates into screen coordinates and display appropriate images at those locations. I made a (very, very simple) game in Java once, and this is the method I used.

如果您的游戏是 2D 游戏,一个简单的方法就是使用 Java 自己的 2D 图形 API。可以在The Java Tutorials找到一个很好的入门教程(至少在我看来)。根据我的经验,我发现 Java 的图形 API 很容易学习,而且比人们想象的更高效。基本技术是让您的游戏代码跟踪所有对象的位置,然后将这些坐标转换为屏幕坐标并在这些位置显示适当的图像。我曾经用 Java 制作了一个(非常非常简单的)游戏,这就是我使用的方法。

If your game is in 3D, OpenGL is definetly the way to go. I have only limited experience with OpenGL, so I'm not sure how easy the Java bindings are to work with. In general, 3D programming is a massive topic, so if this is a first-time project or you aren't prepared for a major time investment, I would attempt to find a good game framework to use or make the game 2D. If you are interested in OpenGL, or 3D programming in general, a quick Google turned up the JOGLproject. I would recommend investigating JOGL as a way to access the OpenGL API from within Java code, but for actually learning OpenGL I recommend The OpenGL SuperBible(5th edition), commonly known as "The Blue Book". The code examples are all in C++, but for the OpenGL functions it could possibly be just a matter of using a wrapper library. For example:

如果您的游戏是 3D 游戏,那么 OpenGL 无疑是最佳选择。我对 OpenGL 的经验有限,所以我不确定 Java 绑定使用起来有多容易。一般来说,3D 编程是一个庞大的话题,所以如果这是一个第一次项目或者你没有准备好投入大量时间,我会尝试找到一个好的游戏框架来使用或制作 2D 游戏。如果您对 OpenGL 或一般的 3D 编程感兴趣,可以通过 Google 快速搜索JOGL项目。我建议研究 JOGL 作为从 Java 代码中访问 OpenGL API 的一种方式,但对于实际学习 OpenGL,我建议使用The OpenGL SuperBible(5th edition),俗称“蓝皮书”。代码示例都是用 C++ 编写的,但对于 OpenGL 函数,它可能只是使用包装库的问题。例如:

glDrawElements(...);

May become:

可能会变成:

JavaGLWrapperObject.glDrawElements(...);

Unfortunately I can't give concrete examples because I haven't used OpenGL with a Java program, but the above example very coarsely approximates how OpenGL ES is used on the Android platform.

不幸的是我不能给出具体的例子,因为我没有在 Java 程序中使用 OpenGL,但是上面的例子非常粗略地近似了 OpenGL ES 在 Android 平台上的使用方式。

As far as performance... Java's API comes with a non-trivial ammount of overhead, but I could see it doing alright for your purposes. You may have to put a little more effort into making your algorithms efficient, and your game may not run as well on less-capable hardware. If you do decide to go the OpenGL route, it will almost certainly be muchfaster (again, depending on how efficient your algorithms are), but is correspondingly muchharder to learn and get started with. Certainly doable, but it will be a challenge.

就性能而言……Java 的 API 带来了大量的开销,但我认为它可以满足您的目的。您可能需要付出更多努力来提高算法效率,而您的游戏可能无法在功能较弱的硬件上运行良好。如果你决定去OpenGL的路线,它几乎肯定会快(再次,这取决于你的算法如何高效的),但也相应更难学习并开始使用。当然可行,但这将是一个挑战。

回答by Jason Rogers

Canvas will quickly be too slow for 25+ fps targeted.

对于 25+ fps 的目标,画布很快就会太慢。

the number of object is irrelevant, what is is how complex they are.

对象的数量无关紧要,重要的是它们有多复杂。

if it is 100 images will take nothing compared to 1 3D model of Avatar for example.

例如,如果是 100 张图像,则与 Avatar 的 1 个 3D 模型相比什么都不用做。

in Java you can use either Opengl or Java3D

在 Java 中,您可以使用 Opengl 或 Java3D

I would tend to go with Opengl as a personel choise

我倾向于使用 Opengl 作为个人选择