Home arrow Java arrow Page 4 - Getting Started with J2ME
JAVA

Getting Started with J2ME


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).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 52
April 05, 2005
TABLE OF CONTENTS:
  1. · Getting Started with J2ME
  2. · Creating the “Hello, World” Application
  3. · Using KToolbar
  4. · Optimizing Your Game for Different Devices
  5. · Configuring the Server

print this article
SEARCH DEVARTICLES

Getting Started with J2ME - Optimizing Your Game for Different Devices
(Page 4 of 5 )

Ideally it’d be nice to have access to a whole range of devices to test your game on throughout the development phase. But even working for a large corporation you won’t necessarily have access to every device on which you may want to run your games. Fortunately, the emulator has a number of performance parameters you can set in order to approximate your target devices as closely as possible. These parameters include the heap size, the virtual machine speed, the refresh speed, and the network speed. You can set these parameters from KToolbar. The toolkit’s HTML documentation gives details on how to do it.

The bad news is that according to the emulator’s own documentation, “Setting the VM speed parameters does not emulate real device speed, even though a real device skin might be used. ” Furthermore, the documentation states, “Setting the network throughput speed does not emulate actual network transmission speed. ” So testing your game with a range of values for these two parameters will give you an idea of how your game will perform on various devices, but it’s not perfect. Additionally, it can be difficult to find out precisely what to set these parameters to in order to emulate a given device. The device manufacturer’s site usually has some product specs, but not always all the detail you’d like. If the product details aren’t in an obvious location, then your best bet is usually to go to Google and search for the device name plus specs. A good place to start to get a list of possible devices from various manufacturers to develop for is http://www.microjava.com.

If you have a particular device in mind, the next best thing to testing on the actual target device is to test on an emulator designed by the device’s manufacturer. Most major device manufacturers (including Nokia, Motorola, Ericsson, Samsung, and Siemens, to name a few) offer emulators or at least emulator skins you can download. As usual, you can find these by consulting the manufacturer’s Web site or Google. (Additionally, I found a comprehensive list of links to emulator downloads at the site http://www.jroller.com/page/shareme/J2MEEmulators.) These often have proprietary class libraries included, so you should probably avoid using a proprietary toolkit as your primary development tool unless you’re 100 percent certain you’re developing games for only one particular brand of device. On the other hand, if you have plenty of room on your development machine, it wouldn’t hurt to install some additional toolkits to fine-tune your games for particular devices.


Running Your Game on an Actual Cell Phone

The emulator is a helpful development tool, but even though it works well, it’s no substitute for testing your game on an actual device. Plus, playing your game on your own cell phone is the fun part! The idea of how to load the file onto the phone is pretty simple, but you need to be aware of a few details.

You have two ways to proceed. The first is to transfer files from your PC using a serial/Universal Serial Bus (USB) cable or an infrared connection. This option doesn’t require the data to even leave your house. The second option is more exciting. It consists of placing the required files on a server on the Internet and downloading them using a data connection from the phone—using Global System for Mobile Communications/General Packet Radio Service (GSM/GPRS).

Both methods have advantages. The first method doesn’t require you to make a call from the phone and is therefore completely free (except for the cost of the cable if one isn’t included with the phone). Also, since the transfer takes place entirely within your own network (generally behind your firewall), there’s no danger of your game being downloaded by unauthorized users. But this technique can place additional requirements on your local system. For example, in the case of Nokia, the software that’s used to perform the transfer works only on Windows, and the PC that the data is being loaded from must have an infrared port or a separate USB cable.

The second method, placing the games on a Wireless Application Protocol (WAP)–accessible Web page, is clearly preferable if you intend to distribute the games yourself—even if you’re distributing it only to your friends. After all, if you set it up so that you can download your game off the Internet, you can tell other people where it is and they can download it as well. For this option you need to be sure that your phone service contract includes WAP access and application downloading. (This is a typical option that’s offered with a Java-enabled phone, so the salesperson who sells you the phone will probably suggest it to you before you even have to ask about it.) You’ll also need a server on which to place the files. This shouldn’t be too difficult to come by since most Internet Service Providers (ISPs) offer some personal Web space with standard Internet access contracts. All additional software needed for this means of data transfer exists in free versions for all platforms.

In this book I’ll cover only how to transfer your games to the device through the Internet and not through direct file transfer because transferring the files directly is vendor dependent. If you’d like to transfer the files to your phone directly, then the first step is to go to the Web site of the phone’s manufacturer. In the case of Nokia, for example, the necessary software is easy to find on the site and is well documented. The same should be true of most other makers of Connected Limited Device Configuration (CLDC) devices.

Using WAP

WAP is the protocol that small devices use to access the Internet.

The principle of WAP is that your cell-phone provider makes available a gateway through which your phone can access the Internet. Since small screens make standard browser functions and standard HTML pages unusable, there’s another markup language specially designed for cell phones and other small devices called Wireless Markup Language (WML). If your phone contract specifies WAP access and doesn’t restrict browsing to some specific portal and sites, you can direct your phone to a WML page listing your MIDlets. From there you can download them. This is similar to a standard HTML Web page embedding a Java applet.

So, to prepare your games for download, you first need to upload them onto a Web server. You need to place the WML file on the server as well as the jar and jad files. In addition, you may have to perform some configuration so that the Web server, when accessed, returns a correct Multipurpose Internet Mail Extensions (MIME) type description for those files. Otherwise, the phone may be unable to recognize them. The following sections explain these steps in detail using the Nokia 6100 as the example phone.

Preparing the WML File

You can use WML to display interesting content by itself, offering User Interface (UI) elements such as forms, buttons, and so on. But you don’t need to do anything fancy to make a page from which your game can be downloaded. In fact, it’s better to resist the temptation to make a complex WML page because of the screen limitations of the target device. Try to keep it small and simple. Listing 1-5 shows a minimal example suitable for a download page. The file is called hello.wml.

Listing 1-5.hello.wml

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
  "http://www.WAPforum.org/DTD/wml_1.1.xml">
<wml>
<card id="hello">
<p>
Hello world!
</p>
<p>
<a href="hello.jad">Hello World App</a>
</p>
</card>
</wml>

Here’s how it works: The first two lines are mandatory to identify the file as WML. The content of the page must be enclosed between the <wml> opening tag and the </wml> closing tag. The <card> tag delimits one screen of data for the device (not much!). As you may guess if you know some HTML or Extensible Markup Language (XML), this page will display one line containing the text Hello world! and another line with a link with the text Hello World App, as shown in Figure 1-5.


Figure 1-5. The hello.wml WAP page running on the Nokia 6100

For this link to work, the server directory containing the file hello.wml must contain a jar file called hello.jar and a jad file called hello.jad.

The page can contain multiple game links, and you add them in the obvious way. For example, inside the enclosing <card> tags, you could add a second triple such as the following:

<p>
<a href="maze.jad">Amazing Maze!</a>
</p>

on the lines immediately following these lines:

<p>
<a href="hello.jad">Hello World App</a>
</p>

(Obviously, you must also upload the corresponding jar and jad files to the server for this link.) But if you have a large number of downloadable jar files, you’ll probably want to arrange them on a series of separate pages. If you have multiple versions of the same game suite that are optimized for different devices, it’s a good idea to make a separate WML page for each device rather than making a page for each game suite and having the page contain the versions for multiple devices. Also, it’s better to put the lengthy explanations of which version is which on a normal Web page and just put simple descriptive tags on your WML page to save the user the annoyance of excessive scrolling.

One word of warning: The <p></p> tags enclosing the links in the previous files aren’t optional. In the case of the Nokia 6100, the device failed to recognize the links without the <p></p> tags.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials