import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class
KeyCodeExample extends MIDlet
{
private Display d;
private MyCanvas c;
public KeyCodeExample()
{
d=Display.getDisplay(this);
c=new MyCanvas(this);
}
protected void startApp()
{
d.setCurrent(c);
}
protected void pauseApp()
{
}
protected void destroyApp(boolean b)
{
}
public void exitMIDlet()
{
destroyApp(true);
notifyDestroyed();
}
}
class MyCanvas extends Canvas implements CommandListener
{
private Command exit;
private String direction;
private KeyCodeExample kce;
public MyCanvas (KeyCodeExample kce)
{
direction="2=up 8=dn 4=lt 6=rt";
this.kce=kce;
exit=new Command("Exit", Command.EXIT,1);
addCommand(exit);
setCommandListener(this);
}
protected void paint(Graphics g)
{
// g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
// g.fillRect(10,5,getWidth(),getHeight());
//g.fillRect(0,0,0,0);
// g.fillRect(0,0,5,20);
g.setColor(255,0,0);//IT IS NOT VISIBLE IF U OMIT
// g.drawString(direction,0,0,Graphics.TOP|Graphics.LEFT);
// g.drawString(direction,0,0,Graphics.TOP|Graphics.RIGHT);//IT GOES TO LEFT
// g.drawString(direction,0,0,0);
g.drawString(direction,4,15,Graphics.TOP|Graphics.LEFT);
}
public void commandAction(Command c, Displayable dd)
{
if(c==exit)
{
kce.exitMIDlet();
}
}
protected void keyPressed(int key)
{
switch(key)
{
case KEY_NUM2:
direction="up";
break;
case KEY_NUM8:
direction="down";
break;
case KEY_NUM4:
direction="left";
break;
case KEY_NUM6:
direction ="right";
break;
}
repaint();
}
}
import javax.microedition.lcdui.*;
public class
{
private Display d;
private MyCanvas c;
public KeyCodeExample()
{
d=Display.getDisplay(this);
c=new MyCanvas(this);
}
protected void startApp()
{
d.setCurrent(c);
}
protected void pauseApp()
{
}
protected void destroyApp(boolean b)
{
}
public void exitMIDlet()
{
destroyApp(true);
notifyDestroyed();
}
}
class MyCanvas extends Canvas implements CommandListener
{
private Command exit;
private String direction;
private KeyCodeExample kce;
public MyCanvas (KeyCodeExample kce)
{
direction="2=up 8=dn 4=lt 6=rt";
this.kce=kce;
exit=new Command("Exit", Command.EXIT,1);
addCommand(exit);
setCommandListener(this);
}
protected void paint(Graphics g)
{
// g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
// g.fillRect(10,5,getWidth(),getHeight());
//g.fillRect(0,0,0,0);
// g.fillRect(0,0,5,20);
g.setColor(255,0,0);//IT IS NOT VISIBLE IF U OMIT
// g.drawString(direction,0,0,Graphics.TOP|Graphics.LEFT);
// g.drawString(direction,0,0,Graphics.TOP|Graphics.RIGHT);//IT GOES TO LEFT
// g.drawString(direction,0,0,0);
g.drawString(direction,4,15,Graphics.TOP|Graphics.LEFT);
}
public void commandAction(Command c, Displayable dd)
{
if(c==exit)
{
kce.exitMIDlet();
}
}
protected void keyPressed(int key)
{
switch(key)
{
case KEY_NUM2:
direction="up";
break;
case KEY_NUM8:
direction="down";
break;
case KEY_NUM4:
direction="left";
break;
case KEY_NUM6:
direction ="right";
break;
}
repaint();
}
}