当前页面: 开发资料首页 → Netbeans 专题 → J2ME MIDP Device Fragmentation Tutorial with Marv The Miner(1)
摘要: This tutorial shows how to configure the IDE to work with existing Java? 2, Micro Edition Platform (J2ME? Platform) MIDP projects and, specifically, how to use the NetBeans? IDE's built-in solution to the device fragmentation problem.
This tutorial shows how to configure the IDE to work with existing Java™ 2, Micro Edition Platform (J2ME™ Platform) MIDP projects and, specifically, how to use the NetBeans™ IDE's built-in solution to the device fragmentation problem. Following this tutorial from start to finish will illustrate the following:
The example program this tutorial uses is the popular J2ME game "Marv The Miner," which was released under GPL and written to run on several different mobile platforms.
This tutorial should take approximately two hours to complete and covers the following topics:
To follow this tutorial you need the MarvTheMiner_120_all.zip file, which contains the original source files of the program. Click here and download "Marv the Miner v1.2.0 (including level editor)" to obtain the source files.
This section describes how to set up your system before starting the tutorial.
The following software is required for the example:
Use the instructions provided at the download sites to install the software.
To start the NetBeans IDE:
The IDE-install-path variable stands for the path to the IDE's home directory.
The MarvTheMiner_120_all.zip file contains the source files of the original Marv The Miner application. The zip includes a MarvTheMiner directory, which contains the src and res subdirectories that you will use in this tutorial.
To unzip the MarvTheMiner_120_all.zip file:
File or Subdirectory
|
Description
|
src | The source files which make up the Marv The Miner program. |
res | Contains the resource files used by the program. This includes graphics as well as .map files which are used to define the game level's layouts. |
MarvEditor | A level editor (outside the scope of this tutorial). |
Nokia.full | A Nokia build of the game. |
MIDP.full | A generic MIDP build of the game. |
In this section, you set up and work with the existing project sources within the NetBeans IDE.
The first step is to create a new project in the NetBeans IDE.
The NetBeans device fragmentation solution is based on the use of project configurations. A project should have one project configuration for each distribution Jar/Jad you would like created for that project.
If you examine the file Main.java
, you will see that
the the authors support multiple platforms with a combination of
comments (for Nokia's FullCanvas) and Class.forname()
.
They also define a variable vendor that is used to track on which phone
the application is running. Based on this information, you can see
that they are supporting five different groups of phones:
As such, you will create four configurations to handle differences between these distributions.
NokiaSeries60
and press OK. Motorola
, Vodafone
,
and NokiaOther
. Click Close
to close the Manage Configuration dialog. The Project Configuration combo box at the top of the Project Properties page should now contain five configurations. We will use these configurations in a moment, but for now lets add some emulators into the IDE.
To run the project using the emulator platforms you have installed on your computer, you must first register them with the IDE.
Note that this dialog can also be accessed from the main menu at Toeols>Java Platform Manager.
Repeat these steps for each emulator platform you have installed on your machine and that you want to use from within the IDE.
Note that only emulators that comply with UEI standards will be detectable by the IDE. Unfortunately, this means that the Motorola 7.5 emulator that the developers used for Marv The Miner can not be detected. A later section in the tutorial will explain a work-around for using non-UEI emulators from within the IDE.
Each panel in the Project Properties page can be customized separately for each project configuration. By default, configurations will use the settings defined in the default configuration.
If you toggle between the each configuration, you'll see that all are now using the value specified in DefaultConfiguration except for the NokiaSeries60 configuration. If you change any of the configuration settings in the DefaultConfiguration Platform panel, the changes will propagate to all configurations except NokiaSeries60.
{motorola install
directory}\Emulator7.5\lib
.
Select each .zip
file (using ctl+mouse) and click Open.
The second part of the device fragmentation solution is the ability to specify certain blocks of code in your source files as being specific to one or more configurations. This is accomplished by using actions in the right-click context menu of the editor, or from the Edit > Preprocessor Blocks menu.
Here is the pertinent part of initStaticData()
:
vendor = 0; try { // Nokia Class.forName("com.nokia.mid.sound.Sound"); vendor = 1; try { Class.forName("com.nokia.mid.ui.DeviceControl"); com.nokia.mid.ui.DeviceControl.setLights(0,100); vendor = 2; } catch(Exception ex2){} } catch(Exception ex){} try { // Vodafone Class.forName("com.vodafone.v10.system.device.DeviceControl"); com.vodafone.v10.system.device.DeviceControl.getDefaultDeviceControl(). setDeviceActive(com.vodafone.v10.system.device.DeviceControl.BACK_LIGHT, true); vendor = 3; } catch(Exception ex){} try { // Motorola Class.forName("com.motorola.multimedia.Vibrator"); Class.forName("com.motorola.multimedia.Lighting"); com.motorola.multimedia.Lighting.backlightOn(); vendor = 4; } catch(Exception ex){}
// Nokia com.nokia.mid.ui.DeviceControl.setLights(0,100); // Vodafone com.vodafone.v10.system.device.DeviceControl. getDefaultDeviceControl(). setDeviceActive(com.vodafone.v10.system.device.DeviceControl.BACK_LIGHT, true); // Motorola com.motorola.multimedia.Lighting.backlightOn();
You now have three blocks of code that should be associated with your different configurations.
These code blocks are now associated with the configurations listed in the headers and footers of the preprocessor blocks. Changing the project's active configuration will trigger a preprocessor that will comment in and out blocks based on if the active configuration is a part of the preprocessor block's header and footer.
beep()
method. If you also condense
the if blocks, you are
left with the following code:if (midlet.muteSound == 0) { // Nokia 3650, 7650 com.nokia.mid.sound.Sound sound = new com.nokia.mid.sound.Sound(1000,100); sound.play(1); // All other Nokias com.nokia.mid.sound.Sound sound = new com.nokia.mid.sound.Sound(1000,100); sound.play(1); } if (midlet.useVibra == 1) { // Nokia com.nokia.mid.ui.DeviceControl.startVibra(50,500); // Vodafone SDK com.vodafone.v10.system.device.DeviceControl. getDefaultDeviceControl(). setDeviceActive( com.vodafone.v10.system.device.DeviceControl.VIBRATION, true); vibra_delay = 10; // Motorola 7.5 com.motorola.multimedia.Vibrator. setVibrateTone( com.motorola.multimedia.Vibrator.VIBRATE_LONG); com.motorola.multimedia.Vibrator.vibrateFor(500); }
You should be able to associate each block with the correct configuration. Remember to associate the Nokia vibration control call with both Nokia configurations.
We're almost done. The final step is to somehow handle Nokia's FullCanvas.
When you are done,
your class definition should appear like so: