13,100
社区成员
发帖
与我相关
我的任务
分享
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class MenuCanvas extends Canvas implements Runnable {
private String[] menuItemList = new String[] { "ActionOne", "ActionTwo",
"ActionThree" };
private final static int ITEM_HEIGHT = 50;
private final static int ITEM_INTEVAL = 20;
private String promptMessage = "";
private int currentIndex = 0;
public MenuCanvas() {
}
protected void keyPressed(int keyCode) {
switch (getGameAction(keyCode)) {
case UP:
currentIndex -= 1;
break;
case DOWN:
currentIndex += 1;
break;
case FIRE:
fireAction();
break;
}
if (currentIndex < 0) {
currentIndex = menuItemList.length - 1;
}
if (currentIndex >= menuItemList.length) {
currentIndex = 0;
}
}
private void fireAction() {
System.out.println("you fire index is: " + currentIndex);
}
protected void paint(Graphics g) {
for (int i = 0; i < menuItemList.length; ++i) {
if (i == currentIndex) {
g.setColor(0xff00ff);
} else {
g.setColor(0x0000ff);
}
g.fillRect(50, i * (ITEM_HEIGHT + ITEM_INTEVAL), getWidth() / 2,
ITEM_HEIGHT);
g.setColor(0x000000);
g.drawString(menuItemList[i], 70, i * (ITEM_HEIGHT + ITEM_INTEVAL)
+ 10, Graphics.TOP | Graphics.LEFT);
}
}
public void run() {
while (true) {
this.repaint();
serviceRepaints();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void start() {
new Thread(this).start();
}
}
Display display = Display.getDisplay(this);
MenuCanvas canvas = new MenuCanvas();
canvas.start();
display.setCurrent(canvas);