Monday, October 15, 2007

Basic MIDP 2.0 Game Template Part 1

Jump to part: 1 | 2 | 3

Creating the Midlet

The last tutorial showed you how to use Netbeans IDE to create a basic MIDP 2.0 MIDlet, compile it, and preview your program in the emulator. Unfortunately, the code generated for the Hello MIDlet is of little or no use for making games. So in this tutorial you will be shown the basic code needed for creating your games.

First off, open Netbeans and create a new mobile application project. What we need is a blank project so remember to uncheck the "Create Hello MIDlet" on the "Name and Location" screen.

Project Name and Location Screen

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 New, then Midlet

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

Choose File Type

On the next 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 MyGame in the Package field. A folder with the same name will be created inside the project source folder where all source code for your game should go. Click on the "Finish" button.

MIDlet Name and Location

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


branajam said...

My name is James Branam and I'm the NetBeans Community Docs Manager. Your blog entry would make a fantastic tutorial for our Community Docs wiki ( Would you be willing to contribute it? If you need any help or have any questions, please contact me at I look forward to hearing from you.

branajam said...

Please contact me at


Anonymous said...

Hi, My name is Raja and i was going through your tutorials.when i tried to copy paste the example of custom menu of from your blog ,it shows nothing.I am using WTK,sony ericson...can you please tell me wts the pblm.


devlin said...

Hello Raja,

You need to download the basic project template found on that page : Link to Post. Unzip it and copy the images folder which contains the images used in the tutorial. The same tutorial will also explain where to place the images, which images to use and how to make use of them.

You may also want to read the Making A Full Screen Canvas tutorial if you're getting an exception error even before the images are loaded. This is just to see if the glitch applies to your phone.

Finally, if you can zip your project folder, upload, and post a link here, I can take a look at it and see what's actually going on.

Anonymous said...

Hii Devlin,
I tried to run the code sample u have uploaded on 1 december on wtk....but i am unable to open the project.i am using sony ericsons emulator...any help is highly regarded.

devlin said...

What version of Sun Java WTK are you using? It should be accessible no matter which emulator you use as long as you place it in the correct folder.

One way to deal with this is to make a new project in Sun Java WTK.
Enter "MyGame.midMain" without the quotes in the New Project dialog.

In the Settings, Change the "Target Platform" to "Custom" then choose MIDP 2.0 for the Profile and CLDC 1.0 for the Configuration.

Optionally, under the MIDlets section, you can change the MIDlet Icon property to "/images/icon.png".

After creating the project you can overwrite the "res" and "src" folder with the ones from the sample project you downloaded.

Tell me if you got it.

devlin said...

BTW, I updated the december 1 post and included a way to find the correct apps folder.

You can read more about it here: Alternate Project Templates for Sun Java Wireless Toolkit Users.

Anonymous said...


Yah finally got the solution.The templet is running fine on the emulator displaying a single black screen.

Sorry for the inconvenience caused..and thanks for the support ,i am going to use your tutorials in programming and let you know if i face any problem further..

Anonymous said...

Hii Devlin

I need some of ur help..I am developing one application where i hav to zoom in one image to 50% and after some commands to 100%...Basic thing is giving one zooming effect to the image...from smaller to larger...Can you help out or give me any suggestion regarding this..

any help will be highly appreciated.

devlin said...

One thing I can think of is to you use OpenGL/ES so you can use the image as a texture to a quad/rectangle and then you can just change the size of the quad to whatever you like. You can even rotate it, make it do flip flops, animate it to do whatever.

You also can use SVG images if your phone supports it : Link to Info.

I also found this tutorial from Sun Developer Network : Java ME Guideline: How to resize images to screen size. You can use the resize function to create different sizes of the image then show them one by one, like a frame-by-frame animation, from smallest to largest, to zoom-in. Then show them from largest to smallest to zoom-out.

Does that help?

Unknown said...

Hi Devlin,

I am Praveen. I am using TiledLayer for some purpose. I have one query on that.

I create an animated tile, and associate that animated tile index to one static tile index.

If animated TileIndex is -1, then do we need to have this value(-1) in the grid provided by the application(game). OR Is there any API that will add this value in the grid.

As I understand We can't generate the grid values.. It has to be provided by the application.
Could you please calrify me on this..

Thanks for the clarification.

devlin said...

Hi Praveen,

You should use the createAnimatedTile() to create an animated tile index. This method will let you create a virtual index that points to a tile in your tileset. The parameter is the real index of the tile in your tileset. You can then assign that virtual index to any part of the map using the setCell() method or you can also use the fillCells() to assign the value to a larger portion of the map.

Let's say that the virtual index is -1 and it points to tile 2. All the cells of the map with an index of -1 will display tile 2.

Let's say that the frames of the animation are tiles 2, 3 and 4 on your tileset. You can switch to a different frame by using setAnimatedTile() method like so:

setAnimatedTile(-1, 3);
// all cells of the map with -1 will now display tile 3

setAnimatedTile(-1, 4);
// all cells of the map with -1 will now display tile 4

I hope this makes it a little bit clearer :).