Key MIDlet : Key Event « J2ME « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » J2ME » Key EventScreenshots 
Key MIDlet
Key MIDlet

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;

public class KeyMIDlet extends MIDlet {
  public void startApp() {
    Displayable d = new KeyCanvas();

    d.addCommand(new Command("Exit", Command.EXIT, 0));
    d.setCommandListener(new CommandListener() {
      public void commandAction(Command c, Displayable s) {
        notifyDestroyed();
      }
    });

    Display.getDisplay(this).setCurrent(d);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }
}

class KeyCanvas extends Canvas {
  private Font mFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

  private String mMessage = "[Press keys]";

  public KeyCanvas() {
  }

  public void paint(Graphics g) {
    int w = getWidth();
    int h = getHeight();

    g.setGrayScale(255);
    g.fillRect(00, w - 1, h - 1);
    g.setGrayScale(0);
    g.drawRect(00, w - 1, h - 1);

    g.setFont(mFont);

    int x = w / 2;
    int y = h / 2;

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

  protected void keyPressed(int keyCode) {
    int gameAction = getGameAction(keyCode);
    switch (gameAction) {
    case UP:
      mMessage = "UP";
      break;
    case DOWN:
      mMessage = "DOWN";
      break;
    case LEFT:
      mMessage = "LEFT";
      break;
    case RIGHT:
      mMessage = "RIGHT";
      break;
    case FIRE:
      mMessage = "FIRE";
      break;
    case GAME_A:
      mMessage = "GAME_A";
      break;
    case GAME_B:
      mMessage = "GAME_B";
      break;
    case GAME_C:
      mMessage = "GAME_C";
      break;
    case GAME_D:
      mMessage = "GAME_D";
      break;
    default:
      mMessage = "";
      break;
    }
    repaint();
  }
}
           
       
Related examples in the same category
1.Key Event DemoKey Event Demo
2.Low-Level Display Canvas:Key Code Example Low-Level Display Canvas:Key Code Example
3.Event Example 1Event Example 1
4.Event Example 2Event Example 2
5.Game Key EventGame Key Event
6.Key Event with CanvasKey Event with Canvas
7.Canvas for processing key code and commandsCanvas for processing key code and commands
8.Canvas for processing game actionsCanvas for processing game actions
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.