package peng.canvas;
import javax.microedition.lcdui.*;
public class MenuCanvas extends Canvas implements CommandListener {
private String[] menuName = new String[] { "login", "picture", "wap",
"phone", "call", "goto", "fuck", "music", "video" };
private int canvaWidth = -1, canvaHeight = -1;
private int maxHeight = -1, imgWidth = -1, imgHeight = -1;
private int focus = 0;
private int fontHeight = -1;
private Image menuImg[] = new Image[9];
private Command exit;
public MenuCanvas() {
exit = new Command("Exit", Command.EXIT, 1);
addCommand(exit);
setCommandListener(this);
canvaWidth = getWidth();
canvaHeight = getHeight();
try {
for (int i = 0; i < menuImg.length; i++) {
menuImg[i] = Image.createImage(28, 28);
Graphics g = menuImg[i].getGraphics();
g.translate(-28 * i, 0);
}
} catch (Exception e) {
System.out.println(e.toString());
}
imgWidth = menuImg[0].getWidth();
imgHeight = menuImg[0].getHeight();
fontHeight = Font.getDefaultFont().getHeight();
maxHeight = imgHeight + fontHeight;
}
public void paint(Graphics g) {
int color = g.getColor();
g.setColor(0xFFFFFF);
g.fillRect(0, 0, canvaWidth, canvaHeight);
int cellWidth = canvaWidth / 3;
int cellHeight = canvaHeight / 3;
g.setColor(color);
for (int i = 0; i < menuImg.length; i++) {
g.drawImage(menuImg[i], cellWidth * (i % 3)
+ (cellWidth - imgWidth) / 2, cellHeight * (i / 3)
+ (cellHeight - maxHeight) / 2, Graphics.TOP
| Graphics.LEFT);
int fontWidth = Font.getDefaultFont().stringWidth(menuName[i]);
g.drawString(menuName[i], cellWidth * (i % 3)
+ (cellWidth - fontWidth) / 2, cellHeight * (i / 3)
+ (cellHeight - maxHeight) / 2 + imgHeight + 1,
Graphics.TOP | Graphics.LEFT);
if (i == focus) {
g.drawRect(cellWidth * (i % 3) + (cellWidth - imgWidth) / 2,
cellHeight * (i / 3) + (cellHeight - maxHeight) / 2,
imgWidth + 1, imgHeight + 1);
}
}
}
public void keyPressed(int key) {
int action = getGameAction(key);
switch (action) {
case FIRE:
Alert alert = new Alert("You select:", menuName[focus],
menuImg[focus], AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
break;
case UP:
focus = focus - 3;
if (focus < 0) {
focus = focus + 9;
}
break;
case DOWN:
focus = focus + 3;
if (focus >= 9) {
focus = focus - 9;
}
break;
case LEFT:
if (focus == 0) {
focus = 8;
} else {
focus = (--focus) % 9;
}
break;
case RIGHT:
focus = (++focus) % 9;
break;
}
repaint();
serviceRepaints();
}
public void commandAction(Command c, Displayable dpa) {
if (c == exit) {
}
}
}