- Code: Select all
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class b extends Canvas implements Runnable {
static final Font lowFont = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
static final Font highFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
static final int lowColor = 0x000000FF;
static final int highColor = 0x00FF0000;
static final int highBGColor = 0x00CCCCCC;
static int width;
Command cmd;
static int height;
static int startHeight;
static final int spacing = highFont.getHeight() / 2;
Image img;
static final String[] mainMenu = {"New Game", "HighScore", "Settings", "Help", "About"};
static int menuIdx;
Thread menuThread;
public b() {
width = getWidth();
height = getHeight();
this.setFullScreenMode(true);
cmd=new Command("deepak",Command.OK,0);
startHeight = (highFont.getHeight() * mainMenu.length) + ((mainMenu.length - 1) * spacing);
startHeight = (height - startHeight) / 2;
try {
img = Image.createImage("/a.png");
} catch (Exception e) {
}
menuIdx = 0;
menuThread = new Thread(this);
menuThread.start();
}
public void run() {
while (true) {
repaint();
}
}
public void paint(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0, 0, width, height);
g.drawImage(img, 40, getHeight() - 100, 0);
for (int i = 0; i < mainMenu.length; i++) {
if (i == menuIdx) {
g.setColor(highBGColor);
g.fillRect(0, startHeight + (i * highFont.getHeight()) + spacing, width, highFont.getHeight());
g.setFont(highFont);
g.setColor(highColor);
g.drawString(mainMenu[i], (width - highFont.stringWidth(mainMenu[i])) / 2, startHeight + (i * highFont.getHeight()) + spacing, 20);
} else {
g.setFont(lowFont);
g.setColor(lowColor);
g.drawString(mainMenu[i], (width - lowFont.stringWidth(mainMenu[i])) / 2, startHeight + (i * highFont.getHeight()) + spacing, 20);
}
}
}
protected void keyPressed(int code) {
if (getGameAction(code) == Canvas.UP && menuIdx - 1 >= 0) {
menuIdx--;
} else if (getGameAction(code) == Canvas.DOWN && menuIdx + 1 < mainMenu.length) {
menuIdx++;
}
switch (code) {
case FIRE:
{
if (menuIdx == 0) {
display.setCurrent(nextScreen);
// This is where i get the Error the application throws a null pointer
// Exception and dies when i call the next Display on the screen which is a
// Game Canvas
} else if (menuIdx == 1) {
System.out.println("b");
} else if (menuIdx == 2) {
System.out.println("c");
} else if (menuIdx == 3) {
System.out.println("d");
} else if (menuIdx == 4) {
System.out.println("e");
}
}
}
}
}
