Pages

Subscribe:

Ads 468x60px

Saturday, July 31, 2010

Hello World Part 2

I think that everyone got the Hello World program running. Now, from this chapter I'm going to explain the coding part of the Hello World Program.

What is a MIDlet ?

MIDlet is the connection between the java application and the operating system in the MIDP(Mobile Information Device Profile) device. When you are creating a j2me application you are extending this MIDlet class ( javax.microedition.midlet.MIDlet ). Typical J2ME application must have extended at least one MIDlet in addition to all supporting classes which application uses.
MIDlet has three states in it's life cycle.

Let's take a look at MIDlet life cycle
MIDlet Life Cycle
    * Paused --> The MIDlet instance has been constructed and is inactive
    * Active  --> The MIDlet is active
    * Destroyed --> The MIDlet has been terminated and is ready for reclamation by the garbage collector.

 Basic MIDlet code

/*Code taken from http://developers.sun.com/ */
import javax.microedition.midlet.*;

public class BasicMIDlet extends MIDlet {
    public BasicMIDlet(){
    // constructor - don't do much here
    }

    protected void destroyApp( boolean unconditional )
          throws MIDletStateChangeException {
    // called when the system destroys the MIDlet
    }

    protected void pauseApp(){
    // called when the system pauses the MIDlet
    }

    protected void startApp()
          throws MIDletStateChangeException {
    // called when the system activates the MIDlet
    }
}

In my example hello world I have implements the CommandLinstner interface in order to write Command Button actions. Alert class is used  to notify your user about anything important. Here it is used for just a simple message. and the alert.setTimeout(Alert.FOREVER) so up until you press OK the alert will be on the screen forever. That's it for this program. Next time we are going to talk about J2ME Low level User Interfaces.
So try out this example code of J2ME Low Level Interfaces Simple Drawing Example.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.*;

/**
 * @author Aruna Sujith
 */
public class SimpleDraw extends MIDlet {
    Display myDisplay;
    DrawShape myDrawShape;

    public SimpleDraw() {
        myDisplay = Display.getDisplay(this);
            myDrawShape = new DrawShape();
    }

    public void startApp() {
        myDisplay.setCurrent(myDrawShape);

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

}

class DrawShape extends Canvas {

    public void paint(Graphics g) {
        int w = getWidth();
        int h = getHeight();
        g.setColor(0xffffff);
        g.fillRect(0, 0, w, h);
        g.setColor(0x000000);
        for (int x = 0; x < w; x += 10) {
            g.drawLine(0, w - x, x, 0);
        }

        for (int x = 0; x < w; x += 20) {
            g.setColor(128, 0, 255 * x / w);
            g.fillRect(x, h - 30, 20, 20);

        }

    }
}




Saturday, July 24, 2010

Hello World part 1

So here we are going to do our first program in J2ME Hello World...

First We need to install an IDE to code, debug and run our mobile applications. There are several IDE's available NetBeans, Eclipse, Borland JBuilder.. etc. I personally go for the NetBeans IDE though it takes huge amount of your RAM :) . I'm using NetBeans 6.1 it's a bit older version. I found that there is a bug in newer versions that you can't run multiple emulators at the same time. I haven't tried the latest release NetBeans 6.9. So the first thing you have to do is that download NetBeans (I highly recommend 6.1 rather than the latest versions since you find it very difficult when you come to WMAPI, Bluetooth.. etc ) and install it in to your computer. (Before installing NetBeans hope you guys have installed Java Development KIT or we all known as JDK .. Click here)
Start your IDE and do the following..

1. Create a new Project.  File->New Project or Ctrl+Shift+N or Click Create New Project
    Select Mobility --> MIDP Application (My Project name is HelloWorld)
Create New Project
2. Give a name for your project. Make sure to uncheck Create HelloWorld  MIDlet
Create MIDP Application
3. Platform ,Device, Configuration , Profile Selection
Default Platform Selection
4. Create a package (My package name is hello)
Create a package
5. Create a new Midlet (My MIDlet name is Hello)

6. Now you have created a Midlet now lets start coding our first Mobile Application. This is a very simple program and when you press the "OK" button it pop up an Alert (I'll explain what is an Alert in Hello World  part 2 ). And when you press the "EXIT" button application will be closed. 
The code snippet is given below..

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Aruna Sujith
 */
public class Hello extends MIDlet implements CommandListener {

    Display myDisplay;
    Form myForm;
    Command ok;
    Command exit;

    public Hello() {
        myDisplay = Display.getDisplay(this);
        myForm = new Form("Hello");
        ok = new Command("OK", Command.OK, 1);
        exit = new Command("EXIT", Command.EXIT, 0);
        myForm.addCommand(ok);
        myForm.addCommand(exit);
        myForm.setCommandListener(this);

    }

    public void startApp() {
        myDisplay.setCurrent(myForm);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable arg1) {
        if (c == ok) {
            Alert alert = new Alert("Hello World!", "Hey It Really works!!!!", null, AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            myDisplay.setCurrent(alert, myForm);

        } else if (c == exit) {
            destroyApp(true);
            notifyDestroyed();
        }
    }
}

To build the project; right click on the project --> click "Build" to build the project
To run the Peoject: right click on the project --> click "Run" to run the project or simply press F6 :-)
If you have completed the above instructions when you run the project you should have a emulator started.

The Emulator
Launch the program you will see two buttons press "OK" and "Exit" and see what happens. So this is it this is it. This is a simple program  and I' will explain more about the code and theory  in my next post " Hello World part 2 ". If you have any problem while doing this please put a comment. I'll answer as soon as possible to the best of my knowledge. Hope you'll enjoy programming mobile devices in J2ME .You can even try out this program in your own mobile. See you around... :-)  

Wednesday, July 21, 2010

Introduction to Java 2 Micro Edition

Before start programming using J2ME technology I think it's better explaining what is J2ME and why it is so popular. Actually what is java J2ME? Is it only for java enabled mobile phones?... The answer is NO.
" Java ME is designed to provide portability of applications between platforms " this is what java officail website says about J2ME.
In late 1998 Sun Microsystems (now a subsidiary of Oracle Corporation) focused on developing Mobile devices and entertainment devices. This was originally named as The Oak Project. As a result Sun released PersonalJava  also abbreviated to pJava to be used on mobile and other resource constrained devices. This is the birth of J2ME. (However the pJava has discontinued now).

See the above picture. It shows java technology stack.We all know that java is running on a virtual machine. In J2ME it also running on a virtual machine except for mobile phones and for some PDA's (Personal Digital Assistant) Since the limitations of these devices they use a virtual machine called KVM to run java applications.

Java 2 Micro Edition mainly divided into three different sections.
            
       1.Configurations                                                                                                                  
Configuration is a set of hardware functionality available to a mobile device. Processor type and speed, amount of memory available, network connections available to the device etc. These configurations are totally dependent on their manufactures. Currently  there are  two types of  configurations available for J2ME. 
    • Connected Limited Device Configuration ( CLDC )
    This CLDC is defined for the lowest electronic consumer devices. Typically a low end mobile phone or a PDA with following specifications.

    * 128KB of memory for run java programs
    * 32KB of memory for run time allocation
    * Limited user interface etc.

     There are two versions of CLDC which is CLDC 1.0 and CLDC 1.1 . Before programming mobile applications you must have a clear idea of these two versions. Some unique characteristics of CLDC 1.0 and 1.1 are listed below
    1.  Floating point support added in CLDC 1.1, ( not present in 1.0 )
    2.  The minimum memory budget has been raised from 160KB to 192KB to support the Floating point  capabilities
    3.  Methods like equalsIgnoreCase(), insert() append() (in StringBuffer class) , Thread.interrupt() Thread.getName() are present in CLDC 1.1
    4. The final thing you have to remember is that CLDC 1.0 is fully backward compatible with CLDC 1.1 which says that CLDC 1.0 Midlets will run on CLDC 1.1 devices. 
    • Connected Device Configuration( CDC )  
    CDC requires much more advance device configuration than in CLDC. Here are some of the features which is compatible with CDC.

    * 32 Bit CPU or above
    * 2 MB RAM or above
    * 2.5 MB ROM or above

    CDC configurations can be seen in Smart Phones, Navigation Systems etc. 

      2.Profiles
    Profile  is a API (Application Programming Interface ) for a specific domain. Profiles are built on top of configurations. Certain Profiles are defined according to their configurations MIDP or the Mobile Information Device Profile  is the most talked about when comes to java mobile programming. 

       3.Optional Packages
    There are several Optional Packages available  such as Wireless Messaging API (WMAPI), Mobile Media API (MMAPI), J2ME Web Services API, Bluetooth API etc. We will talk more about these in later posts. 

    Now I think you have some idea about J2ME and what is it meant for. In later chapters we are going to talk about Midlets and their functionality. Afterwards we gonna have a hell lot of coding. Don't get upset I'm just beating around the bush, you to be familiar with the jargons that are coming in near future.  

    Sunday, July 18, 2010

    Exploration Begins...

    We all search for something.. Is it Something which we need to satisfy our selves? ore is it to satisfy others? or it may be the something to satisfy both. Someone can ask the question that what the hell is this guy talking about is he crazy or something.. No.. I'm talking about the unsatisfied us..We all are knowledge hunters. But are we really keen on hunting?.. I think it depends..

    So this is my first ever blog. I am hoping to put some articles about mobile technology. Most of my batch mates even ask me to share some ideas on how to develop a mobile application using java micro technology or we also known as J2ME . If there are any kind of queries please free to ask anything..  Anything regarding J2ME ;). So lets get start doing mobile programming....


    Oh one last thing I have to say... If you want to be a really good programmer.. (Not the best programmer, It'll be a different story.. ) You have explore things beyond what I put here .. That's the story behind  "Exploration Begins"... My personal notion is that experiments will make you perfect..