Saturday, January 16, 2010

Basic Java ME Game Template

Jump to part: 1 | 2 | 3

The last tutorial showed you how to use Netbeans IDE to create a basic Java ME MIDlet, compile it, and preview your program in the emulator. Unfortunately, the code generated for the Hello MIDlet is not really suitable for making games that require constant screen updates and lots of animation or movement. This tutorial will show a simple framework that allows you to use lower level drawing functions for making games. The steps that will be taken are as follows:
  • Create an empty Java ME - Mobile Application project.
  • Create the main MIDlet.
  • Create a clsCanvas class which extends the GameCanvas class and implements the Runnable interface.
  • Create the main game loop which will contain all the drawing routines.
  • Launch a worker thread to start and run the main game loop.

Creating the Mobile Application project
First off, open Netbeans IDE and press CTRL+SHIFT+N to create a new project. Choose "Java ME" from the "Categories" list and "Mobile Application" from "Projects" list then click on the "Next" button.

New Project wizard

Change the project name to "myGame". What we need is an empty or blank project so rememeber to uncheck the "Create Hello MIDlet" option before clicking on the "Next" button.

Name and Location screen

On the "Default Platform Selection" screen, choose "DefaultCldsPhone1" for the "Device". Choose "CLDC-1.0" for the "Device Confirguration". Finally, choose "MIDP-2.0" for the "Device Profile" then click on the "Finish" button.

Default Platform Selection screen

You should now be looking at something like this:

The empty project

Creating the Midlet
Now we need to create the MIDlet ourselves. There are several ways to go about this.
  • By pressing CTRL+N on your keyboard.
  • By choosing "New File" from the "File Menu".

  • By right-clicking on the project panel treeview and selecting the "New" item, then clicking on "MIDlet".

For those of you who used one of the first two ways, select "MIDP" from the list of categories and "MIDlet" from the list of file types then click on the "Next" button.

New File wizard

On the "Name and Location" screen, type in "midMain" for "MIDlet Name". The "MIDlet Class Name" field will be automatically filled in for you. Leave the "MIDlet Icon" blank for now. As a rule of thumb, never create your files in the "default package". Enter "pkgGame" in the "Package" field. Click on the "Finish" button.

Name and Location screen

A folder with the same name as the package name you previously entered will be created inside the project source folder where all source code for your game should go.

MIDlet source code

Congratulations!! You have succeeded on creating an empty MIDlet that does absolutely nothing. Next stop, Creating the Canvas...


Car Dvd said...

Nice Post
Best Car Double Din

iam active said...

This is a wonderful post.thanks for sharing free pet catalogs by mail

Mehmood Ali said...

I have perused your article, it is exceptionally instructive and accommodating for me.I appreciate the significant data you offer in your articles. Much obliged for posting it..
Enhancing The Effectiveness Of Application Development

Unknown said...

An intriguing discussion is worth comment. I think that you ought to publish more on this subject matter, it might not be a taboo matter but generally people don't discuss these issues. To the next! Kind regards! How to get Live psl streaming ?

advrt kolan said...

keto trim diet plan
“I’ve gained a few of my weight back again because of letting myself have cheats here and there - especially during the holidays and being with friends and family! Right now, I’m going back to keto basics, and losing the weight that I had gained and, hopefully, I’ll be much more successful this time around,” Lele explains.

mohammed ababneh said...

Thanks for the great post