Google
 

Friday, March 21, 2008

Key Event Example 01(Event Demo)

/*
* @(#)Tiles.java 1.6 00/05/24 Copyright (c) 2000 Sun Microsystems, Inc. All
* Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with the terms
* of the license agreement you entered into with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITSDERIVATIVES.
*/

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 Tiles extends MIDlet {

Board b;

public Tiles() {
b = new Board(this);
}

public void startApp() {
Display.getDisplay(this).setCurrent(b);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}

/*
* @(#)Board.java 1.14 00/05/23 Copyright (c) 2000 Sun Microsystems, Inc. All
* Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with the terms
* of the license agreement you entered into with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/

class Board extends Canvas implements CommandListener {

MIDlet midlet;
Command exitCommand;
Font font;

// Character Position
int xPos, yPos;

// Chracter Height and Width in pixels
int charW, charH;

public Board(MIDlet midlet_) {
int i;

midlet = midlet_;
Display dpy = Display.getDisplay(midlet);
int letterWidth = 4;

font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

charW = font.charWidth('M') + 7;
charH = font.getHeight() + 1;

xPos = (getWidth() - (letterWidth * charW) + 1) / 2;
yPos = 1;

exitCommand = new Command("Exit", Command.SCREEN, 2);
addCommand(exitCommand);

setCommandListener(this);
repaint();
}

public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
midlet.notifyDestroyed();
}
}

public void paint(Graphics g) {
g.setColor(0);
g.drawRect(4, 4, 4 * charW + 2, 4 * charH + 2);
}

public void keyPressed(int code) {
int game = getGameAction(code);

switch (game) {
case Canvas.UP:
System.out.println("Canvas.UP");
break;
case Canvas.DOWN:
System.out.println("Canvas.DOWN");
break;
case Canvas.LEFT:
System.out.println("Canvas.LEFT");
break;
case Canvas.RIGHT:
System.out.println("Canvas.RIGHT");
break;
}

switch (code) {

case Canvas.KEY_NUM0:
System.out.println("Key 0");
break;
case Canvas.KEY_NUM1:
System.out.println("Key 1");
break;
case Canvas.KEY_NUM2:
System.out.println("Key 2");
break;
case Canvas.KEY_NUM3:
System.out.println("Key 3");
break;
case Canvas.KEY_NUM4:
System.out.println("Key 4");
break;
case Canvas.KEY_NUM5:
System.out.println("Key 5");
break;
case Canvas.KEY_NUM6:
System.out.println("Key 6");
break;
case Canvas.KEY_NUM7:
System.out.println("Key 7");
break;
case Canvas.KEY_NUM8:
System.out.println("Key 8");
break;
case Canvas.KEY_NUM9:
System.out.println("Key 9");
break;
case Canvas.KEY_STAR:
System.out.println("Star Key");
break;

case Canvas.KEY_POUND:
System.out.println("Pound Key");
break;

// default:

// System.out.println( "default" );
// return;
}

}

}

0 comments: