Home arrow Java arrow Page 5 - 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 - Configuring the Server
(Page 5 of 5 )

Many Web servers arenít configured by default to recognize the file types associated with WAP/WML/J2ME by the file extensions. If youíre lucky, just uploading the files (described in the previous section) to a directory in the public area of the server will be sufficient. If youíre not, the cell phone will complain that the files are in an unrecognized format (even though the jad file is just text...). If you run into this problem, youíll need to do a little bit of server configuration. Iíll explain what to do in the case of the popular Apache server.

If the Apache server is running on your own machineówhich is a convenient option if you have cable or Asymmetric Digital Subscriber Line (ADSL)óall you need to do is update the httpd.conf file. (On Red Hat 9, this file is located at /etc/httpd/conf/httpd.conf.) Just add the following lines to the file:

#### WAP/WML/JAD
##
AddType text/vnd.wap.wml wml
AddType text/vnd.wap.wmlscript wmls
AddType application/vnd.wap.wmlc wmlc
AddType application/vnd.wap.wmlscriptc wmlsc
AddType image/vnd.wap.wbmp wbmp
AddType text/vnd.sun.j2me.app-descriptor jad
AddType application/java jar
####

Then you must restart the server to make this information available. On Red Hat 9 you can restart the server by typing the following command as root:

# service httpd restart

If the Web server youíre using belongs to your ISP, first check if the server is already configured to recognize the required types. (To check, create the page and then try to access it with your cell phone as described in the following section.) If youíre using Web space made available by the cell-phone provider, then thereís a high probability that the ISP has already taken care of the proper configuration.

If the server hasnít been configured correctly, you may be able to fix it yourself. Hereís what to do if your ISP uses an Apache server (other servers may have similar tricks that you can find by consulting the documentation or Google...): You wonít be allowed to change the main configuration file, but you can inform Apache of the correct MIME types by placing the same lines as previously in a file called .htaccess somewhere in your Web space area. (Note that you must put such a file in every directory in which you place WAP/WML/J2ME files.) Hereís the file .htaccess:

#### WAP/WML/JAD
##
AddType text/vnd.wap.wml wml
AddType text/vnd.wap.wmlscript wmls
AddType application/vnd.wap.wmlc wmlc
AddType application/vnd.wap.wmlscriptc wmlsc
AddType image/vnd.wap.wbmp wbmp
AddType text/vnd.sun.j2me.app-descriptor jad
AddType application/java jar
####

Of course, you canít ask the ISP to restart its server, but it isnít necessary because Apache will notice the new file automatically. Itís possible that the main configuration (which you donít control) forbids this type of user-directed overriding. In that case, youíll have to ask the ISP to change its policy or use another ISP.

Accessing the WML File and Downloading Applications

Now for the fun part: downloading the games onto the phone! Recall that in this section Iíll use the Nokia 6100 as an example. This is a typical CLDC-enabled phone, so you can apply these same ideas to other devices without too much difficulty.

Remember, you need WAP access as mentioned previously. Be sure to check the costs involved in your contract with WAP connections. For friends who simply download your games and keep them in their phones, this is unlikely to be expensive since MIDlets are quite small and the time required for downloading them will rarely exceed a single minute. For the developer, however (thatís you!), itís best to get a contract that has a fixed price with unlimited WAP usage since youíll certainly have to perform this operation a number of times (unless youíre placing your games on your phone using a direct PC connection during the development phase).

Before you start, you must verify that the WAP access is configured on the phone. This should have been done when you got the phone, or you should have documentation from your phone service and WAP provider giving the details. In my case, I configured the phone by going to a particular Web site and giving the phone number and a PIN code. The server then sent a Short Message Service (SMS) message containing all required information to the phone, which responded by prompting me through the procedure of entering the correct settings. On the Nokia phone you can view or edit the settings by selecting the menu item Menu -> Services -> Settings -> Edit Active Service Settings.

You can then connect to the hello.wml page by selecting the menu item Menu -> Services -> Go To and typing the URL just as you would for a regular Web page. So, for example, if your domain name is frog-parrot.net, then youíd type http:// frog-parrot.net/hello.wml if the hello.wml file is in the top-level directory. (If your hello.wml is in a subdirectory, add the names of the subdirectories to the URL just as you would for any other URL.)

If youíre using your own server at home and connecting through cable or ADSL, then you may not have a nice domain name, but you should still have an Internet Protocol (IP) address to which the phone can connect. Depending on how your connection works, your IP address may change from time to time. The operating system will tell you what your current IP address is. In the case of Linux you can find out by looking for the inet addr in the output you get from typing the following command:

$ /sbin/ifconfig ppp0

If your address is just a set of numbers, it still works perfectly well in the URL. Suppose, for example, that you entered the previous command and you got the following output:

ppp0 Link encap:Point-to-Point Protocol
     inet addr:81.49.195.43 P-t-P:193.253.160.3 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1
RX packets:1108 errors:0 dropped:0 overruns:0 frame:0
TX packets:1097 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:798514 (779.7 Kb) TX bytes:98348 (96.0 Kb)

In this case, the URL to enter into your phone is http://81.49.195.43/hello.wml.

Merely typing the URL into the phone can be tricky! In the case of the Nokia 6100 you can get a list of symbols by hitting the asterisk+plus key (*+). Navigate through this list using the right and left arrow keys until you find the desired symbol (such as . or / for a URL), and then select Use. Fortunately, the fact that the period (.) is the default symbol may save you a little effort. If you have a fixed IP address or domain name, you can also save yourself some typing by making a bookmark to your page.

Once the URL is entered, click OK, and the phone should open your WML page! From here you can click the link to your game, and your phone will download and install it.


Making Image Files

If youíre wondering where all the image files in this book came from, I drew them myself with a free program called the gimp, which you can download from http://www.gimp.org/download.html. If you decide to draw your image files with the gimp and youíd like them to have transparent backgrounds, be sure to select a transparent background when you first create a new image file. (A transparent background is nice for game objects because you donít want the rectangular frame of one game object obscuring another game object.) Then make sure you select Save Background Color when you save the file. Also, you should save it with the .png extension so that the gimp will save it in the right format to be used by a J2ME game.

One thing to keep in mind when making images is that the difference in screen size from one device to another is the factor thatís likely to break your game most dramatically when you try to port it from one device to another. With very small screens every pixel counts, so a screen size difference that seems insignificant can translate to a major problem for a game. To make your game more portable, you should of course avoid using hard-coded numerical values when drawing the graphics. Additionally, it helps to make different versions of your images in different sizes. If you have only a few image files, itís probably OK to have the game dynamically choose which images to use based on screen calculations, but if you have quite a number of image files, itís usually preferable to maintain different versions of the gameís jar file for different devices. Graphics in the png format tend to be pretty small, but they can add up quickly and therefore significantly impact the size of your jar.



DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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