New to game programming

Hey guys.

So Ive been programming in Java for a few years now, but nothing game related. I am quite comfortable programming in Java… although I do know Python and C/C++ on a beginner/medium level. Since I want to do game programming, and since its a notoriously difficult thing to do, I thought it would make sense to use the language that Im the best with. So, how can I start game programming in Java? What libraries will I need for 2D games? It seems like other languages are more clear cut… for C++ Id just use SDL, for Python I’d use pygame, etc. Mind you I dont want some pre fabricated game engine… just the API to access graphics, sound, input, etc. Basically I want to do as much on my own as I can, because its all about learning for me. Once I have a library, where do I start? People have said do something simple like tetris or breakout… but I wouldnt even know where to start those. Ive looked at LWJGL and it seems like what Im looking for. But Im also confused as to the difference between using something like that, or using the AWT and applets to create a game? Some of the tutorials Ive seen use threads and the AWT rectangles and circles to draw animations to make simple pong games and such. Should I be doing something like this instead of getting a library? Im so confused… I just want to get started on the right track. Thanks

LWJGL gives you OpenGL, OpenAL, and JInput, which take care of graphics, sound, and input. It usually replaces anything Java has built in (ie. the AWT). You might use AWT for utility functions such as loading images still, or even use LWJGL embedded in AWT frames as a convenience, or even use AWT’s input methods instead of JInput.

Cas :slight_smile:

I’m interested in seeing the range of replies and suggestions. I have been programming Java for a year. My background is MS Access database & VBA programming. I have been using a 2D game project for learning Java.

I think the Graphics2D library is perfectly good for doing a Breakout or Pong, if that is what you want to do. There is a lot to learn in terms of game structure and basics. You would want to know how to use a Timer object, like java.util.Timer, for animation. And some Swing components, and how to integrate listeners for them. That would be a substantial start for stage I “bouncing ball” and stage II “Pong” or “Breakout” which mainly needs the ability to make a controllable paddle once you have the ball bouncing.

I am very much looking forward to getting into LWJGL! Haven’t tried it out yet. For now, for sound, I’ve been using the javax.sound.sampled library. At first it was really confusing, but after becoming acquainted with the “Decorator” design pattern, I don’t find the multiple nesting of objects quite so off-putting. But I hear that this library may not be the best in terms of doing sound, and am open to learning about other options. As a learning project for sound, I built a little windchime, and it runs and sounds great. So I don’t know yet what the problem is with this library other than a rough learning curve.

I wouldn’t worry about the Applet vs Application distinction. It is easy enough to convert from one to another when the time comes.

I think the best way to get you up to start is to do Kev’s space invaders tutorial http://www.cokeandcode.com/spaceinvaderstutorial. It even starts with Java2D and moves to OpenGL in the end, so it should be exactly the kind of experience you need to help you decide which route to follow.

If you want to use OpenGL in Java, think about JogAmp too (OpenGL, OpenGL-ES, OpenCL and OpenAL binding for Java):
http://jogamp.org

I stick with the basic stuff in AWT rather than using an outside library. It’s less to keep track of that way.

I did write my own basic widget system though - AWT and Swing really aren’t that good for games. I have one instance of JFrame that everything goes into, and I create my input events from the input events AWT sends me. I also use the AWT for image loading because it takes care of hardware acceleration for me.

The games are make are mostly rather simplistic. Even the more complicated game I’m trying to work on now doesn’t have complicated graphics.

Ok yea so Im planning on just making a few simple games using built in Java, then Ill learn a library to enhance things. Ive been trying to find a clear and concise tutorial on making games with built in java libraries… but most of them are really unorganized. Every one of them just throws code together without any structure, and its really hard for me to follow. It seems like there really arent any useful books either. Can you guys point me towards some valuable resources?

Killer Game programming in java : http://fivedots.coe.psu.ac.th/~ad/jg/

Took a look at the one I posted?

Thanks psycho pat!

cylab: OO crap I didnt even see that… sorry man. thanks so much Ill check it out.

I was looking through “TheNewBostons” tutorials but he really confused me with his organization of code… I just really got tripped up in the way he did things.

Ok I wanted to ask something. Im working Kevins tutorial for space invaders over at http://www.cokeandcode.com

Im pretty much following it, but what I dont get is how the hell is somebody supposed to go about learning and remembering all of these damn built in Java classes and methods? Just my sprite class alone has types such as Sprite, Image, GraphicsConfiguration, BufferedImage, etc… plus I am using methods like getClass(), getClassLoader(), getResource(), getLocalGraphicsEnvironment(), getDefaultScreenDevice(), getDefaultConfiguration(), etc.

I hate just copying this shit without knowing what each this is doing. The goal of this is for me to be able to do all of this on my own in the future and not follow someone else. But I feel like Im just copying his methods because there are just so many built in ones. He has his code commented but its doesnt help as to why we are doing these things.

I made a call:
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

Thats fine, but why??? Will the books like Killer Game Programming in Java explain these things to me so I can use them on my own?

I’m afraid that’s exactly what programming is all about.

Each of those methods and classes has very thorough Java documentation. You can look them all up online (or download the Javadocs), or they should be directly accessible from your IDE. (In Eclipse, for example, just open the Javadoc view, and then any method you hover over will show its documentation which for the core Java APIs is usually very comprehensive)

Eventually you’ll learn about 10% of the API very well, and that’s all you mostly need. Takes a few years. Took me about 10.

Cas :slight_smile:

Yes well thats fair…all of that is definitely true… but the biggest problem for me is that I cant figure out how I would know when I needed to use all of these things. Is all of that sort of the “backbone” of a Java game? So like getting the graphics environment and all that would be something all Java games would need to use? Im guessing the if an external API is used like JOGL or LWJGL , I would no longer use these built in Java methods?

LWJGL is a complete API unto itself really. Init code can be just a single line:


Display.create();

but then you have to learn OpenGL…

Cas :slight_smile:

[quote] but the biggest problem for me is that I cant figure out how I would know when I needed to use all of these things.
[/quote]
That’s why I really don’t agree with trying to start on the internal libraries. I think it’s better to pick a useful library and learn it well. I know I don’t understand a lot, but I don’t really care. I just want to write games. The nice thing about java is that it’s class based and I can add functionality by extending classes. That’s a lot better than trying to do things with scripts in my book.

Ok so is the general recommendation for me to just stick with the Java base libraries for a while and get comfortable making games with it… then move onto a more extensive library once I need functionality beyond the scope of built in Java graphics? Or would diving into LWJGL right off the bat be better? The books I have “Killer game Programming in Java” as well as a few others tend to rely heavily on built in JAVA API’s, so maybe sticking with them right now makes sense so that I can make use of books? Right now using built in Java for games is confusing the shit out of me, so if moving to another library with deeper functionality will only be worse, it doesnt make sense to do that at this point.

You can always ask questions here about the things in core Java that are confusing! Better to clear them up sooner rather than later, I would think.

Sometimes more advanced libraries make things much easier, sometimes they propagate misconceptions that really should be dealt with. But you know that.

I just (last two days) tried my hand at making a game tutorial, with an emphasis on the things in core Java that I personally found confusing, and posted it in the tutorials section above. Would love to have some feedback if you take a look at it. But it might be pitched at too easy a level for what you are looking for.

thanks philfrei. Ill check out the tutorial.

I think Im going to go the route of getting comfortable with the base java graphics library, and then ease my way into opengl and LWJGL. As far as my level, I think I am a pretty advanced Java programmer. I am by no means a master of the language, but Ive been using it for many years now, just nothing graphical. Game programming is proving to be a totally different evil than general app programming. The game logic part is sort of tough for me, but Im catching on rather quickly. Its the graphics that are killing me. I think its just the whole idea of the Canvas and JFrame and Buffering, etc. Plus I still have no idea what a graphics context is! I think I need to read the java docs a bit better and really try to understand whats happening. doing that plus following tutorials plus reading source code should do it.

I am not by any means trying to find an easy way, i know that this stuff isnt easy… I never thought it would be. Im ready to bust my ass and learn

You are probably ahead of me in many aspects then, as far as Java programming. But I have been spending some time with various aspects of graphics while working on a puzzle game, so I might be able to help a bit. Helps to have a concrete question.

There’s a LOT of good stuff in the Graphics2D library. Sounds like you may already be aware of these tutorials, but just in case:
http://download.oracle.com/javase/tutorial/2d/index.html

I frankly haven’t looked into the “Canvas” as an object for displaying. I’m not even sure what it is. AFAIK, a JPanel is perfectly fine for display. Could someone say why prefer a Canvas to a JPanel? I can’t find any references to “Canvas” object in Horstmann’s Core Java 8th ed. And JPanel has built in double buffering. I’ve been able to display repaints in the teens (in terms of milliseconds), for example at www.adonax.com/Jean/BotBlocker/ if you’d like to see my amusing captcha fiasco. (Commentary in “Miscellaneous Topics” was very educational.)

With a Canvas, you have full control over when the screen is repainted. With a JPanel, you’re basically tellingt the EDT to “repaint this when you have time”.
I recommend using a Canvas for games since it provides much smoother gameplay than a JPanel. It’s very easy to implement doublebuffer on a canvas as well.