Developing Games in Java
- Get the book
- Play the example game
- Download the source
- View the notes & errata
- What's happened since the book was released?
- View source code license
Get the book
Amazon: Developing Games in Java
Play example game
Requires Java 1.4.2 or newer. Warning: rumors are, the demo may not work correctly on Java 7!
This game demonstrates:
- Thread pools (in the sound manager) - Chapter 1.
- Full screen mode, hardware-accelerated graphics, animation, and sprites - Chapter 2.
- Keyboard input and mapping multiple keys to the same action - Chapter 3.
- Sound, music, and sound filters - Chapter 4.
- Paralax scrolling, tile-based maps, gravity, jumping, and bounding-box collisions - Chapter 5.
- Simulated timer accuracy - Chapter 16.
- Art and sound creation techniques - Chapter 17.
- Game states (loading, intro sequence, main game) - Chapter 18.
- Deployment using Java Web Start (JNLP files) and loading media from jar files - Chapter 18.
Download source code
These packages include source code and any resources (graphics, sounds, scripts, etc) needed to run the examples.
Requires Apache Ant 1.5 to compile. Ant is either directly integrated or available as a plugin for several free/open source editors and IDEs, including jEdit, NetBeans, and Eclipse.
If you use Apache Ant, everything compiles error-free! See the errata below for any issues.
Description | Download |
---|---|
All source code | allsrc.zip (4.3MB) |
Chapter 1, "Java Threads" | ch01src.zip |
Chapter 2, "2D Graphics and Animation" | ch02src.zip |
Chapter 3, "Interactivity and User Interfaces" | ch03src.zip |
Chapter 4, "Sound Effects and Music" | ch04src.zip |
Chapter 5, "Creating a 2D Platform Game" | ch05src.zip |
Chapter 6, "Multi-Player Games" (By Bret Barker) | ch06src.zip |
Chapter 7, "3D Graphics" | ch07src.zip |
Chapter 8, "Texture Mapping and Lighting" | ch08src.zip |
Chapter 9, "3D Objects" | ch09src.zip |
Chapter 10, "3D Scene Management Using BSP Trees" | ch10src.zip |
Chapter 11, "Collision Detection" | ch11src.zip |
Chapter 12, "Path Finding" | ch12src.zip |
Chapter 13, "Artificial Intelligence" | ch13src.zip |
Chapter 14, "Game Scripting" | ch14src.zip |
Chapter 15, "Persistence - Saving the Game" (By Laurence Vanhelsuwé) | ch15src.zip |
Chapter 16, "Optimization Techniques" | ch16src.zip |
Chapter 17, "Creating Game Art and Sounds" | ch17src.zip |
Chapter 18, "Game Design and the Last 10%" | ch18src.zip |
Errata
Here are a few source code notes based on reader comments.
Loading Images (relative path issues)
Some chapters contain both code and images, but don't use jar files. Thus, images are loaded via relative paths like "../images/background.jpg" with the idea that you'll run the code from the chapter's "build" directory. You'll need to change the paths if you run the code from a different directory.
Sound Issues in Java 5 and Java 6.
- MIDI music doesn't loop. Add this line (in bold) in MidiPlayer.java:
public void meta(MetaMessage event) { if (event.getType() == END_OF_TRACK_MESSAGE) { if (sequencer != null && sequencer.isOpen() && loop) { sequencer.setTickPosition(0); sequencer.start(); } } }
Thanks to Brian Bishop for the fix.
- Sounds don't play. Change these lines (in bold) in SoundManager.java:
public static int getMaxSimultaneousSounds(AudioFormat playbackFormat) { DataLine.Info lineInfo = new DataLine.Info( SourceDataLine.class, playbackFormat); Mixer mixer = AudioSystem.getMixer(null); int maxLines = mixer.getMaxLines(lineInfo); if (maxLines == AudioSystem.NOT_SPECIFIED) { maxLines = 32; } return maxLines; }
Swing integration
Eric Berry writes of a solution of integrating Swing components on Mac OS X:
The fix consisted of basically 2 things. First, I had to set ignore repaint to true on everything. This includes the frame itself, the content pane and the layered pane of the frame. Secondly I had to put the call to paintComponents in a SwingUtilities.invokeAndWait() call.
SwingUtilities.invokeAndWait(new Runnable() { public void run() { paintComponents(graphics); } });This means making the graphics object final in the draw method sig. but that's about it.
Now using the text field doesn't freeze the entire application.
There doesn't seem to be very much drawback and I did find that the invokeAndWait was all that was absolutely necessary to make it work. However, making sure everything had ignore repaint set to true improved performance slightly (a few milliseconds). There is a performance hit with adding the invoke and wait, but it doesn't seem that bad. Without it I was getting refresh rate of about 16-20 ms, after it went from 22-26 (depending on if I was using the text field or not).
Mouselook (chapter 3)
Olli Aalto (using Gentoo Linux, J2SE 1.4.2) suggests this fix for mouselook (commenting out a couple lines):
// from the MouseMotionListener interface public synchronized void mouseMoved(MouseEvent e) { // this event is from re-centering the mouse - ignore it if (isRecentering) //&& //centerLocation.x == e.getX() && //centerLocation.y == e.getY()) { isRecentering = false; }
The original code works correctly on Windows, so this appears to be a Linux-only fix.
What's happened since...?
In chapter 19, "The Future", several things were mentioned that could make things better for Java game programmers. Java 5, Java 6, and Java 7 have been released since the book came out, but how do they stack up?
Feature | Included in Java 5.0 | Included in Java 6.0 | Notes |
---|---|---|---|
New Java language features (Generics, Enumerations, Static import, Enhanced for loop, etc.) | Yes | Yes | See J2SE 1.5 in a Nutshell. |
Compiler API (JSR 199) | No | Yes | Not crucial for most game programmers. |
Network Transfer Format (JSR 200) | Yes | Yes | See the Pack200 class. |
Shared VM (JSR 121) | Yes and No | Yes and No | The VM isn't shared, but class data sharing was implemented in Java 5.0. |
Input polling (Bug ID 4083501) | Mostly No | No | Mouse position polling is implemented, but you can't poll the mouse buttons, the mouse wheel, the relative position, the keyboard, or the joystick. The bug evaluation currently states "Given a low priority by Tiger planning," probably because of the presence of the jinput project. |
Disabling the mouse cursor (Bug ID 4526950) | No | No | The bug evaluation currently states this bug will be considered for Dolphin (Java 7). As mentioned in the book, creating an invisible cursor isn't perfect on all platforms, and we need a way to disable the cursor. |
Higher resolution timer | Yes | Yes | See System.nanoTime(). |
Hardware-accelerated translucent images (Bug ID 5002129) | No | Yes | New methods have been added to support translucent VolatileImages, so we will probably see this in a subsequent 1.5 release. |
Hardware-accelerated graphics on Linux | Yes | Yes | |
Hardware-accelerated 3D included in the runtime | No | No | |
Option to turn off vertical sync | No | No | |
HotSpot SIMD operations | ? | ? | |
OGG support (Bug ID 4671067) | No | No | |
Sub-pixel anti-aliased text support - AKA ClearType (Bug IDs 4726365 and 4502804) | No | Yes |
The new higher-resolution timer is a very welcome addition, and implementing hardware-accelerated graphics on Linux is fantastic. However we still need a for more things: For game programming, hardware-accelerated translucent images is the single most important feature that needs to be implemented. Next, we need an option to disable the mouse cursor. Finally, we need input polling.
There are lots of other features added to J2SE 1.5.0, including some full-screen fixes, sound fixes, JNLP (web start) updates, Swing updates, concurrency utilities, and bit-manipulation operations. Check out Sun's list of new features.
License
The source code for the book has a BSD-style license. The new license makes it easier to include the code in commercial games.
Copyright (c) 2003, David Brackeen
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of David Brackeen nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.