Live Traffic

Our Story

Ceremony

Search

Just Married

How to use alphabets in canvas

by - April 26, 2010

Hi,

The following sample code helps you to print the alphabetical values in canvas. For example if u select the key 2 at first time it prints character ‘a’. If you select the same key again within a particular time period it prints b. Now your keypad works in canvas as like forms.



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

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author test
*/

public class UserInterface extends MIDlet {
private Display display;
public void startApp() {
display=Display.getDisplay(this);
canvas can=new canvas(this);
display.setCurrent(can);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}


class canvas extends Canvas implements Runnable {
private String text="";
String key="";
private int count2=0;
private int count3=0;
private int count4=0;
private int count5=0;
private int count6=0;
private int count7=0;
private int count8=0;
private int count9=0;
Font font;
Thread t;
public canvas(UserInterface midlet) {
setFullScreenMode(true);
}
public void paint(Graphics g) {
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255, 255, 255);
g.fillRect(20, 40, 100, 20);
g.setColor(255, 0, 0);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD, Font.SIZE_SMALL));
g.drawString(text, 25, 45, Graphics.LEFT | Graphics.TOP);
}
public void keyPressed(int keyCode) {
key=getKeyName(keyCode);
if(getKeyName(keyCode).equals("1")) {
text+=".";
}
if (getKeyName(keyCode).equals("2")) {
count2++;
if (count2==1) {
t = new Thread(this);
t.start();
text += "a";
repaint();
} else if (count2 == 2 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"b";
repaint();
} else if (count2 == 3 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"c";
repaint();
}
}
if (getKeyName(keyCode).equals("3")) {
count3++;
if (count3==1) {
t = new Thread(this);
t.start();
text += "d";
repaint();
} else if (count3 == 2 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"e";
repaint();
} else if (count3 == 3 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"f";
repaint();
}
}
if(getKeyName(keyCode).equals("4")) {
count4++;
if (count4==1) {
t = new Thread(this);
t.start();
text += "g";
repaint();
} else if (count4 == 2 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"h";
repaint();
} else if (count4 == 3 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"i";
repaint();
}
}
if(getKeyName(keyCode).equals("5")) {
count5++;
if (count5==1) {
t = new Thread(this);
t.start();
text += "j";
repaint();
} else if (count5 == 2 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"k";
repaint();
} else if (count5 == 3 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"l";
repaint();
}
}
if(getKeyName(keyCode).equals("6")) {
count6++;
t = new Thread(this);
t.start();
text += "m";
repaint();
} else if (count6 == 2 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"n";
repaint();
} else if (count6 == 3 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"o";
repaint();
}
}
if(getKeyName(keyCode).equals("7")) {
count7++;
if (count7==1) {
t = new Thread(this);
t.start();
text += "p";
repaint();
} else if (count7 == 2 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"q";
repaint();
} else if (count7 == 3 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"r";
repaint();
} else if (count7 == 4 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"s";
repaint();
}
}
if(getKeyName(keyCode).equals("8")) {
count8++;
if (count8==1) {
t = new Thread(this);
t.start();
text += "t";
repaint();
} else if (count8 == 2 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"u";
repaint();
} else if (count8 == 3 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"v";
repaint();
}
}
if(getKeyName(keyCode).equals("9")) {
count9++;
if (count9==1) {
t = new Thread(this);
t.start();
text += "w";
repaint();
} else if (count9 == 2 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"x";
repaint();
} else if (count9 == 3 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"y";
repaint();
} else if (count9 == 4 && t.isAlive()==true) {
text=text.substring(0,text.length()-1)+"z";
repaint();
}
}
if(getKeyName(keyCode).equals("0")) {
text+=" ";
repaint();
}
if(getKeyName(keyCode).equals("POUND")) {
}
if(getKeyName(keyCode).equals("ASTERISK")) {
}
if(getKeyName(keyCode).equals("SOFT1")) {
}
if(getKeyName(keyCode).equals("SOFT2")) {
}
if(getKeyName(keyCode).equals("SEND")) {
}
if(getKeyName(keyCode).equals("END")) {
}
if(getKeyName(keyCode).equals("LEFT")) {
}
if(getKeyName(keyCode).equals("RIGHT")) {
}
if(getKeyName(keyCode).equals("SELECT")) {
}
if(getKeyName(keyCode).equals("CLEAR")) {
text=text.substring(0,text.length()-1);
repaint();
}
}
public void run() {
if(key.equals("2")) {
try {
Thread.sleep(1600);
count2=0;
}
catch(Exception e) {
}
}
if(key.equals("3")) {
try {
Thread.sleep(1600);
count3=0;
}
catch(Exception e) {
}
}
if(key.equals("4")) {
try {
Thread.sleep(1600);
count4=0;
}
catch(Exception e) {
}
}
if(key.equals("5")) {
try {
Thread.sleep(1600);
count5=0;
}
catch(Exception e) {
}
}
if(key.equals("6")) {
try {
Thread.sleep(1600);
count6=0;
}
catch(Exception e) {
}
}
if(key.equals("7")) {
try {
Thread.sleep(1600);
count7=0;
}
catch(Exception e) {
}
if(key.equals("8")) {
try {
Thread.sleep(1600);
count8=0;
}
catch(Exception e) {
}
}
if(key.equals("9")) {
try {
Thread.sleep(1600);
count9=0;
}
catch(Exception e) {
}
}
}
}


link:http://j2mesamples.blogspot.com

You May Also Like

0 comments