4)Simple Example Program
This section shows a simple application. The MIDlet will display the message “ Hi This is My First Program” on the screen. Clicking the Exit button will terminate the MIDlet. The application consists of two classes: the MIDlet subclass called FirstOne and the Canvas subclass called FirstCanvas.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | // **********FirstOne.java************
Package FirstOne;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class FirstOne extends MIDlet implements CommandListener
{
// The canvas is the region of the screen that has been allotted to the game.
FirstCanvas myCanvas;
private Command exitCommand = new Command("Exit", Command.EXIT, 99);
private Command changeCommand = new Command("changeMessage ", Command.SCREEN, 1);
// Initialize the canvas and the commands.
public FirstOne()
{
myCanvas = new FirstCanvas();
myCanvas.addCommand(exitCommand);
myCanvas.addCommand(changeCommand);
// Set one command listener to listen to all of the commands on the canvas
myCanvas.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException
{
// display my canvas on the screen:
Display.getDisplay(this).setCurrent(myCanvas);
myCanvas.repaint();
}
// If the MIDlet was using resources, it should release them in this method.
public void destroyApp(boolean unconditional)
throws MIDletStateChangeException { }
//MIDlet enters a paused state. The MIDlet should release shared resources.
public void pauseApp() { }
//Respond to a command. Either reset or exit.
public void commandAction(Command c, Displayable s)
{
if(c == changeCommand)
{
myCanvas.changeFirst();
}
else if(c == exitCommand)
{
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException ex){ }
}
}
}
// *************FirstCanvas.java.***************
package FirstOne;
import javax.microedition.lcdui.*;
//This class represents the region of the screen that has been allotted.
public class FirstCanvas extends Canvas
{
boolean firstDisp = true;
//Change the display message.
void changeMsg()
{
firstDisp = !firstDisp;
repaint();
}
//clear the screen and display the " Hi This is My First Program " message.
public void paint(Graphics g)
{
// get the dimensions of the screen:
int width = getWidth ();
int height = getHeight();
// clear the screen. Setting Screen Color to white:
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
// display the " Hi This is My First Program " message if appropriate:.
if(firstDisp)
{
Font font = g.getFont();
int fontHeight = font.getHeight();
int fontWidth = font.stringWidth("Hi This is My First Program ");
// set the text color
g.setColor(255, 255, 0);
g.setFont(font);
// write the string in the center of the screen
g.drawString("Hi This is My First Program ", (width - fontWidth)/2,
(height - fontHeight)/2,g.TOP|g.LEFT);
}
}
} |
5) Limitations with J2ME
Some ofthe limitations of J2ME compared with Core JAVA
- Some J2SE applications require classes that are not available in J2ME.
- Java applications won’t run in the J2ME environment without requiring modification to the code.
- . Devices that use the CDC configuration use the full Java Virtual Machine implementation, while devices that use the CLDC configuration use the Kjava Virtual Machine implementation.
- MIDlets are controlled by application management software (AMS). So we cant invoke a MIDLET like a J2SE Application.
6)Conclusion
Herewith we have an introduction to Java 2 Micro Edition. This example is very simle example. It can be executed using editors like NetBeans IDE or J2ME Wireless Toolkit(KToolBar). Using API’s like JSR 135 etc.. try to create more programs and try them in these tools. I will get back to you with more articles on J2ME.Refer this site for more information on starting with J2ME.






July 27, 2007
J2ME, Mobile