Smackall Games - Mobile Game Developer

Mobile Game Developer Articles

J2ME MIDP Development for NetBeans IDE 4.1 Quick Start Guide

This document takes you through the basics of using NetBeans IDE 4.1 to create a Java™ 2 Platform, Micro Edition (J2ME™ platform), Mobile Information Device Profile (MIDP) application. This document is designed to get you going as quickly as possible. More information about using the J2ME support in the NetBeans development environment is provided in the following documents:

The following short tutorial takes you through some of the basic steps of working with the new project system. We will show two ways to create a J2ME MIDP project named "MyHello" that displays the text "Hello World" in a device emulator.


Requirements

You must have NetBeans IDE 4.1 and the NetBeans Mobility Pack 4.1 installed before you can start J2ME MIDP development. See the J2ME MIDP Development Download page for instructions on downloading and installing the complete environment.


Creating a MIDP Application

The NetBeans IDE provides a wizard that enables you to quickly create a MIDP project. When creating the project, you can choose to develop your application in the Visual Mobile Designer or in the Source Code Editor.
Using the Visual Mobile Designe gives you the ability to graphically plan out the flow of the application and design the screens the application will use. The designer automatically creates the code for the application. One limitation of this approach, however, is that if you edit the generated code in the Source Editor, you cannot continue to edit the code in the Visual Designer. You also need to use callback methods to create preprocessor blocks for your project configurations.
Using the Source Code Editor, you manually create the code for your MIDlets. Creating code in the Source Code Editor gives you more flexibility when editing the code, and enables you to create preprocessor code blocks.
Creating a MIDP Application Using the Visual Mobile Designer


Creating a new J2ME MIDP project
  • Choose File > New Project (Ctrl-Shift-N). Under Categories, select Mobile. Under Projects, select Mobile Application and click Next.
  • Enter MyHello in the Project Name field. Change the Project Location to any directory on your system. From now on, we will refer to this directory as $PROJECTHOME.
  • Check the Set as Main Project and Create Hello MIDlet checkboxes (both are checked by default). Click Next.
  • Leave the J2ME Wireless Toolkit as the selected Target Platform.
  • Click Finish. The IDE creates the $PROJECTHOME./MyHello project folder. The project folder contains all of your sources and project metadata, such as the project Ant script.
    The application itself is displayed in the Flow Design window of the Visual Mobile Designer.

mobile game developer

Editing the Java Source Code

Now let's edit the text the MIDlet displays.

  • Click on Screen Design.
    This opens the Screen Designer window, and displays the helloTextBox[Textbox] screen, which is the only screen available in the application.
  • Click on the helloTextBox screen and type in some text. The default text is, "Hello, World!"

Compiling and Running a Project

  • Choose Run > Run Main Project (F6) from the Run menu. Double-click the Output window to maximize it so you can see all the output. Note that the HelloMIDlet.java file is built before it is executed. A device emulator opens to display the results of the executed MIDlet. The default device emulator is DefaultColorPhone.
  • In the device emulator window, click on the button below the Launch command. The device emulator launches the MIDlet and displays the text you entered in the source code.
  • Click on the button below Exit to close the MIDlet. Then click on the button in the upper right corner of the device to close the emulator window.

 

Creating a MIDP Application Using the Source Editor


Creating a new J2ME MIDP project
  • Choose File > New Project (Ctrl-Shift-N). Under Categories, select Mobile. Under Projects, select Mobile Application and click Next.
  • Enter MyHelloMIDlet in the Project Name field. Change the Project Location to any directory on your system. From now on, we will refer to this directory as $PROJECTHOME.
  • Check the Set as Main Project checkbox and remove the check from the Create Hello MIDlet checkbox. Click Next.
  • Leave the J2ME Wireless Toolkit as the selected Target Platform.
  • Click Finish. The IDE creates the $PROJECTHOME./MyHelloMIDlet project folder. The project folder contains all of your sources and project metadata, such as the project Ant script.
  • Right-click the MyHelloMIDlet node in the Explorer window and choose New > File/Folder.
  • Under Categories, choose MIDP. Under File Types, choose MIDlet. Click Next.
  • Enter HelloMIDlet as the MIDlet name. Click Finish. The HelloMIDlet.java is created.
  • Double click the HelloMIDlet.java to display the source code in the Editor.
  • Click in the Source Editor and change

public class HelloMidlet extends MIDlet

to

public class HelloMIDlet extends 
                  MIDlet implements javax.microedition.lcdui.CommandListener
{
  • Add the following text before the startApp() method:

public HelloMIDlet(){}
private void initialize() {
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_helloTextBox());
}
public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
if (displayable == helloTextBox) {
if (command == exitCommand) {
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
}
}
private javax.microedition.lcdui.TextBox get_helloTextBox() {
if (helloTextBox == null) {
helloTextBox = new javax.microedition.lcdui.TextBox(null, "Test String",120, 0x0);
helloTextBox.addCommand(get_exitCommand());
helloTextBox.setCommandListener(this);
}
return helloTextBox;
}
private javax.microedition.lcdui.Command get_exitCommand() {
if (exitCommand == null) {
exitCommand = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT, 1);
}
return exitCommand;
}
javax.microedition.lcdui.TextBox helloTextBox;
javax.microedition.lcdui.Command exitCommand;

Editing the Java Source Code

Now let's add some text for our MIDlet to display.

  • In the startApp() method, replace the "test string" code with the text of your choice. For example, "Hello World."

Compiling and Running a Project

  • Choose Run > Run Main Project (F6) from the Run menu. Double-click the Output window to maximize it so you can see all the output. Note that the HelloMIDlet.java file is built before it is executed. A device emulator opens to display the results of the executed MIDlet. The default device emulator is DefaultColorPhone.
  • In the device emulator window, click on the button below the Launch command. The device emulator launches the MIDlet and displays the text you entered in the source code.
  • Click on the button below Exit to close the MIDlet. Then click on the button in the upper right corner of the device to close the emulator window.

Changing the Emulator Platform

Changing the Default Emulator Device

You can create different project configurations to test your MIDlet on different emulator platforms.

  • Right-click the MyHello Project node and choose Properties. In the Properties dialog, choose the Platform node. You can change the device for the default configuration.
  • Click the Device dropdown menu and choose QwertyDevice. Click OK.
  • Run the application again, and the applicaton runs in the QwertyDevice emulator.

Adding a new Emulator Platform

  • Choose Tools > Java Platform Manager from the main toolbar.
  • In the Java Platform Editor dialog, click the Add Platform button.
  • In the Choose Platform Folder page of the Add Platform wizard, Use the Browse button to navigate to the parent directory of the emulator platform you want to install. For example, c:\WTK22. Click Next.
  • In the Configure the Platform Page, the IDE detects and configures the platform and displays information about the platform. Click finish to complete the configuration.
  • Choose File > "MyHello" Properties.
  • Choose the Platform node, then choose the name of the new emulator (for example, J2ME Wireless Toolkit 2.2) from the Emulator Platform dropdown menu. Click OK.

Advanced: Using Configurations

Adding a Configuration

You can use configurations to create more than one set of distribution JAR and Java Application Descriptor (JAD) files for your project. This enables you to create one set of source code and customize it for each mobile device you're programming for.

  • Right-click the MyHello Project node and choose Properties. In the Properties dialog, Choose Add Configuration from the Project Configuration combo box. This opens the Add Configuration dialog.
  • Enter a name for your configuration in the Configuration Name field. The name should identify the target platform on which you will deploy the JAR/JAD created for this configuration. For this example, enter BlackWhiteDisplay and click OK. You have just created a new configuration
  • Configurations can also be added by clicking the Manage Configurations button in the Properties dialog. This brings up the Project Configuration Manager dialog which contains options for adding, removing, or duplicating configurations.

Customizing a Configuration

You can add as many configurations as you would like to your project. You can then modify settings in the Project Properties dialog for each configuration that you've added.

  • Right-click the MyHello Project node and choose Properties. In the Properties dialog, choose the Platform node. This opens the J2ME MIDP Configuration dialog.
  • Choose the BlackWhiteDisplay configuration from the configuration combo box at the top of the Properties dialog.
  • The components in the Platform dialog are all disabled. This is because this configuration is currently taking the values used by the Default configuration for this panel. Uncheck the Use Default Project Configuration Values option at the bottom of the panel. All components on this panel are now editable.
  • Change the Device option to DefaultGrayPhone.
  • Toggle the Project Configuration dialog at the top of the Properties dialog and observe that the Device option changes based on which configuration is currently selected. This method of configuration customization works for each panel (other than the general panel) in the dialog. Click OK to save your configuration changes and exit the Properties dialog.

Creating an Ability

When you create an ability, you associate it with one or more project configurations that support a specific attribute or set of attributes. You can then associate the ability with one or more code blocks that support the attribute. If you later need to add or remove configurations, you won't have to go through all your code and associate the configuration with each code block. Instead, you need only to associate the configuration with an ability. The configuration is then automatically associated with any code block that is associated with the ability.

  • Right-click on the MyHello project node and choose Properties.
  • Choose the Abilities page from the Category menu tree.
  • Choose a configuration from the Project Configuration dropdown menu. This is the configuration with which the ability will be associated.
  • If necessary, uncheck the Use Values from "DefaultConfiguration" checkbox.
  • Click the Add Ability button.
    This opens the Add Ability dialog.
  • Click the New Ability button.
  • In the New Ability dialog, enter a name for the ability.
    Click Ok to close the New Ability dialog. The ability is now associated with the selected project configuration.

You can associate the ability with other configurations by choosing a different configuration from the Project Configuration menu and clicking the Add Ability button.

Adding Configuration- Specific Code (Preprocessor blocks)

Preprocessor blocks enable you to create, manage and track code that is specific to one or more project configurations or abilities. The code is enabled (or disabled) only for the configurations or abilities you identify.

  • The editor window for HelloMIDlet.java has a combo-box in the upper right corner which reads "Default Configuration" by default. Change this to your newly created configuration, BlackWhiteDisplay.
  • Right-click on the source code line where TextBox is instantiated. Right-click on the line and choose Preprocessor Blocks > Create If/Else Block. In the first, gray block, append the word "Gray" to the "Hello World" string. In the second, pink block, add "Color" to the "Hello World" string.
  • Toggle the configuration combo box in the upper right corner of the editor and notice that blocks are commented in or out based on which configuration is selected. The first block will be commented in whenever the selected configuration matches the value in the header and footer of the block. The second block will be commented in whenever the selected configuration does not match the value in the header and footer. This combo box also controls which properties from the Project Properties are used when taking action on the project.

Running Configurations

Configurations can be built and run individually or simultaneously.

  • Right-click the MyHello Project node and choose Run.
  • Activate a different configuration by right-clicking the MyHello Project node and choosing Set Active Project Configuration and then a choose a different configuration. This can also be done with the configuration combo box in each text editor. Run the project again.
  • Two emulators appear, one color and one gray. The gray emulator displays "Hello World Gray" and the color emulator displays "Hello World Color"

Creating a Client-Server Application

Using the Wireless Connection Wizard

The J2ME Wireless Connection Wizard enables you to generate the connecting classes for a mobile client/server application without hand-coding or using libraries.
To use the J2ME Wireless Connection Wizard, you need to have a mobile project to contain the generated MIDP code, and an opened web project containing at least one J2SE service class to be exported.
To create the client/server connection:

  • Open the Wireless Connection Wizard by selecting a Mobile Project from the Projects view and choosing File > New File, then choosing MIDP > Wireless Connection Wizard.
  • Select a name, web project and package for the generated server code.
  • Identify the services to be exported from the server to the client application.
  • Select a name and package for the generated client class.
  • Determine the types of code to be generated:
  • Stub methods for each exported service
  • Code enabling multiple calls per session
  • Tracing code
  • A sample MIDlet you can examine and modify

The wizard creates:

  • a J2ME client class
  • a servlet and supporting classes
  • a mapping file in xml format.
  • if you choose, a MIDlet you can examine and modify.

The J2ME client , mapping file and the MIDlet are generated under the J2ME project. The servlet and supporting classes are generated under the web project. The servlet is also added to the web application (web.xml).
After you have created the client and connection files, you cannot manually edit them. You can, however, regenerate them with different settings using the Settings file, which is displayed in the Editor after you generate the the original application, or when you open the Settings file in the Editor window.

Localizing Your Application

Adding Localization Support

  • Create the java classes that initialize the localization support and message property files.
    • Right-click the MyHello Project node and choose New > File/Folder.
    • Under Categories, select MIDP. Under File Types, select Localization Support Class. Click Next.
    • Accept the default values by clicking Finish. This creates the files LocalizationSupport.java and messages.properties.
  • Change the Internationalization String and Search formats.
    • Choose Tools > Options and click the Classic View button.
    • Select the Internationalization node in the Options menu tree.
    • In the properties window:
      • Change the value of the II8N String Format property to LocalizationSupport.getMessage("{key}")
      • Change the value of the II8N Search Format property to (getString|getBundle|getMessage)[:space:]*\([:space:]*{hardString}|//NOI18N
  • Add localization keys and their values.
    • Choose Tools > Internationalization > Internationalization Wizard. This opens the Internationalization wizard.
    • Select the HelloMIDlet MIDlet as the source file to internationalize. Click Next.
    • Select the Messages resource file as the file in which to store the values for the internationalized strings. Click Next.
    • Modify the Value fields for each key identified for the HelloMIDlet MIDlet.
    • After all the strings have been added to the messages.properties file, do the following to add values for each new locale:
      • Right-click on the file node in the Projects view and choose Add Locale.
      • Choose a locale you want to support from the Predefined Locales list box, or use the combo boxes at the top of the form to define a new locale.
      • Expand the message.properties node in the Projects view, and double-click on the newly-added locale.
      • Translate all properties into the appropriate language.
  • Add code to initialize localization.
    • Enter the following string in your MIDlet wherever you want a key localized:
      LocalizationSupport.getMessage("Key");
      For example, LocalizationSupport.getMessage("LAB_NextButton");

The mobile application will use the microedition.locale attribute to determine which version of the messages.properties file should be used.



Copyright © 2012 Smackall Games Pvt Ltd. All rights reserved.