What Is Java?
Earlier I mentioned that
Java began as a programming language that enabled networked devices to
communicate with each other. More specifically, Java started out as a
project at Sun with engineers studying how to put computers into
everyday household items. One primary focus of the project was to have
these computerized devices communicate with each other. As it played
out, Sun was a little ahead of its time in applying Java to network
everyday devices. However, the company was quick to react and ended up
making Java a huge success by targeting it for the web.
As soon as
technology and public perception caught up with Java’s earlier
aspirations, Sun went back to the drawing board and retooled Java for
the mobile computing domain. Not only was J2ME designed for the
constraints of mobile devices, but it is also well suited for wireless
networking. J2ME is actually a subset of the larger Java technology,
which consists of a programming language, an API, and a runtime
environment.
Why Java?
Even with Java
being ideally suited for mobile phone development, it wouldn’t be of
much use if it didn’t have broad industry support. Java is currently the
predominant software development technology for mobile phones, and all
signs point to it maintaining and possibly even expanding its market
share. Some analysts estimate that by 2007 some 450 million mobile
phones will be sold with Java support, representing 75% of the entire
mobile phone market.
Developers have
flocked to Java largely because it is considered an open platform, which
means that you can develop one set of code and have it run across a
wide range of mobile devices. Unfortunately, the “openness” of Java has
been complicated a bit because of third party APIs and hardware
variations among different devices, but generally speaking, you can
write a game once and use the majority of the code unmodified on a
variety of mobile phones. Contrast this with BREW, which is geared
solely for phones operating on Qualcomm CDMA networks.
Gamer’s Garage
Technically
speaking, even though Java and J2ME are more “open” than BREW, they
still are not “open source.” All facets of the Java technology,
including J2ME, are owned by Sun Microsystems. Fortunately, Sun has been
very forthcoming with outside input on the Java standards, but many
developers are still lobbying them to turn Java over to the Open Source
community. |
An interesting twist
on the relationship between Java and BREW came about in late 2002 with
the introduction of a Java virtual machine for BREW devices. This means
that BREW devices can effectively support J2ME just as if they were
factory Java-powered devices. In this way, Java doesn’t really compete
with BREW as a platform for game development. However, because “pure”
Java phones don’t support BREW, it does mean that you reach the largest
market by going with Java.
Gamer’s Garage
Incidentally,
you may have noticed that I use the terms Java and J2ME
interchangeably. Although J2ME is technically a subset of the larger
Java technology, within the confines of this book the two terms mean the
same thing because I’m usually discussing Java within the context of
mobile phones. |
Java and Mobile Game Programming
You know the “what” and the
“why,” and now it’s important to get into some of the details of the
“how.” In other words, how does Java make mobile programming possible?
The primary areas of importance for any game programming technology
include the following:
Graphics and animation
User input
Sound
Networking
The next few sections explore these game programming topics in the context of J2ME.
Graphics and Animation
The standard J2ME
API includes support for all kinds of neat graphics features such as
images, 2D graphics primitives, and animation. 2D graphics primitives
include lines, rectangles, ellipses, and so on. In terms of animation,
J2ME supports sprites, which are images that are capable of being
independently moved and animated. The J2ME API also supports sprite
collision detection, which allows you to determine whether two sprites
have collided with each other; this is a critical requirement of
virtually every action game.
Another
very interesting feature specific to games in the J2ME API is tiled
layers, which enable you to arrange small rectangular image tiles to
create much larger backgrounds. Tiled layers make it possible to create
large maps that can be reconfigured in games while also conserving
memory because the individual image tiles are being reused whenever
possible. It is also possible to manage multiple layers with ease,
thanks to a handy layer manager provided by J2ME. This makes it possible
to create one background layer that serves purely as decoration, and
another layer that provides barriers to prevent a character from moving
around freely in a game.
Responding to Mobile User Input
User input is a
very critical area of game development because it dictates how a game
“feels” to the game player. User input is also important because it
establishes the primary interface between the player and the game. J2ME
provides support for key input, which is currently the only mode of
input for mobile phones. There is specific support for directly reading
the state of the keys on a phone, which is very important for games
because you want the keys to be highly responsive.
Gamer’s Garage
Keep
in mind that the key configurations for mobile phones vary
considerably, but they all have common keys for performing certain
tasks. More specifically, every Java-powered mobile phone has keys that
correspond to Up, Down, Left, Right, and Fire, along with several other
“utility buttons.” For phones that actually have game pads, each side of
the game pad maps to a directional key such as Up, Down, and so on. |
Playing Sound in Mobile Games
Rounding out the
“big three” areas of game development is sound. J2ME supports the
playback of digital sounds that are in either the PCM or WAV formats, as
well as MIDI music. The sound support in the J2ME API is based on
Java’s Mobile Media API, which is an API for allowing the playback and
recording of audio and video on mobile devices. For the purposes of game
development, all you’re really concerned with is the efficient playback
of audio and possibly video at some point.
Gamer’s Garage
To
keep from straying too far away from core game programming topics, this
book focuses solely on playing sounds with J2ME, and doesn’t tackle
video. |
Mobile Networking
The true killer
application of mobile phone games will likely be the built-in network
available to all mobile phones. With this in mind, it only stands to
reason that the network features of Java play heavily into its
usefulness as a mobile game platform. Fortunately, networking is one
area where Java really shines because it is such an integral part of the
Java runtime system. Unlike other popular game programming languages
such as C and C++, Java was designed from the ground up to support
networking.
Combine Java’s
extensive network support with its platform independence and you have a
gaming platform that crosses all boundaries for availability to users.
This is very important when you consider that mobile game players will
want to play games across a variety of different devices and wireless
carriers. Game players shouldn’t have to concern themselves with the
technical distinctions between their mobile phone and a phone made by a
different manufacturer. Thanks to the networking features built into
Java, developers don’t have to worry about these distinctions, either.