What should I use to display game graphics?

I have a system for the game, but I do not know what I should use to display it. I am making a vertical shooter game, and I have written methods for all classes controlling enemies and players, but I do not know how to efficiently display the game. I was thinking of a Canvas that repainted every frame, but is this really the most efficient method?

Important details:

  • ideal frame rate: 25 frames per second
  • This is a 2d game
  • At any time, the screen can contain from 25 to 100 objects, all of which are moving.
  • All displayed objects are images, all in PNG format
  • 640-by-480-pixel window
  • Currently, all images are loading as BufferedImage, although I could easily change that.

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

The most important thing is that everything is set up for me, every frame all objects move and interact in the coordinate plane that I came up with (we are talking about collision detection and movement, without a graphical component), then everything should be painted on the screen, just going through ArrayLists, which track all moving objects and draw them one by one.

+7
source share
4 answers

If Swing is acceptable, JPanel has a double buffer by default, and javax.swing.Timer with a period of 40 ms will give you ~ 25 Hz updates. This example shows a basic approach, and this example shows the number of images in motion.

Application:

I need a coordinate plane.

Not surprisingly, the model and view use different coordinates; everything you need is a function to map one system to another. In this game , the presentation relies on four methods : displaying tiles in pixels and vice versa.

+2
source

You have several options available to you:

First, you can use one of the existing Java frameworks:

  • JMonkeyEngine (http://jmonkeyengine.com/)
  • Slick (http://slick.cokeandcode.com/index.php)

(Slick is aimed at 2D graphics, while JMonkey is aimed at 3D and uses OpenGL - although I studied their use, I myself did not use them myself)

Alternatively, you can encode everything yourself. Of the sounds of things, this is your first (graphic) game, so you can read a technique known as double buffering, in which you write every frame off-screen and just draw everything on the screen, as this can lead to smoother animations.

To improve my game development a bit, I would strongly recommend reading this site, “Programming Java Killers” by Dr Andrew Davison, as it provides some good pointers and also provides a good progressive learning path for new game developers and porting them to 2D, as well then into 3D development.

NTN

+2
source

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

If your game is 2D, an easy way to do what you want is to use the native Java 2D graphics API. A good tutorial to get you started (at least in my opinion) can be found in Java Tutorials . In my experience, I have found that Java APIs are easy to learn and more efficient than you might expect. The main way is for your game code to track the positions of all your objects, then translate these coordinates into screen coordinates and display the corresponding images in these places. I made a (very, very simple) Java game once, and this is the method I used.

If your game is in 3D, OpenGL is definitely the way to go. I have limited experience with OpenGL, so I'm not sure how easy it is to work with Java bindings. In general, 3D programming is a serious topic, so if this is the first project or you are not ready for a major investment of time, I would try to find a good gaming environment for using or creating a 2D game. If you are interested in OpenGL or 3D programming in general, fast Google has included the JOGL project. I would recommend exploring JOGL as a way to access the OpenGL API from Java code, but for learning OpenGL I recommend OpenGL SuperBible (5th Edition) , commonly known as the Blue Book. The code examples are all in C ++, but for OpenGL functions this may just be a matter of using a wrapper library. For example:

 glDrawElements(...); 

Can be:

 JavaGLWrapperObject.glDrawElements(...); 

Unfortunately, I cannot give specific examples because I did not use OpenGL with a Java program, but the above example very roughly approximates how OpenGL ES is used on the Android platform.

In terms of performance ... The Java API comes with a non-trivial amount of overhead, but I could see that this is suitable for your purposes. You may need to make more efforts to make your algorithms efficient, and your game may also not work on less capable hardware. If you decide to go the OpenGL route, it will almost certainly be much faster (again, depending on how efficient your algorithms are), but it will be much harder to learn and get started, respectively. Of course, doable, but it will be a problem.

+2
source

Canvas quickly will be too slow for 25 + fps.

the number of objects does not matter how complex they are.

if it's 100 images, it won't take anything compared to 1 Avatar 3D model, for example.

in java you can use opengl or java3d

I would like to go with Opengl as an individual choice

0
source

All Articles