Home J2me J2ME Canvas KeyPressed



J2ME Canvas KeyPressed
Posted on: November 13, 2008 at 12:00 AM
This tutorial is going to show you how to handle keypressed event in J2ME using canvas class. After going through the given example, you will be able to show different output against different keypressed actions.

J2ME Canvas KeyPressed

     

This tutorial is going to show you how to handle keypressed event in J2ME using canvas class. After going through the given example, you will be able to show different output against different keypressed actions. This example illustrates you how to set different game actions on the various keypressed codes that will help you to show different actions on different key clicks.

 Find out these game Action  used in this example within switch statement:

  • case UP:   message = "[UP]";   break;   
  • case DOWN:   message = "[DOWN]";   break;   
  • case LEFT:   message = "[LEFT]";   break;   
  • case RIGHT:   message = "[RIGHT]";   break;   
  • case FIRE:   message = "[FIRE]";   break;   
  • case GAME_A:   message = "[LEFT_UP]";   break;   
  • case GAME_B:   message = "[RIGHT_UP]";   break;   
  • case GAME_C:   message = "[LEFT_DOWN]";   break;   
  • case GAME_D:   message = "[RIGHT_DOWN]";   break;   
  • default:   message = "";   break;

And the following methods is used to paint the screen and text:

  • int width = getWidth();   
  • int height = getHeight();  
  • g.setColor(255, 0, 0);   
  • g.fillRect(0, 0, width - 1, height - 1);   
  • g.setColor(0, 0, 255);   
  • g.drawRect(0, 0, width - 1, height - 1);   
  • g.setFont(font);  
  • int x = width / 2;   
  • int y = height / 2;  
  • g.drawString(message, x, y, Graphics.BASELINE | Graphics.HCENTER);

The Application is as follows:

 

Source Code of KeyCanvas.java

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

public class KeyCanvas extends MIDlet{

  private Display display;

  public void startApp(){
  Canvas canvas = new CanvasKey();
  display = Display.getDisplay(this);
  display.setCurrent(canvas);
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){}
}

class CanvasKey extends Canvas{

  private Font font;
  private String message = "[PRESS KEY]";

  public CanvasKey(){
  font = Font.getFont(Font.FACE_PROPORTIONAL, 
   Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

  }

  public void paint(Graphics g){
  int width = getWidth();
  int height = getHeight();

  g.setColor(25500);
  g.fillRect(00, width - 1, height - 1);
  g.setColor(00255);
  g.drawRect(00, width - 1, height - 1);
  g.setFont(font);

  int x = width / 2;
  int y = height / 2;

  g.drawString(message, x, y, Graphics.BASELINE | Graphics.HCENTER);
  }

  protected void keyPressed(int keyCode){
  int gameAction = getGameAction(keyCode);

  switch(gameAction){
  case UP:
  message = "[UP]";
  break;
  case DOWN:
  message = "[DOWN]";
  break;
  case LEFT:
  message = "[LEFT]";
  break;
  case RIGHT:
  message = "[RIGHT]";
  break;
  case FIRE:
  message = "[FIRE]";
  break;
  case GAME_A:
  message = "[LEFT_UP]";
  break;
  case GAME_B:
  message = "[RIGHT_UP]";
  break;
  case GAME_C:
  message = "[LEFT_DOWN]";
  break;
  case GAME_D:
  message = "[RIGHT_DOWN]";
  break;
  default:
  message = "";
  break;
  }
  repaint();
  }
}

Download Source Code

Related Tags for J2ME Canvas KeyPressed:
cclassj2mediffeventiohelpcanvassedoutputgameclicktutorialkeyusingcliriathisactionaisetkeypressshowhandleifexampletoexamsseshcodeseildesactionscanliputvarindifferentasmntouttrcajafterclesthroughmehowhrrateratestorxaxampsessatkishallivmplpresspregoandaractcodcodestrvavarioustutorssrirenthshostabablafagainsthatfegapleplprpressedndodeono


More Tutorials from this section

Ask Questions?    Discuss: J2ME Canvas KeyPressed   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.