SunQuest
 
       Java
  Home arrow Java arrow Page 5 - Getting Started with J2ME
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Getting Started with J2ME
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2005-04-05

    Table of Contents:
  • Getting Started with J2ME
  • Creating the “Hello, World” Application
  • Using KToolbar
  • Optimizing Your Game for Different Devices
  • Configuring the Server

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

       · I have seen many web-sites on java. I have been searching J2ME tutorials. Rest may...
     

    Buy this book now. This article is excerpted from J2ME Games with MIDP2 by Carol Hamer (Apress, 2004; ISBN 1590593820). Check it out at your favorite bookstore today. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...

    Iron Speed






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway