If you are engaged in game development, you might want to consider using Java 2 Micro Edition. This article will help get started; soon, you'll be well on your way to building your first game. It is excerpted from J2ME Games with MIDP2, written by Carol Hamer (Apress, 2004; ISBN 1590593820).
IN THIS CHAPTER, I cover what you need to do to set up your computer for Java 2 Micro Edition (J2ME) game development and how to get your games running on an actual target device. Once you have your development environment running, you can start by building and modifying the examples from this book. You can download all the source code for the examples from the Downloads section of the Apress Web site (http://www.apress.com). This includes all the image files, descriptor files, and optional scripts.
Downloading and Installing the Toolkit
If you haven’t already downloaded and installed a development toolkit, you can get the standard one at http://java.sun.com/j2me/download.html. Look for the J2ME Wireless Toolkit 2.0. If you’re planning to develop some games that will also runwith Mobile Internet Device Profile (MIDP) 1.0, you may also want to download the J2ME Wireless Toolkit 1.0.4 for backward-compatibility testing. Many other J2ME emulators are available on the Web for free download, but for the rest of this chapter I’ll assume you’re using the J2ME Wireless Toolkit 2.0 from Sun.
If you have trouble downloading the toolkit from Sun, keep in mind that you need to register at the Sun site and log in. This shouldn’t be a problem—it doesn’t cost anything. You have to submit your e-mail address, but Sun has never sent me any spam as a result of my registration, so don’t worry about anything.
The J2ME Wireless Toolkit contains a minimal MIDlet development environment (called KToolbar), a cell-phone emulator, and a number of helpful demo applications with source code. It also contains a clear and comprehensive manual in Hypertext Markup Language (HTML). I therefore won’t take up too much space in this chapter repeating information contained in the manual about using the toolkit. I’ll just highlight a few additional points that I noticed about toolkit in the “Compiling and Running from the Command Line” and “Using KToolbar” sections.
Building an Application for MIDP
I’ll stick with tradition and start with the classic “Hello, World” application. This example will illustrate how to get a minimal MIDlet compiled and running.
When you examine the demo applications that are bundled with the toolkit, you’ll notice that they consist of a jar file and a jad file. The jar file contains the class files, the resources, and a manifest file (just as you’d expect to find in any jar file). The jad file is a Java properties (text) file that contains information to help the device run the application. The manifest file (MANIFEST.MF) found inside the jar file contains the same information as the jad file minus two properties: MIDlet-Jar-URL and MIDlet-Jar-Size.
Listing 1-1 is an example of the jad file I wrote for my “Hello, World” application. I called this file hello.jar.
MIDlet-1: Hello World, /images/hello.png, net.frog_parrot.hello.Hello MMIDlet-Description: Hello World for MIDP MIDlet-Jar-URL: hello.jar MIDlet-Name: Hello World MIDlet-Permissions: MIDlet-Vendor: frog-parrot.net MIDlet-Version: 2.0 MicroEdition-Configuration: CLDC-1.0 MicroEdition-Profile: MIDP-2.0 MIDlet-Jar-Size: 3201
The MIDlet-1 (and MIDlet-2, and so on) property gives the name of the MIDlet, the location of the MIDlet’s icon, and the fully qualified name of the MIDlet class to run. The first two items describe how the MIDlet will appear on the menu of MIDlets. The icon should be in the jar file, and its location should be given in the same format as is used by the method Class.getResource(). Thus, in this example, your jar file should contain a top-level folder called images, which contains an icon called hello.png, as shown in Figure 1-1.
Figure 1-1. This is the icon hello.png.
The MIDlet-Jar-Size property gives the size of the corresponding jar file in bytes, which you can find by looking at the properties or long listing of the jar fille. Be aware that if you rebuild the demos or your own applications using the build script or batch file bundled with the toolkit, you must manually update the size of the jar file in the jad file. If the MIDlet-Jar-Size property in the jad file doesn’t match the size of the jar file, the MIDlet won’t run.
NOTE Since the size generally changes with every build, and it’s annoying to open your jad file in a text editor with every build, I’ve included some build script modification suggestions in the “Compiling and Running from the Command Line” section.
The MIDlet-Jar-URL property gives the address of the MIDlet jar relative to the location of the jad file. If the jar file and the jad file are kept in the same directory, this is just the name of the jar file. It’s also possible to use a complete Uniform Resource Locator (URL) if the jar file is located somewhere else on the Internet. Chapter 7 discusses the MIDlet-Permissions property, but for simple games, you can omit it or leave it blank. The other properties are self-explanatory.