Friday, April 29, 2011

Creating Multiple Type List

This example is shows how to create the list which has multiple type choice
option. In this example user can make selection in name list and after selection
application will display message according to the selection. Like..........



 

Sandeep: selected


Kumar: selected

Suman: not selected

 

 

The MULTIPLE keyword is used to create the multiple type list as
follows:

list = new List("Multiple Option", List.MULTIPLE);

The application look like as follows:

 



Source Code Of MultipleList.java
 





import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;




public class MultipleList extends MIDlet implements CommandListener{

  private Display display;

  private Command exit,view;     


  private List list;       



  public MultipleList(){

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


    view = new Command("View", Command.SCREEN,2);

  }



  public void startApp(){


    display = Display.getDisplay(this);

    list = new List("Multiple Option", List.MULTIPLE);


    list.append("Sandeep"null);

    list.append("Kumar"null);

    list.append("Suman"null);


    list.addCommand(exit);

    list.addCommand(view);

    list.setCommandListener(this);   


    display.setCurrent(list);

  }



  public void pauseApp(){ }



  public void destroyApp(boolean unconditional){


    notifyDestroyed();

  }



  public void commandAction(Command c, Displayable s){


    String label = c.getLabel();

    if (label.equals("View")){

      boolean selected[] new boolean[list.size()];


      list.getSelectedFlags(selected);

      for (int i = 0; i < list.size(); i++)


        System.out.println(list.getString(i(selected[i

                           
": selected" ": not selected"));


    }else if (label.equals("Exit")){

      destroyApp(false);      

    


  }

}




Output:

Download Source Code

Creating Menu Using Canvas Class

This example shows how to create the menu and call the canvas class to show
the toggle message. The Toggle message will appear when user perform some action
like click on a button ("Show"). In this example we have used the
following method:

  • setColor()
  • fillRect()
  • getWidth()
  • getHeight()
  • getFont()
  • fontHeight()
  • fontWidth()
  • setFont()
  • drawString()
  • repaint()

The repaint() method is used to appear the string on Canvas form.

The application look like as follows:

 




Source Code For CanvasMenu.java




import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;




public class CanvasMenu extends MIDlet implements CommandListener{

  CanvasString canvas;

  private Command exit;

  private Command toggle;




  public CanvasMenu() {

    canvas = new CanvasString();

  }



  public void startApp() throws MIDletStateChangeException {


    Display.getDisplay(this).setCurrent(canvas);

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

    toggle = new Command("Show", Command.SCREEN, 1);


    canvas.addCommand(exit);

    canvas.addCommand(toggle);

    canvas.setCommandListener(this);

    canvas.repaint();

  }



  public void destroyApp(boolean unconditional){


    notifyDestroyed();

  }



  public void pauseApp(){}



  public void commandAction(Command c, Displayable s){


    String label = c.getLabel();

    if(label.equals("Show")){

      canvas.toggleString();

    else if(label.equals("Exit")) {


      destroyApp(false);

    }

  }

}



class CanvasString extends Canvas {


  boolean string = true;

  void toggleString() {

    string = !string;

    repaint();

  }




  public void paint(Graphics g) {    

    g.setColor(0xccff66);

    g.fillRect(00, getWidth(), getHeight());


    if(string) {

      Font font = g.getFont();

      int fontHeight = font.getHeight();

      int fontWidth = font.stringWidth("This is the Toggle Message");


      g.setColor(2230112);

      g.setFont(font);

      g.drawString("This is the Toggle Message", (getWidth()-fontWidth)/2,


      (getHeight()-fontHeight)/
2, g.TOP|g.LEFT);

    }

  }

}





Download Source Code

 

J2ME Canvas Example

A J2ME Game Canvas Example

This example illustrates how to create a game using GameCanvas class.
In this example we are extending GameCanvas class to draw the circle and
rotate the circle continuously. The GameCanvas class has following
methods:

  • flushGraphics():- This is the void type method, it flushes to
    display on the off-screen buffer.
  • flushGraphics(int x, int y, int width, int height):- This is the
    void type method, it flushes to display of specified region on the
    off-screen buffer. 
  • getGraphics():- This is used to get the graphics objects.
  • getKeyStates():- This is the integer type variable, it is used to
    find the states of the key. 
  • paint(Graphics g):- This is also the void type method, it is used
    to paint the canvas. 


Other commands, input event, etc  methods inherited from Canvas class.
The Canvas class has following methods:

  • getGameAction(int keyCode) 
  • getHeight()
  • getKeyCode(int gameAction) 
  • getKeyName(int keyCode)
  • getWidth()
  • hasPointerEvents()
  • hasPointerMotionEvents() 
  • hasRepeatEvents()
  • hideNotify() 
  • isDoubleBuffered()
  • keyPressed(int keyCode) 
  • keyReleased(int keyCode) 
  • keyRepeated(int keyCode) 
  • paint(Graphics g)
  • pointerDragged(int x, int y) 
  • pointerPressed(int x, int y) 
  • pointerReleased(int x, int y) 
  • repaint() 
  • repaint(int x, int y, int width, int height) 
  • serviceRepaints() 
  • showNotify()

 


Source Code of CanvasGame.java







import javax.microedition.lcdui.*;

import javax.microedition.lcdui.game.*;

import javax.microedition.midlet.*;



public class CanvasGame extends MIDlet{




  private Command back;

  private Display display;

  final SweepGame game = new SweepGame();




  public void startApp() {

    back = new Command("Back", Command.BACK, 0);

    game.start();

    game.addCommand(back);


    game.setCommandListener(new CommandListener(){

      public void commandAction(Command c, Displayable s) {

        game.stop();

        notifyDestroyed();

      }


    });

    display = Display.getDisplay(this);

    display.setCurrent(game);

  }



  public void pauseApp() {}




  public void destroyApp(boolean unconditional) {}

}



class SweepGame extends GameCanvas implements Runnable {


  private boolean move;

  private int radius;

  private int diameter;

  private int interval;




  public SweepGame() {

    super(true);

    radius = 0;

    diameter = 10;


    interval = 0;

  }

  public void start() {

    move = true;

    Thread t = new Thread(this);


    t.start();

  }

  public void stop() {

    move = false;

  }

  public void render(Graphics g) {


    int width = getWidth();

    int height = getHeight();

    g.setColor(183,251,121);

    g.fillRect(0, 0, width - 1, height - 1);


    int x = diameter;

    int y = diameter;

    int w = width - diameter * 2;


    int h = height - diameter * 2;

    for (int i = 0; i < 17; i=i+2) {


      g.setColor(((17 - i) * 15 - 7),20,((17 - i) * 15 - 7));

      g.fillArc(x, y, w, h, radius + i * 10, 10);


      g.fillArc(x, y, w, h, (radius + 180) % 360 + i * 10, 10);

    }


  }

  public void run() {

    Graphics g = getGraphics();

    while (move) {

      radius = (radius + 1) % 360;


      render(g);

      flushGraphics();

      try {

        Thread.sleep(interval);

      }

      catch (InterruptedException ie) {}

    }


  }

}





Download Source Code

List in J2ME

Exclusive List MIDlet Example

This example illustrates how to create a Exclusive List. The Exclusive List
is used to select only one list element at a time. The EXCLUSIVE Field
inherited from interface javax.microedition.lcdui.Choice. There are three
choices in this interface, that are:

  • EXCLUSIVE 
  • IMPLICIT 
  • MULTIPLE

The one line code of Exclusive Choice is as follows:

list = new List("Movies", Choice.EXCLUSIVE)

In the next example, you will see how to use implicit choice list 


The EXCLUSIVE List Look like as follows:

 




Source Code Of ExclusiveChoiceList.java

 




import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;



public class ExclusiveChoiceList extends MIDlet{


  private Display display;

  private List list;



  public ExclusiveChoiceList() {

    list = new List("Movies", Choice.EXCLUSIVE);


  }



  public void startApp(){

    display = Display.getDisplay(this);

    list.append("The Legend of Bhagat Singh"null);


    list.append("Mother India"null);

    list.append("Lagaan"null);


    list.append("Chak De.."null);

    list.append("Hum Aapke Hain Kaun"null);


    display.setCurrent(list);

  }



    public void pauseApp() {}



    public void destroyApp(boolean unconditional){


    notifyDestroyed();

  }

}





 

Download Source Code

Search This Blog

Total Pageviews