Tutorials‎ > ‎

J2ME Tutorial (Part II)

posted Dec 27, 2011, 12:43 AM by Reza Aggi Prasetyo   [ updated Aug 15, 2016, 11:31 PM by Surya Wang ]

High Level User Interface

 

Command and Alert

                Command Class represents the action of the information shown. Command is identical with button in other programming platforms. Command Class has some types such as: BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN, and STOP. Command Class also has priority level that describe the hierarchy of the command objects. Here is the implementation object of Command Class and object of Alert Class with the type alarm alert, confirmation alert, error alert, and warning alert.

 

MyCommand.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

 

public class MyCommand extends MIDlet implements CommandListener {

 

    private Display display = null;

    private Form form = null;

    private Command exit = null;

    private Command [] item = null;

    private Alert alert = null;

 

    public MyCommand()

    {

        form = new Form("My Command");

        exit = new Command("Exit",Command.EXIT,1);

        item = new Command[5];

        for(int i=0;i<item.length;i++)

        {

            item[i] = new Command("Item "+(i+1),Command.ITEM,1);

            form.addCommand(item[i]);

        }

        form.addCommand(exit);

        form.setCommandListener(this);

    }

    public void startApp() {

        if(display == null)

        {

            display = Display.getDisplay(this);

            display.setCurrent(form);

        }

    }

 

    public void pauseApp() {

    }

 

    public void destroyApp(boolean unconditional) {

    }

 

    public void commandAction(Command c,Displayable d)

    {

        if(c == exit)

        {

            notifyDestroyed();

            destroyApp(true);

        }

        else if(c == item[0])

        {

            alert = new Alert("Alarm","Ini command item 1, Tipe Alert Alarm",null,AlertType.ALARM);

            display.setCurrent(alert);

        }

        else if(c == item[1])

        {

            alert = new Alert("Confirmation","Ini command item 2, Tipe Alert Confirmation",null,AlertType.CONFIRMATION);

            display.setCurrent(alert);

        }

        else if(c == item[2])

        {

            alert = new Alert("Error","Ini command item 3, Tipe Alert Error",null,AlertType.ERROR);

            display.setCurrent(alert);

        }

        else if(c == item[3])

        {

            alert = new Alert("Information","Ini command item 4, Tipe Alert Info",null,AlertType.INFO);

            display.setCurrent(alert);

        }

        else if(c == item[4])

        {

            alert = new Alert("Warning","Ini command item 5, Tipe Alert Warning",null,AlertType.WARNING);

            display.setCurrent(alert);

        }  

    }

}

 

Description

 

public class MyCommand extends MIDlet implements CommandListener {

 

Here, we implemented the CommandListener interface because we will use some buttons to control this application.

 

       

exit = new Command("Exit",Command.EXIT,1);

        item = new Command[5];

        for(int i=0;i<item.length;i++)

        {

            item[i] = new Command("Item "+(i+1),Command.ITEM,1);

            form.addCommand(item[i]);

        }

 

This part is built by exit object with the Command.EXIT type and this object type of Command Class.

 

public void commandAction(Command c,Displayable d)

 

The commandAction method is used to define actions that will be done if objects of Command Class are pressed.

 

 

Form

                Form is a subclass of Screen. Form is a container for subclass items such as: TextField, StringItem, ImageItem, DateField, and ChoiceGroup. Form handles the layout of those components.

                Here is the example of ChoiceGroup component placed in a Form object. ChoiceGroup has some type of choice, such as: EXCLUSIVE, MULTIPLE, and POPUP.

 

MyChoiceGroup.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

 

public class MyChoiceGroup extends MIDlet implements CommandListener{

 

    private Display display = null;

    private Form form = null;

    private Command exit = new Command("Exit",Command.EXIT,1);

    private ChoiceGroup cg_exclusive = null,cg_multiple = null,cg_popup = null;

 

    public MyChoiceGroup()

    {

        form = new Form("My Choice Group");

        String [] elements = {"Choice 1","Choice 2","Choice 3"};

        cg_exclusive = new ChoiceGroup("Exclusive",ChoiceGroup.EXCLUSIVE,elements,null);

        form.append(cg_exclusive);

        cg_multiple = new ChoiceGroup("Multiple",ChoiceGroup.MULTIPLE,elements,null);

        form.append(cg_multiple);

        cg_popup = new ChoiceGroup("Popup",ChoiceGroup.POPUP,elements,null);

        form.append(cg_popup);

        form.addCommand(exit);

        form.setCommandListener(this);

    }

 

    public void startApp() {

        if(display == null)

        {

            display = Display.getDisplay(this);

            display.setCurrent(form);

        }

    }

 

    public void pauseApp() {

    }

 

    public void destroyApp(boolean unconditional) {

    }

 

    public void commandAction(Command c,Displayable d)

    {

        if(c == exit)

        {

            notifyDestroyed();

            destroyApp(true);

        }

    }

}

 

 

Low Level User Interface

 

Canvas

                Canvas is a subclass of Displayable which is the implementation of Low Level User Interface. Canvas is an abstract class that must be extended before an application can use its functions. Canvas can be combined with a high level Displayable subclass, which is Screen. Program can move to and from Canvas and Screen. Canvas describes empty handling event methods. Canvas Class describes an abstract method called paint(). An application that use Canvas Class must provide an implementation for paint() method.

 

MyCanvas.java

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

 

public class MyCanvas extends MIDlet implements CommandListener{

 

    private Display display = null;

    private scrCanvas scrCanvasObj = null;

    private Command exit = new Command("EXIT",Command.EXIT,1);

    private Command fullScreen = new Command("Full Screen",Command.OK,1);

 

    public MyCanvas()

    {

        scrCanvasObj = new scrCanvas();

        scrCanvasObj.addCommand(exit);

        scrCanvasObj.addCommand(fullScreen);

        scrCanvasObj.setCommandListener(this);

    }

 

    public void startApp() {

        if(display == null)

        {

            display = Display.getDisplay(this);

            display.setCurrent(scrCanvasObj);

        }

    }

 

    public void pauseApp() {

    }

 

    public void destroyApp(boolean unconditional) {

    }

 

    public void commandAction(Command c,Displayable d)

    {

        if(c == exit)

        {

            notifyDestroyed();

            destroyApp(true);

        }

        else if(c == fullScreen)

        {

            scrCanvasObj.setFullScreenMode(true);

        }

    }

}

 

srcCanvas.java

import javax.microedition.lcdui.*;

 

public class scrCanvas extends Canvas implements Runnable{

 

    private int x = 0,y = 0;

    private int ax = 1,ay = 1;

 

    public scrCanvas()

    {

        Thread thread = new Thread(this);

        thread.start();

    }

 

    private void blackBackground(Graphics g)

    {

        g.setColor(0x000000);

        g.fillRect(0, 0, this.getWidth(), this.getHeight());

    }

 

    private void myBox(Graphics g)

    {

        g.setColor(0xff0000);

        g.fillRect(x,y, 25,25);

    }

 

    private void writeString(Graphics g)

    {

        g.setColor(0x00ff00);

        g.drawString("Canvas Example", 1, 1, Graphics.LEFT | Graphics.TOP);

    }

 

    private void changeMyBoxPosition()

    {

        x+=ax;

        y+=ay;

        if(x<=0 || x+25>=this.getWidth())

            ax*=-1;

        if(y<=0 || y+25>=this.getHeight())

            ay*=-1;

    }

 

    public void paint(Graphics g)

    {

        blackBackground(g);

        writeString(g);

        myBox(g);

    }

 

    public void run()

    {

        while(true)

        {

            repaint();

            changeMyBoxPosition();

            try

            {

                Thread.sleep(1);

            }

            catch(Exception e)

            {

                e.printStackTrace();

            }

        }

    }

}

 

Description

                In this example, we use two main class, MyCanvas as the inherit of MIDlet Class and srcCanvas as the inherit of Canvas Class.  MyCanvas Class is a class that will be the Display container and srcCanvas Class will be the drawing system.

 

public class scrCanvas extends Canvas implements Runnable{

 

The scrCanvas Class extends Canvas the abstract class and implements Runnable Interface because in this scrCanvas Class there will be a working thread.

 

   

    public scrCanvas()

    {

        Thread thread = new Thread(this);

        thread.start();

    }

 

The constructor of srcCanvas contains the thread object initialization. When start() method in this thread object is called, then automatically the run() method in this class will be run.

 

   

    private void blackBackground(Graphics g)

    {

        g.setColor(0x000000);

        g.fillRect(0, 0, this.getWidth(), this.getHeight());

    }

 

    private void myBox(Graphics g)

    {

        g.setColor(0xff0000);

        g.fillRect(x,y, 25,25);

    }

 

    private void writeString(Graphics g)

    {

        g.setColor(0x00ff00);

        g.drawString("Canvas Example", 1, 1, Graphics.LEFT | Graphics.TOP);

    }

 

The functions above are used to do drawing object to canvas. Drawing system uses the object from Graphics Class.

 

   

    public void run()

    {

        while(true)

        {

            repaint();

            changeMyBoxPosition();

            try

            {

                Thread.sleep(1);

            }

            catch(Exception e)

            {

                e.printStackTrace();

            }

        }

    }

 

The run() method will be automatically called when the start() method is called from the thread object. In this run() method, there is calling of repaint() method that actually will call the paint(Graphics g) method.