|
Friday, 18 June 2010 09:22 |
// description of your code here
the code to add menubar in j2me app
// insert code here..
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mobilePackage;
/**
*
* @author Himanshu
*/
import java.util.*;
import javax.microedition.lcdui.*;
public class Menu {
private String leftOption; // will be displayed when menu is inactive
private String rightOption; // will be displayed when menu is inactive
private String cancelOption = "Cancel"; // also may be "Back" or something else
private String[] menuOptions;
private int padding = 5; // just like in CSS
/**
* Creates a new instance of Menu.
*/
public Menu(String leftOption, String rightOption, String[] menuOptions) {
this.leftOption = leftOption;
this.rightOption = rightOption;
this.menuOptions = menuOptions;
} // end constructor
public void drawInactiveMenu(Canvas canvas, Graphics g) {
// create inactive menu font
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
int fontHeight = font.getHeight();
// clear inactive menu background
int width = canvas.getWidth();
int height = canvas.getHeight();
g.setColor(0xcccccc); // grey color
g.fillRect(0, height - fontHeight - 2 * padding, width, height);
// draw left and right menu options
g.setFont(font);
g.setColor(0x000000); // black
g.drawString(leftOption, padding, height - padding, g.LEFT | g.BOTTOM);
if (!(rightOption.compareTo("")==0))
{
g.drawString(rightOption, width - padding, height - padding, g.RIGHT | g.BOTTOM);
}
//canvas.flushGraphics();
} // end drawInactiveMenu
public void drawActiveMenu(Canvas canvas, Graphics g, int selectedOptionIndex) {
// create active menu font
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
int fontHeight = font.getHeight();
// clear menu bar background
int width = canvas.getWidth();
int height = canvas.getHeight();
g.setColor(0xcccccc);
g.fillRect(0, height - fontHeight - 2 * padding, width, height);
// draw default menu bar options
g.setFont(font);
g.setColor(0x000000); // black
// draw "Cancel" option
g.drawString(cancelOption, width - padding, height - padding, g.RIGHT | g.BOTTOM);
// draw menu options
if (menuOptions != null) {
// check out the max width of a menu (for the specified menu font)
int menuMaxWidth = 0;
int menuMaxHeight = 0;
int currentWidth = 0;
// we'll simply check each option and find the maximal width
for (int i = 0; i < menuOptions.length; i++) {
currentWidth = font.stringWidth(menuOptions[i]);
if (currentWidth > menuMaxWidth) {
menuMaxWidth = currentWidth; // update
}
menuMaxHeight += fontHeight + padding; // for a current menu option
} // end for each menu option
menuMaxWidth += 2 * padding; // padding from left and right
// now we know the bounds of active menu
// draw active menu's background
g.setColor(0xcccccc);
g.fillRect(0, // x
height - fontHeight - 2 * padding - menuMaxHeight, // y
menuMaxWidth,
menuMaxHeight);
// draw menu options (from up to bottom)
g.setFont(font);
int menuOptionX = padding;
int menuOptionY = height - fontHeight - 2 * padding - menuMaxHeight + padding;
for (int i = 0; i < menuOptions.length; i++) {
if (i != selectedOptionIndex) { // draw unselected menu option
g.setColor(0x000000); // black
} // end if draw unselected menu option
else { // draw selected menu option
/**
* The simplest way to separate selected menu option
* is by drawing it with different color.
* However, it also may be painted as underlined text
* or with different background color.
*/
g.setColor(0x000000);
g.fillRect(0, // x
menuOptionY - padding, // y
menuMaxWidth,
fontHeight + padding);
g.setColor(0xffffff); // blue
} // end if draw selected menu option
g.drawString(menuOptions[i], menuOptionX, menuOptionY, g.LEFT | g.TOP);
menuOptionY += padding + fontHeight;
} // end for each menu option
} // end if menu options were specified
} // end drawActiveMenu
} // end Menu.java
 Read more: |
|
Friday, 18 June 2010 09:22 |
// description of your code here
the keycode class to get keystroke values
// insert code here..
package mobilePackage;
import javax.microedition.lcdui.Canvas;
/**
* Class redefines codes of mobile phone to our constant values.
* Class can give to developers following information:
*
* defined platform name
* In case if device vendor not defined we'll recieve PLATFORM_NOT_DEFINED like platform name.
* Same in this case keyCodes will be setted like for Nokia and SE. It's done for work on emulators,
* because on some of them it's impossible to define platform name.
* adopted to our constants key code value
* for test returns defined real code of left softkey
*
*/
public final class KeyCodeAdaptor {
/**
* instance on this class
*/
private static final KeyCodeAdaptor instance = new KeyCodeAdaptor();
/**
* canvas used for definig codes
*/
private final Canvas adaptorCanvas;
/**
* constants for platforms names
*/
public static final String PLATFORM_MOTOROLA = "motorola";
public static final String PLATFORM_NOKIA = "nokia";
public static final String PLATFORM_SONY_ERICSSON = "SE";
public static final String PLATFORM_SIEMENS = "siemens";
public static final String PLATFORM_SAMSUNG = "samsung";
public static final String PLATFORM_LG = "LG";
public static final String PLATFORM_NOT_DEFINED = "NA";
/**
* constants for keycodes
*/
public static final int SOFT_KEY_LEFT = -201;
public static final int SOFT_KEY_RIGHT = -202;
public static final int SOFT_KEY_MIDDLE_INTERNET = -203;
/**
* this key is present on Nokia s60
*/
public static final int PENCIL_KEY = -207;
public static final int DELETE_KEY = -204;
public static final int BACK_KEY = -205;
// public static final int SEND_KEY = -206; //constant will be used in future for green key start dialling
public static final int KEY_1 = 201;
public static final int KEY_2 = 202;
public static final int KEY_3 = 203;
public static final int KEY_4 = 204;
public static final int KEY_5 = 205;
public static final int KEY_6 = 206;
public static final int KEY_7 = 207;
public static final int KEY_8 = 208;
public static final int KEY_9 = 209;
public static final int KEY_0 = 200;
public static final int KEY__POUND = 211;
public static final int KEY__STAR = 212;
/**
* KEYS on JOISTICK
*/
public static final int UP_KEY = 221;
public static final int DOWN_KEY = 222;
public static final int LEFT_KEY = 223;
public static final int RIGHT_KEY = 224;
public static final int CENTER_KEY = 225;
public static final int NOT_DEFINED_KEY = 254;
// QWERTY NUMERIC
public static final int QKEY_1 = 49;
public static final int QKEY_2 = 50;
public static final int QKEY_3 = 51;
public static final int QKEY_4 = 52;
public static final int QKEY_5 = 53;
public static final int QKEY_6 = 54;
public static final int QKEY_7 = 55;
public static final int QKEY_8 = 56;
public static final int QKEY_9 = 57;
public static final int QKEY_0 = 48;
/**
* current platform name
*/
private final String PLATFORM_NAME;
/**
* current platform codeofSoftkey
*/
private final int SOFTKEY_LEFT;
private final int SOFTKEY_RIGHT;
private final int SOFTKEY_MIDDLE_INTERNET;
private final int SOFTKEY_DELETE;
private final int SOFTKEY_BACK;
/**
* standart values for softkeys of different platforms
* used only in predefining
*/
private static final int SOFT_KEY_LEFT_SE = -6;
private static final int SOFT_KEY_RIGHT_SE = -7;
private static final int DELETE_KEY_SE = -8;
private static final int INTERNET_KEY_SE = -10;
private static final int BACK_KEY_SE = -11;
private static final int SOFT_KEY_LEFT_SAMSUNG = -6;
private static final int SOFT_KEY_RIGHT_SAMSUNG = -7;
private static final int DELETE_KEY_SAMSUNG = -8;
private static final int SOFT_KEY_LEFT_SIEMENS = -1;
private static final int SOFT_KEY_RIGHT_SIEMENS = -4;
private static final int SOFT_KEY_LEFT_NOKIA = -6;
private static final int SOFT_KEY_RIGHT_NOKIA = -7;
private static final int DELETE_KEY_NOKIA = -8;
private static final int PENCIL_KEY_NOKIA = -50;
private static final int SOFT_KEY_LEFT_MOTOROLA = -21;
private static final int SOFT_KEY_RIGHT_MOTOROLA = -22;
private static final int SOFT_KEY_LEFT_MOTOROLA2 = -20;
private static final int SOFT_KEY_LEFT_MOTOROLA1 = 21;
private static final int SOFT_KEY_RIGHT_MOTOROLA1 = 22;
private static final int SOFT_KEY_MIDLE_MOTOROLA = -23;
private static final int SOFT_KEY_MIDLE_NOKIA = -5;
private static final String SOFT_WORD = "SOFT";
/**
* constructor.
* here is predefining of special keys and platform made
*/
private KeyCodeAdaptor() {
adaptorCanvas = MobileOrder.canvas;
PLATFORM_NAME = getPlatform();
SOFTKEY_LEFT = getLeftSoftkeyCode();
SOFTKEY_RIGHT = getRightSoftkeyCode();
SOFTKEY_MIDDLE_INTERNET = getMidleORInternetSoftkeyCode();
SOFTKEY_DELETE = getDeleteKeyCode();
SOFTKEY_BACK = getBackKeyCode();
}
/**
* return platform keycode of left softkey
* if it's defined
* default value -6
*
* @return SOFTKEY_LEFT
*/
public int getPlatformSoftkeyLeftCode() {
return SOFTKEY_LEFT;
}
/**
* Returns mobile phone platform
*
* @return name mobile phone platform
*/
private String getPlatform() {
// detecting NOKIA or SonyEricsson
try {
final String currentPlatform = System.getProperty("microedition.platform");
if (currentPlatform.indexOf("Nokia") != -1) {
return PLATFORM_NOKIA;
} else if (currentPlatform.indexOf("SonyEricsson") != -1) {
return PLATFORM_SONY_ERICSSON;
}
} catch (Throwable ex) {
}
// detecting SAMSUNG
try {
Class.forName("com.samsung.util.Vibration");
return PLATFORM_SAMSUNG;
} catch (Throwable ex) {
}
// detecting MOTOROLA
try {
Class.forName("com.motorola.multimedia.Vibrator");
return PLATFORM_MOTOROLA;
} catch (Throwable ex) {
try {
Class.forName("com.motorola.graphics.j3d.Effect3D");
return PLATFORM_MOTOROLA;
} catch (Throwable ex2) {
try {
Class.forName("com.motorola.multimedia.Lighting");
return PLATFORM_MOTOROLA;
} catch (Throwable ex3) {
try {
Class.forName("com.motorola.multimedia.FunLight");
return PLATFORM_MOTOROLA;
} catch (Throwable ex4) {
}
}
}
}
try {
if (adaptorCanvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA).toUpperCase().indexOf(SOFT_WORD) > -1) {
return PLATFORM_MOTOROLA;
}
} catch (Throwable e) {
try {
if (adaptorCanvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase().indexOf(SOFT_WORD) > -1) {
return PLATFORM_MOTOROLA;
}
} catch (Throwable e1) {
try {
if (adaptorCanvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA2).toUpperCase().indexOf(SOFT_WORD) > -1) {
return PLATFORM_MOTOROLA;
}
} catch (Throwable e2) {
}
}
}
// detecting SIEMENS
try {
Class.forName("com.siemens.mp.io.File");
return PLATFORM_SIEMENS;
} catch (Throwable ex) {
}
// detecting LG
try {
Class.forName("mmpp.media.MediaPlayer");
return PLATFORM_LG;
} catch (Throwable ex) {
try {
Class.forName("mmpp.phone.Phone");
return PLATFORM_LG;
} catch (Throwable ex1) {
try {
Class.forName("mmpp.lang.MathFP");
return PLATFORM_LG;
} catch (Throwable ex2) {
try {
Class.forName("mmpp.media.BackLight");
return PLATFORM_LG;
} catch (Throwable ex3) {
}
}
}
}
return PLATFORM_NOT_DEFINED;
}
/**
* define real left soft key code by platform
*
* @return code
*/
private int getLeftSoftkeyCode() {
int keyCode = 0;
try {
if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
String softkeyLeftMoto = "";
try {
softkeyLeftMoto = adaptorCanvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA).toUpperCase();
} catch (IllegalArgumentException ilae) {
// ilae.printStackTrace();
}
String softkeyLeftMoto1 = "";
try {
softkeyLeftMoto1 = adaptorCanvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase();
} catch (IllegalArgumentException ilae) {
// ilae.printStackTrace();
}
String softkeyLeftMoto2 = "";
try {
softkeyLeftMoto2 = adaptorCanvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA2).toUpperCase();
} catch (IllegalArgumentException ilae) {
// ilae.printStackTrace();
}
if (softkeyLeftMoto.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto.indexOf("1") >= 0) {
return SOFT_KEY_LEFT_MOTOROLA;
} else if (softkeyLeftMoto1.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto1.indexOf("1") >= 0) {
return SOFT_KEY_LEFT_MOTOROLA1;
} else if (softkeyLeftMoto2.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto2.indexOf("1") >= 0) {
return SOFT_KEY_LEFT_MOTOROLA2;
} else if (softkeyLeftMoto.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto.indexOf("LEFT") >= 0) {
return SOFT_KEY_LEFT_MOTOROLA;
} else if (softkeyLeftMoto1.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto1.indexOf("LEFT") >= 0) {
return SOFT_KEY_LEFT_MOTOROLA1;
} else if (softkeyLeftMoto2.indexOf(SOFT_WORD) >= 0 && softkeyLeftMoto2.indexOf("LEFT") >= 0) {
return SOFT_KEY_LEFT_MOTOROLA2;
}
} else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
return SOFT_KEY_LEFT_NOKIA;
} else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
// String leftkeySamsungName = adaptorCanvas.getKeyName(SOFT_KEY_LEFT_SAMSUNG).toUpperCase();
// if (leftkeySamsungName.indexOf(SOFT_WORD) >= 0) {
// if (leftkeySamsungName.indexOf("1") >= 0) {
return SOFT_KEY_LEFT_SAMSUNG;
// } else if (leftkeySamsungName.indexOf("LEFT") >= 0) {
// return SOFT_KEY_LEFT_SAMSUNG;
// }
// }
} else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
String leftKeySiemensName = adaptorCanvas.getKeyName(SOFT_KEY_LEFT_SIEMENS).toUpperCase();
if (leftKeySiemensName.indexOf(SOFT_WORD) >= 0) {
if (leftKeySiemensName.indexOf("1") >= 0) {
return SOFT_KEY_LEFT_SIEMENS;
} else if (leftKeySiemensName.indexOf("LEFT") >= 0) {
return SOFT_KEY_LEFT_SIEMENS;
}
}
} else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
return SOFT_KEY_LEFT_SE;
} else if (PLATFORM_NAME.equals(PLATFORM_NOT_DEFINED)) {
//
for (int i = -125; i <= 125; i++) {
if (i == 0) {
i++;
}
// System.out.println(getKeyName(i).toUpperCase());
final String s = adaptorCanvas.getKeyName(i).toUpperCase();
if (s.indexOf(SOFT_WORD) >= 0) {
if (s.indexOf("1") >= 0) {
keyCode = i;
break;
}
if (s.indexOf("LEFT") >= 0) {
keyCode = i;
break;
}
}
}
}
if (keyCode == 0) {
//#if emulator
//# return SOFT_KEY_LEFT_NOKIA;
//#endif
}
} catch (Throwable iaEx) {
//#if emulator
//# return SOFT_KEY_LEFT_NOKIA;
//#endif
}
return keyCode;
}
/**
* define real right soft key code for current platform
*
* @return code
*/
private int getRightSoftkeyCode() {
int keyCode = 0;
try {
if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
String rightSoftMoto1 = "";
try {
rightSoftMoto1 = adaptorCanvas.getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase();
} catch (IllegalArgumentException ilae) {
// ilae.printStackTrace();
}
String rightSoftMoto = "";
try {
rightSoftMoto = adaptorCanvas.getKeyName(SOFT_KEY_RIGHT_MOTOROLA).toUpperCase();
} catch (IllegalArgumentException ilae) {
// ilae.printStackTrace();
}
String rightSoftMoto2 = "";
try {
rightSoftMoto2 = adaptorCanvas.getKeyName(SOFT_KEY_RIGHT_MOTOROLA1).toUpperCase();
} catch (IllegalArgumentException ilae) {
// ilae.printStackTrace();
}
if (rightSoftMoto.indexOf(SOFT_WORD) >= 0 && rightSoftMoto.indexOf("2") >= 0) {
return SOFT_KEY_RIGHT_MOTOROLA;
} else if (rightSoftMoto1.indexOf(SOFT_WORD) >= 0 && rightSoftMoto1.indexOf("2") >= 0) {
return SOFT_KEY_RIGHT_MOTOROLA;
} else if (rightSoftMoto2.indexOf(SOFT_WORD) >= 0 && rightSoftMoto2.indexOf("2") >= 0) {
return SOFT_KEY_RIGHT_MOTOROLA1;
} else if (rightSoftMoto.indexOf(SOFT_WORD) >= 0 && rightSoftMoto.indexOf("RIGHT") >= 0) {
return SOFT_KEY_LEFT_MOTOROLA;
} else if (rightSoftMoto1.indexOf(SOFT_WORD) >= 0 && rightSoftMoto1.indexOf("RIGHT") >= 0) {
return SOFT_KEY_RIGHT_MOTOROLA1;
} else if (rightSoftMoto2.indexOf(SOFT_WORD) >= 0 && rightSoftMoto2.indexOf("RIGHT") >= 0) {
return SOFT_KEY_RIGHT_MOTOROLA;
}
} else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
return SOFT_KEY_RIGHT_NOKIA;
} else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
// String rightSoftSamsung = adaptorCanvas.getKeyName(SOFT_KEY_RIGHT_SAMSUNG).toUpperCase();
// if (rightSoftSamsung.indexOf(SOFT_WORD) >= 0) {
// if (rightSoftSamsung.indexOf("2") >= 0) {
return SOFT_KEY_RIGHT_SAMSUNG;
// } else if (rightSoftSamsung.indexOf("RIGHT") >= 0) {
// return SOFT_KEY_RIGHT_SAMSUNG;
// }
// }
} else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
String rightSoftSiemens = adaptorCanvas.getKeyName(SOFT_KEY_RIGHT_SIEMENS).toUpperCase();
if (rightSoftSiemens.indexOf(SOFT_WORD) >= 0) {
if (rightSoftSiemens.indexOf("4") >= 0) {
return SOFT_KEY_RIGHT_SIEMENS;
} else if (rightSoftSiemens.indexOf("RIGHT") >= 0) {
return SOFT_KEY_RIGHT_SIEMENS;
}
}
} else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
return SOFT_KEY_RIGHT_SE;
} else if (PLATFORM_NAME.equals(PLATFORM_NOT_DEFINED)) {
for (int i = -125; i <= 125; i++) {
if (i == 0) {
i++;
}
String keyName = adaptorCanvas.getKeyName(i).toUpperCase();
if (keyName.indexOf(SOFT_WORD) >= 0) {
if (keyName.indexOf("2") >= 0) {
keyCode = i;
break;
} else if (keyName.indexOf("4") >= 0) {
keyCode = i;
break;
} else if (keyName.indexOf("RIGHT") >= 0) {
keyCode = i;
break;
}
}
}
}
} catch (Throwable iaEx) {
//#if emulator
//# return SOFT_KEY_RIGHT_NOKIA;
//#endif
}
return keyCode;
}
/**
* define real middle soft key code for current platform
*
* @return code
*/
private int getMidleORInternetSoftkeyCode() {
try {
if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
if (adaptorCanvas.getKeyName(SOFT_KEY_MIDLE_MOTOROLA).toUpperCase().indexOf("SOFT") >= 0) {
return SOFT_KEY_MIDLE_MOTOROLA;
}
} else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
if (adaptorCanvas.getKeyName(SOFT_KEY_MIDLE_NOKIA).toUpperCase().indexOf("SOFT") >= 0) {
return SOFT_KEY_MIDLE_NOKIA;
}
} else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
} else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
} else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
return INTERNET_KEY_SE;
}
} catch (Throwable e) {
}
return 0;
}
/**
* define real key's C or DELETE code for current platform
*
* @return code
*/
private int getDeleteKeyCode() {
try {
if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
} else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
if (adaptorCanvas.getKeyName(DELETE_KEY_SE).toUpperCase().indexOf("CLEAR") >= 0) {
return DELETE_KEY_NOKIA;
} else {
return DELETE_KEY_NOKIA;
}
} else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
if (adaptorCanvas.getKeyName(DELETE_KEY_SAMSUNG).toUpperCase().indexOf("CLEAR") >= 0) {
return DELETE_KEY_SAMSUNG;
}
} else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
} else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
if (adaptorCanvas.getKeyName(DELETE_KEY_SE).toUpperCase().indexOf("CLEAR") >= 0) {
return DELETE_KEY_SE;
} else if (adaptorCanvas.getKeyName(DELETE_KEY_SE).toUpperCase().indexOf("C") >= 0) {
return DELETE_KEY_SE;
} else {
return DELETE_KEY_SE;
}
}
} catch (Throwable e) {
return DELETE_KEY_SE;
}
return 0;
}
/**
* define real key's BACK code for current platform
*
* @return code
*/
private int getBackKeyCode() {
try {
if (PLATFORM_NAME.equals(PLATFORM_MOTOROLA)) {
} else if (PLATFORM_NAME.equals(PLATFORM_NOKIA)) {
} else if (PLATFORM_NAME.equals(PLATFORM_SAMSUNG)) {
} else if (PLATFORM_NAME.equals(PLATFORM_SIEMENS)) {
} else if (PLATFORM_NAME.equals(PLATFORM_SONY_ERICSSON)) {
return BACK_KEY_SE;
}
} catch (Throwable e) {
}
return 0;
}
/**
* name of curent platform
*
* @return PLATFORM_NAME
*/
public String getPlatformName() {
return PLATFORM_NAME;
}
/**
* Used to adopt key kode to predefined constances, which are platform independent.
*
* You can use this method in any kind of canvas, but better at first time to call
* getInstance() method at the beginning of midlet work, because initialisation takes time.
*
* Best variant for usage is calling adoptKeyCode() to use keyPressed() method in Canvas:
*
* protected void keyPressed(int keyCode) {
* keyCode = KeyCodeAdaptor.getInstance().adoptKeyCode(keyCode);
* }
*
* and then you can use it:
*
* switch (keyCode) {
* case KeyCodeAdaptor.UP_KEY:
* break;
* case KeyCodeAdaptor.SOFT_KEY_LEFT:
* break;
* }
* or send this code to any other clesses.
*
* @param keycode This code is sent by platform to canvas and redirected here
* @return this keycode is equal to one of our constants declared in this class
*/
public int adoptKeyCode(int keycode, boolean isNumeric) {
switch (keycode) {
case Canvas.KEY_NUM0:
return KEY_0;
case Canvas.KEY_NUM1:
return KEY_1;
case Canvas.KEY_NUM2:
return KEY_2;
case Canvas.KEY_NUM3:
return KEY_3;
case Canvas.KEY_NUM4:
return KEY_4;
case Canvas.KEY_NUM5:
return KEY_5;
case Canvas.KEY_NUM6:
return KEY_6;
case Canvas.KEY_NUM7:
return KEY_7;
case Canvas.KEY_NUM8:
return KEY_8;
case Canvas.KEY_NUM9:
return KEY_9;
case Canvas.KEY_STAR:
return KEY__STAR;
case Canvas.KEY_POUND:
return KEY__POUND;
default:
if (keycode == SOFTKEY_LEFT) {
return SOFT_KEY_LEFT;
} else if (keycode == SOFTKEY_RIGHT) {
return SOFT_KEY_RIGHT;
} else if (keycode == SOFTKEY_DELETE) {
return DELETE_KEY;
} else if (keycode == SOFTKEY_BACK) {
return BACK_KEY;
} else if (keycode == SOFTKEY_MIDDLE_INTERNET) {
return SOFT_KEY_MIDDLE_INTERNET;
} else if (keycode == PENCIL_KEY_NOKIA) {
return PENCIL_KEY;
} else {
try {
final int gameAction;
gameAction = adaptorCanvas.getGameAction(keycode);
if (gameAction == Canvas.UP) {
return UP_KEY;
} else if (gameAction == Canvas.DOWN) {
return DOWN_KEY;
} else if (gameAction == Canvas.LEFT) {
return LEFT_KEY;
} else if (gameAction == Canvas.RIGHT) {
return RIGHT_KEY;
} else if (gameAction == Canvas.FIRE) {
return CENTER_KEY;
}
} catch (IllegalArgumentException e) {
// e.printStackTrace();
}
}
break;
}
if (isNumeric)
{
if (keycode == QKEY_0){
return KEY_0;
} else if (keycode == QKEY_1){
return KEY_1;
} else if (keycode == QKEY_2){
return KEY_2;
} else if (keycode == QKEY_3){
return KEY_3;
} else if (keycode == QKEY_4){
return KEY_4;
} else if (keycode == QKEY_5){
return KEY_5;
} else if (keycode == QKEY_6){
return KEY_6;
} else if (keycode == QKEY_7){
return KEY_7;
} else if (keycode == QKEY_8){
return KEY_8;
} else if (keycode == QKEY_9){
return KEY_9;
}
}
//#if debug
//# return keycode;
//#else
return NOT_DEFINED_KEY;
//#endif
}
/**
* return instance of class
*
* @return instance
*/
public static KeyCodeAdaptor getInstance() {
return instance;
}
}
 Read more: |
|
|
Wednesday, 14 April 2010 04:06 |
// the code takes no. of nodes and adjency matrix as input and prints path and distance
// for specified source and destination
#include
#include
#define infinity 1000
class queue
{
public:
int top;
int rear;
int a[10];
queue()
{
top=rear=-1;
}
void push(int n)
{
a[++top]=n;
}
int isempty()
{
if(top==rear)
return 1;
return 0;
}
int del()
{
int temp=a[++rear];
if(rear==top)
{
rear=top=-1;
}
return temp;
}
};
void main()
{
clrscr();
int n,mat[10][10],s,d,path[10],dist[10],v,w;
queue q;
printf("Enter no. of nodes::");
scanf("%d",&n);
printf("Enter the adjancey matrix(enter 0 for diagonal elements)::\n");
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&mat[i][j]);
printf("Enter the source node::");
scanf("%d",&s);
printf("Enter the destination node::");
scanf("%d",&d);
q.push(s);
dist[s]=0;
for(i=1;i<=n;i++)
{
if(i!=s)
{
q.push(i);
dist[i]=infinity;
}
path[i]=0;
}
while(!q.isempty())
{
v=q.del();
for(i=1;i<=n;i++)
{ if(mat[v][i]!=0)
{
w=i;
if(dist[v]+mat[v][w] {
dist[w]=dist[v]+mat[v][w];
path[w]=v;
}
}
}
}
getch();
printf("%d",d);
do
{
printf("<-%d",path[d]);
d=path[d];
}
while(d!=s);
getch();
}
 Read more: |
|
Thursday, 25 March 2010 22:49 |
template
class Node
{
public:
Node(const T &theData, Node* theLink)
: data(theData), link(theLink)
{
// left blank intentionally
}
Node* getLink( ) { return link; }
T& getData( ) { return data; }
void setData(const T &theData) { data = theData; }
void setLink(Node* pointer) { link = pointer; }
private:
T data; // The data object
Node* link; // pointer to next node (i.e. link)
};
template
void headInsert(Node* &head, const T &theData)
{
head = new Node(theData, head);
}
template
void insert(Node* &afterMe, const T &theData)
{
if (afterMe != NULL)
{
afterMe->setLink(new Node(theData, afterMe->getLink( )));
}
else
{
headInsert(afterMe,theData);
}
}
class Leg
{
private:
string startCity;
string destCity;
string transMode; // transportation mode between cities (car, plane,\
etc)
int milesBetween; // mileage between start and destination cities
Date startDateTime;
Date arrivalDateTime;
public:
Leg();
Leg(string sCity, string dCity, string transMode, int miles,
Date startDate, Date arrivalDate);
void display();
bool operator == (Leg rightSide);
bool operator != (Leg rightSide);
};
int main()
{
string startCity;
string destCity;
string transMode;
int miles;
int month;
int day;
int year;
int hour;
int min;
Node* head = NULL;
Node* endLeg = head;
char fileName[80] = "trip.dat";
ifstream fin;
int errorCnt = 0;
fin.open(fileName);
while (fin.fail())
{
errorCnt++;
cout << "File not found! - '" << fileName << "'" << endl;
if (errorCnt <= 3)
{
cout << "\nPlease enter the complete path or correct file name: "\
;
cin >> fileName;
}
else
{
cout << "\nI sorry the file '" << fileName
<< "' is not found were specified. Exiting the program!!!\n\
";
exit(1);
}
fin.open(fileName);
}
fin >> startCity;
// hint: read startCity
while (!fin.eof())
{
getline(&fin, startCity);
getline(&fin, destCity);
getline(&fin, transMode);
fin >> miles;
// hint: read destination city, transportation mode, miles
fin >> month >> day >> year >> hour >> min;
// hint: read month, day, year, hr, min
Date startDate(day, month, year, hour, min);
// hint: create start date object
fin >> month >> day >> year >> hour >> min;
// hint: read month, day, year, hr, min
Date arriveDate(day, month, year, hour, min);
// hint: create arrival date object
// hint: throw away newline characters so next getline() will work.
fin.ignore(80,'\n');
// hint: create a Leg object using startCity, destCity, transMode,
// miles, startDate, arrivalDate
Leg leg(startCity, destCity, transMode, miles,
startDate, arriveDate);
// hint: insert new leg of the trip at the end of the linked list
insert(endLeg, leg);
if(head = NULL)
head = endLeg;
else
endLeg = endLeg -> getLink();
// hint: read next startCity from file
fin >> startCity;
}
cout << endl;
cout << "Start City Start Date Destination City A\
rrival Date Mode Miles \n";
cout << "-------------------- ---------------- -------------------- -\
--------------- ----- -----\n";
// Display the itinerary HERE by traversing through the linked list!!
Node* currentLeg = head;
while(currentLeg -> getLink() != NULL)
{
currentLeg.getData() -> display();
currentLeg = currentLeg -> getLink();
}
return 0;
}
 Read more: |
|