Table of Contents

java 1.4 game programming
Java 1.4 Game Programming
byAndrew MulhollandandGlen Murphy ISBN:1556229631
Wordware Publishing 2003 (647 pages)

This text covers all of the key elements in Java which are required for creating games, as well as significant new features which have become available with the release of the latest version 1.4 from Sun.

Table of Contents
Java 1.4 Game Programming
Chapter 1 - Introduction to Java 1.4
Chapter 2 - Basics of Java Programming
Chapter 3 - Arrays and Strings
Chapter 4 - Multiple Classes
Chapter 5 - Packages, Utilities, and Error Handling
Chapter 6 - Stream I/O
Chapter 7 - Threads
Chapter 8 - Applications and Applets
Chapter 9 - Graphics
Chapter 10 - Using the Mouse and Keyboard
Chapter 11 - Using Sound and Music
Chapter 12 - Game Programming Techniques
Chapter 13 - Introduction to GUI
Chapter 14 - Introduction to Databases
Chapter 15 - Using SQL with MySQL
Chapter 16 - Using the JDBC
Chapter 17 - Introduction to Networking
Chapter 18 - Introduction to NIO Networking
Bonus Chapter - A Tour of the Swing GUI
List of Figures
List of Listings

Back Cover

With the release of Java 1.4, it is now possible to develop more advanced computer games using the Java language than ever before. Java 1.4 Game Programming focuses on the technical aspects of game programming using the latest release of Java, beginning with an introduction to the Java programming language and building upon that knowledge by exploring the key elements required to develop games, such as graphics, input, sound, and networking. Features new in Java 1.4, including full-screen exclusive mode, new graphics features, and NIO networking, are also covered.

  • Gain a solid foundation in the Java 1.4 language.
  • Learn key issues associated with graphics programming in Java.
  • Understand the importance of threads throughout all related topics.
  • Find out how to handle mouse and keyboard input correctly in a game environment.
  • Develop a reusable sound manager for your games.
  • Learn about animation, collision detection, and high-resolution timing.
  • See how to create your own in-game GUI system.
  • Create a solid game framework for use in your own game projects.
  • Learn how to develop multiplayer games in Java.

About the Authors

Andrew Mullholland and Glenn Murphy have each been programming games for more than six years and have several years of experience with Java. They are both in the final year of the Computer Games Technology program at the University of Abertay in Dundee, Scotland. Andrew is also a co-author of Developer’s Guide to Multiplayer Games from Wordware Publishing.

Java 1.4 Game Programming

Andrew Mulholland and Glenn Murphy

Library of Congress Cataloging-in-Publication Data

Mulholland, Andrew.
     Java 1.4 game programming / by Andrew Mulholland and Glenn Murphy.
         p. cm.
     ISBN 1-55622-963-1

     1.   Java (Computer program language) 2.  Computer games—Programming.
     I.  Murphy, Glenn, 1908- II.  Title.
     QA76.73.J38 M849 2003

     794.8'152762—dc21                                                2002155485

© 2003, Wordware Publishing, Inc.

All Rights Reserved
2320 Los Rios Boulevard
Plano, Texas 75074

No part of this book may be reproduced in any form or by any means without permission in writing from Wordware Publishing, Inc.

ISBN 1-55622-963-1

10  9  8  7  6  5  4  3  2  1

Java is a trademark of Sun Microsystems, Inc.
All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks should not be regarded as intent to infringe on the property of others. The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products.

All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the above address. Telephone inquiries may be made by calling:

(972) 423-0090

About the Authors

This for me is probably the easiest part of the book to write. I really have a liking for writing about myself as I always feel it will sound boastful and conceited, which I am.

Well, I started life as a baby and I'm afraid that's where it all began. I started programming at around 16 years of age while attending college in my hometown of Manchester, England. I use the term "attended" loosely, however, because as soon as the programming began, I was hooked, and ducked out of many a lecture to create a variety of games, albeit on an 80x25 ASCII character resolution and a useful gotoxy(x, y) method. In the second year of college, a friend and programming buddy, Nick Kitson, and I co-wrote a 16,000-line soccer management game in Pascal called ESM European Soccer Manager, where you could actually watch the matches in an overhead view. Working on this taught me more than anything about programming. My advice—pick a goal and go for it.

After college, I made it into the Computer Games Technology program at the University of Abertay Dundee and am now midway through the (honours) 4th year. While "attending" university I have furthered my knowledge from Pascal to C/C++ and then on to Java. The ability to make web games playable in a browser (applets) was what originally made Java so appealing to me, and had been a mystery to me for long enough. There began my introduction to Java. Before Java, I was mainly a procedural programmer, and not that well tuned to object-oriented programming (OOP). The good thing about Java, in this sense, is that it is completely OOP, so there was no choice but to program in this style. For this I think learning Java is the best guide to OOP you can get. And then came the book.

After working on a Java game over the summer of 2000, I teamed up with my now co-author Andrew, who has been my flatmate and friend since the first year of university. The university's random accommodation allocation for freshers can take the credit for us meeting in the student halls. We began work on the book in late 2001, while both juggling our honours degree courses at the same time. Besides losing my virginity and trying to complete Jet Set Willy, this book has been the most grueling experience of my life, but it was all worth it in the end. (I hope this last sentence makes it to publication.)

My primary hope for this book is that it makes me as much money as possible. My secondary hope, besides programming games in Java, is that the book indicates the difficulties that we came across when researching Java for games programming in a clear manner, especially those surrounding threads, input, and graphics that we put a lot of work into. I think to become a good programmer you have to enjoy it; otherwise it's little use. Most of the enjoyment I find is in showing off what I have done, which there is no harm in now and again (and again and again J). One thing I am aware of is that in actually challenging yourself to do something, and believing that you can do it, there seems to be a fear factor where you often do not even attempt to code something, because you have never done it before. In buying this book, you have made a solid move in conquering this fear.

My interests mostly revolve around playing pool, watching films, and occasionally programming the night away, and there is still no better feeling than "7-balling" someone in a crowded club. I also collect Star Wars costumes and wear them out clubbing regularly.

As a final word, I hope this book is as useful to you as it was for me in writing it, and wish you luck in your quest of knowledge.

Glenn Murphy

This for me is probably one of the hardest parts of the book to write. I really have a dislike for writing about myself as I always feel it will sound boastful or conceited. Nevertheless, here goes…

Well, I am currently 21 years old and halfway through my 4th (honours) year of university studying BSc (Hons) Computer Games Technology at the University of Abertay in Dundee, Scotland. I would say I have been coding for around six years now and have obtained quite a broad range of skills within this time.

My first real stab at game programming was about a year and a half before I left home to go to university when I downloaded the DJGPP DOS compiler ( and the Allegro game library ( originally started by Shawn Hargreaves. After starting and never finishing a few projects, the next logical step was to move onto looking at OpenGL and DirectX. As well as looking at the 3D side of games programming however, I also invested time in learning Perl and MySQL, although I have recently switched to using PHP4 as it is sooo much nicer than Perl. ;)

From there, I then progressed onto Java and to be honest it is probably the best thing I have ever done. Java really is such a great language. Don't get me wrong—it does have some issues, but the structure of the language and also the documentation is really excellent (ever tried using MSDN? Urgh.).

One thing that deceived me, however, when I started to use Java was the simplicity. When you start looking into Java properly, you think—ah great, all the libraries have been written for me. However, as you will see as you progress through the book, these libraries are excellent for business application development, but there are some pitfalls and serious issues to consider when looking at the language and libraries (packages) from a game development point of view.

Probably now is a good time for a plug. Previously to this book, I coauthored Developer's Guide to Multiplayer Games, which focuses in detail on using sockets in C/C++ to create client-server games. That book has a large tutorial section that takes you through the process of writing a reusable network library and a multiplayer game, which also includes a signup/login and lobby system. What the book does not cover, however, is DirectPlay as we remain platform independent throughout the book, so your game server will compile as easily on the Linux platform as it will on the Windows platform. If you are interested, you can find out more information (and buy it J) at the following link:

On a final note, I hope you enjoy reading this book and find the information within it useful. If you have any questions or problems with anything in the book, do not hesitate to e-mail either myself or Glenn and we will try to help you as best we can!

Andrew Mulholland