Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

                         

» View all related tutorials
Related Tags: c string image text io colors user color screen display click source cli message set create define show tex example

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.