Java me basics & game programming


Basics & Game Programming

Latest version tested: Sun Java Wireless Toolkit 2.5.2 for CLDC,NetBeans 6.1 Mobility Pack, CLDC 1.1, MIDP 2.1Last modified: October 13, 2008

You can develop Java ME program using either:

For serious programming, I certainly recommend NetBeans with Mobility Pack.

Bạn đang xem: Java me basics & game programming

There are quite few Java ME books, but tons of on-line learning resources. These are the steps that I recommend:

Download và install the Sun Java Wireless Toolkit or NetBeans with Mobility pack or both, and write a Hello-world program.Study the demos provided in Sun Java Wireless Toolkit và NetBeans Mobility Pack.

Install Sun Java Wireless Toolkit

Step 1: DownloadDownload Sun Java Wireless Toolkit for CLDC (formerly called J2ME wireless toolkit) from

Step 2: InstallRun the downloaded installer.

Step 3: Read DocumentationRead the installed documentation via "index.html" (in the installed directory). Read "Sun Java Wireless Toolkit for CLDC User"s Guide".

Step 4: Try the Demos"Start" button ⇒ Programs ⇒ Sun Java Wireless Toolkit 2.5.2 ⇒ xuất hiện Project... ⇒ choose a chạy thử ⇒ Build ⇒ Run.

Writing First Java ME Program

Step 0: Read DocumentationRead "Sun Java Wireless Toolkit for CLDC User"s Guide" chapters 1 and 2 (via "index.html" in the installed directory).

Step 1: Create a New ProjectLaunch wireless toolkit via "Start" button ⇒ Programs ⇒ Sun Java Wireless Toolkit 2.5.2 ⇒ new Project... ⇒ In "Project Name", enter "Hello". In MIDlet Class Name, enter "HelloMIDlet" ⇒ Create Project ⇒ OK ⇒ Take chú ý of the source file directory, e.g., "c:WTK2.5.2appsHellosrc".

Note: You can change the project directory by editing "$" to showroom the following property:

kvem.apps.dir = your-new-app-directoryStep 2: Write your Source CodeWrite the following source code, using a programming text editor, và saved as "" in the source tệp tin directory noted in the previous step.

// #1: Uses Java ME APIs (instead of Java SE)import javax.microedition.midlet.*;import javax.microedition.lcdui.*;// #2: First MIDlet lớn say "Hello, world"public class HelloMIDlet extends MIDlet implements CommandListener // #3: Declare instance variables private size mainForm; // for holding UI components private Display currentDisplay; // the current display screen private Command cmdExit; // exit command // #4: Called back by the Runtime khổng lồ start or resume (after being paused) the MIDlet public void startApp() mainForm = new Form("HelloMIDlet"); // allocate a size to hold the UI components mainForm.append(new StringItem(null, "Hello, world!")); // add a String component cmdExit = new Command("Exit", Command.EXIT, 0); // allocate the exit command mainForm.addCommand(cmdExit); // showroom the command mainForm.setCommandListener(this); // register "this" to handle command currentDisplay = Display.getDisplay(this); // retrieve the current display currentDisplay.setCurrent(mainForm); // use mainForm as the current display // #5: Called back by the Runtime lớn pause the MIDlet public void pauseApp() // bởi vì nothing // #6: Called back by the Runtime before the MIDlet is destroyed public void destroyApp(boolean unconditional) // vì nothing // #7: Handler for the Exit command public void commandAction(Command cmd, Displayable displayable) if (cmd == cmdExit) destroyApp(true); notifyDestroyed(); // put the midlet into destroy state Explanation:

A midlet (or Java ME program) (#2) extends from the abstract class javax.microedition.midlet.MIDlet. This class declares three abstract methods: startApp(), pauseApp() and destroyApp(). These are call-back methods, that will be invoked by the runtime (or Application Management Software) at the appropriate instance. You need lớn override these methods to lớn program the running behaviors of your midlet.startApp() (#4) is called by the runtime when the midlet is starting, or resuming after it is being paused. PauseApp() (#5) is called by the runtime khổng lồ pause the midlet. DestroyApp() (#6) is called the the runtime when the midlet is about khổng lồ be destroyed and removed from the memory.In the startApp() (#4), we create the mainForm (of javax.microedition.lcdui.Form) lớn place the "Hello, world" StringItem UI component. We địa chỉ cửa hàng a "Exit" command khổng lồ the mainForm, và set this class as the command listener for handling the commands. We then retrieve the current display (of javax.microedition.lcdui.Display) of this midlet, and set the mainForm as the current display.This class, as the command listener, needs to lớn implement the javax.microedition.lcdui.CommandListener interface (#2). This interface declares an abstract method commandAction(). We override this method (#7) lớn invoke notifyDestroy() in response to the "Exit" command, which will inform the runtime that this midlet has entered the destroy state.

Step 3: Build & Run the ProgramBuild ⇒ Run ⇒ Launch ⇒ Exit.

Using the NetBeans Mobility Pack

Step 0: Install NetBeansInstall NetBeans. Read "NetBeans HowTo" on how lớn install NetBeans & write program in NetBeans.

Xem thêm: Diễn Viên Hương Giang Sống Chung Với Mẹ Chồng " Nổi Bật Tại Mrs Vietnam 2018

Step 1: Install Mobility PackDownload NetBeans Mobility Pack CLDC/MIDP from Run the downloaded installer.

Step 2: Read và Read

Step 3: Write a "Hello-world" Program

refer to lớn the getting starting document & the source code above.

Step 4: Visual Programming

Compared with Sun Java Wireless Toolkit, NetBeans provides a GUI Builder (called Visual smartphone Designer) in addition to source coding. Follow the steps in "Quick Start Guide" khổng lồ use the Visual mobile Designer to lớn write a Hello-world Java ME applications.



Study the test codes in Sun Java Wireless Toolkit và NetBeans Mobility Pack.

Java ME, is easier than the Java SE và Java EE, simply because the APIs is very much smaller (because mobile devices has limited capability). For example, the java.lang in Java ME (CLDC 1.1) has 17 classes và 1 interface, compared with the java.lang of Java SE (JDK 1.6), which has 35 classes & 8 interfaces.

Java ME"s Configurations, Profiles và Optional APIs

Unlike Java SE, which is a single piece of software identical in all the operating platforms for developing desktop applications, Java ME is designed for mobile devices, which have limited & diverse computational power, memory, screen size, & networking capability. Due khổng lồ the large variety & diversity of mobile devices, it does not make sense khổng lồ design a one-size-fits-all solution and request all variety of mobile devices to tư vấn this piece of software.

Java ME is, therefore, divided into configurations, profiles, and optional APIs. A mobile device manufacturer is không lấy phí to choose what khổng lồ support, depending on the device"s capability.


A configuration specifies a Java Virtual Machine (JVM) and a base phối of core APIs for a certain class of devices. There are currently two configurations defined:

CLDC (Connected & Limited Device Configuration): for intermittently networked (connected) và limited capability devices such as thiết bị di động phone, smart phone, PDAs. CLDC specifies a JVM & core API packages java.lang,, java.util (which are subset of the Java SE counterparts) & (for networking support).CDC (Connected Device Configuration): for robust networked và more capable devices, such as high-end PDAs, set-top boxes. (CDC is beyond the scope of this writing.)

Configuration (CLDC & CDC), by itself, is incomplete for application development. A profile is defined on vị trí cao nhất of a configuration to provide specific APIs (such as user interface, persistent storage) khổng lồ make a complete application environment for developing applications. For example, MIDP (Mobile Information Device Profile) is build on vị trí cao nhất of CLDC; PDAP (PDA Profile) is build on đứng top of CDC.

Mobile devices may choose to tư vấn optional APIs, such as xanh tooth API, Wireless Messaging API (WMA 1.0 or 2.0) & many others, depending on their capability.

Xem thêm: Tut - Những Công Cụ Giúp Việt Hóa Mọi Game

Configurations and profiles are confusing. As a starter, you need not worry about how khổng lồ distinguish them. You simply need khổng lồ understand the combined APIs provided by the "MIDP for CLDC".