Home > Articles

Constructing a Mobile Game Skeleton

This chapter is from the book


Released by Namco in 1979, Galaxian was the first major space shooter to serve as a legitimate successor to Space Invaders. Galaxian is also the predecessor to Galaga, which is perhaps the most successful space shooter game of all time. In Galaxian, as in most games of the genre, you control a space ship that can move horizontally along the bottom of the screen and fire vertically at attacking aliens. Galaxian holds a special place in video game history because it is the first arcade game to use true RGB (Red Green Blue) color throughout all its graphics.

Java development of any kind revolves around the Java programming language and a set of APIs that provide support for application services such as GUI components, networking, and input/output. Mobile Java game development is no different in that it relies on a set of APIs for supporting the various pieces of functionality required for MIDlet games that must run in a wireless mobile environment. Understanding these APIs and what they have to offer is critical to becoming a mobile Java game developer. This chapter introduces you to the mobile Java APIs and guides you through the creation of a mobile game skeleton. This skeleton MIDlet serves as a template for you to reuse as you continue to develop games throughout the book.

In this chapter, you'll learn

  • How J2ME programming is broken down into a few different APIs

  • About the internal structure of MIDlets

  • How to build a MIDlet from scratch that displays important game-related information about a mobile phone

  • Prepare a MIDlet for distribution

Exploring the J2ME APIs

Before getting into the coding details of your first mobile phone program, you need a quick primer on the APIs that go into building MIDlets. The MIDP (Mobile Information Device Profile) specification is a set of rules that describe the capabilities and limitations of Java with respect to mobile devices. A significant aspect of these capabilities and limitations is the standard set of classes and interfaces that are available for MIDlet programming. Although the MIDP specification provides a detailed description of the API available for MIDlet development, an additional API is provided by the CLDC (Connected Limited Device Configuration). The MIDP API builds on the CLDC API to provide classes and interfaces that are more specific to mobile information devices. You can think of the CLDC as providing a general Java API for networked devices, whereas the MIDP goes a step further in providing a more detailed API that fills in the specifics left out of the CLDC API for compact wireless devices such as phones and pagers.

Figure 3.1Figure 3.1 A MIDlet must make calls to the CLDC and MIDP APIs to carry out most of its functions.

Why should you care about any of these specifications and APIs? The CLDC and MIDP specifications are important because they explicitly define what classes and interfaces can be used to build MIDlets. Mobile devices are nimble machines that don't have the luxury of megabytes of memory to pack full of application overhead. Knowing this, Sun had to figure out a way to provide a core set of functionality with a useful feature set but without bloating the runtime requirements of mobile devices. Their answer is the two-tier approach that consists of a configuration layered with a more detailed profile. The CLDC API describes the core classes and interfaces required by a general network device, whereas the MIDP API adds the classes and interfaces required by a mobile information device such as a mobile phone. Figure 3.1 shows the relationship between a MIDlet and the respective CLDC and MIDP APIs.

Keep in mind that although the CLDC and MIDP APIs have been carefully thought out to trade off functionality against the memory and resource constraints of mobile devices, they will inevitably come up short in certain situations. This means that you will sometimes have to work a little harder as a MIDlet game developer because you don't have as rich an API to work with as you would if you were doing traditional game programming.


The majority of the classes in the CLDC API are directly included from the standard J2SE API. These classes and interfaces are practically identical to those that you may be familiar with from traditional Java programming. This portion of the CLDC API is located in packages with familiar J2SE names such as java.lang and java.util. In addition to the classes and interfaces that are borrowed directly from the J2SE API, a few interfaces are unique to the CLDC API. These interfaces deal primarily with networking, which is an area of the J2SE API that is particularly difficult to scale down for the needs of network devices.

The CLDC defines a set of interfaces that facilitate generic networking, and leaves the specifics of implementing these interfaces to the MIDP API. So the CLDC API is logically divided into two parts:

  • A series of packages that serve as a subset of the J2SE API

  • A set of generic networking interfaces

The bulk of the classes and interfaces in the CLDC API are inherited directly from the J2SE API. J2ME requires that any classes or interfaces inherited directly from J2SE must not be changed in any way, which means that the methods and fields are identical to the versions found in J2SE. This makes it easier to learn how to program in J2ME, and it also makes Java code more portable between J2SE and J2ME.

Where the CLDC veers away from the J2SE API is in its support for networking, which is outlined in a generic network framework known as the Generic Connection Framework (GCF). The purpose of the GCF is to define a general network architecture that supports networked I/O and is extremely flexible, and is therefore extensible. The GCF is designed as a functional subset of the J2SE networking classes, which means that features described in the GCF are available in J2SE. The GCF consists primarily of a set of connection interfaces, along with a Connector class that is used to establish the different connections. Both the Connector class and the connection interfaces are located in the javax.microedition.io package. You learn much more about network mobile game programming in Chapter 14, "Mobile Game Networking Essentials."


A device profile picks up where a configuration leaves off by providing detailed functionality to carry out important tasks on a given type of device. In the case of the Mobile Information Device Profile (MIDP), the type of device is a wireless mobile device such as a mobile phone or pager. It is therefore the job of the MIDP API to take the CLDC API and build on top of it the necessary classes and interfaces that make it possible to build compelling MIDlets such as games.

Similar to the CLDC API, the MIDP API can be divided into two parts:

  • Two classes that are inherited directly from the J2SE API

  • A series of packages that include classes and interfaces unique to MIDP development

Like the CLDC API, the MIDP API borrows from the standard J2SE API. Not surprisingly, the bulk of the MIDP API is new classes and interfaces designed specifically for use in MIDlet programming. Although these classes and interfaces can play a similar role as some of the classes and interfaces in the J2SE API, they are entirely unique to the MIDP API and therefore are carefully designed to solve MIDlet-specific problems. This portion of the MIDP API is divided among several packages, all of which are prefixed with the javax.microedition name:

  • javax.microedition.midlet

  • javax.microedition.lcdui

  • javax.microedition.lcdui.game

  • javax.microedition.media

  • javax.microedition.media.control

  • javax.microedition.io

  • javax.microedition.pki

  • javax.microedition.rms

The javax.microedition.midlet package is the central package in the MIDlet API, and contains only one class: the MIDlet class. The MIDlet class provides the basic functional overhead required of a MIDP application (MIDlet) that can execute on a mobile device. You will continue to learn more about the MIDlet class as you progress through the book and construct more complex MIDlet examples and games.

The javax.microedition.lcdui and javax.microedition.lcdui.game packages include classes and interfaces that support GUI components specially suited for the small screens found in mobile devices. Additionally, there are classes and interfaces that specifically target the development of mobile games. Unique features such as sprite animation and layer management make these packages extremely valuable for mobile game programming. You begin learning about some of the classes in these packages later in this chapter, and continue to dig deeper into them throughout the book.


If you happen to have used J2ME before, you'll be interested to know that the javax.microedition.lcdui.game package is entirely new to MIDP 2.0. This is why MIDP 2.0 represents a significant leap forward in making J2ME a viable mobile game technology.

The javax.microedition.media and javax.microedition.media.control packages include classes and interfaces for managing audio within a MIDlet. These packages represent the MIDP 2.0 Media API, which is a subset of the larger Mobile Media API; the full Mobile Media API supports a wide range of media objects such as images, sounds, music, and videos. The media features in the MIDP 2.0 Media API are limited to tone generation and the playback of digital audio effects via wave files. You find out the specifics of playing sound in MIDlet games in Chapter 8, "Making Noise with Tones."

You learned earlier that the CLDC lays the groundwork for networking and I/O with the Generic Connection Framework (GCF). The MIDP API builds on this support with the javax.microedition.io package, which includes several interfaces and classes for establishing wireless network connections and shuttling data back and forth across them. The javax.microedition.pki package is used in concert with the javax.microedition.io package to provide secure network communications. You learn how to carry out game basic networking tasks in Chapter 14, "Mobile Game Networking Essentials."

Because mobile phones don't have hard drives or any tangible file system (yet), you probably won't rely on files to store away persistent MIDlet data. Instead, the MIDP API describes an entirely new approach to store and retrieve persistent MIDlet data: the Record Management System (RMS). The MIDP RMS provides a simple record-based database API for persistently storing data such as high score lists and saved game data. The classes and interfaces that comprise the RMS are all located in the javax.microedition.rms package.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020