?? menucanvas.java~4~
字號(hào):
package gamemidlet;
import javax.microedition.lcdui.*;
/**
* <p>Title: 黃小輝8000106286網(wǎng)通062班</p>
* <p>Description: GameMIDlet</p>
* <p>Copyright: Copyright (c) 2009</p>
* <p>Company: NCU</p>
* @author not attributable
* @version 1.0
*/
public class MenuCanvas
extends Canvas
implements CommandListener {
String[] menuItem = {
"開始游戲", "最高分", "選項(xiàng)", "游戲規(guī)則說明", "關(guān)于", "退出"};
Font menuFont;
int currentY = 0;
int currentIndex;
Image bckImg;
int[] rgbData;
int MenuHeight;
int topBottom;
int BarSpace = 3;
Command yesCmd, noCmd;
public MenuCanvas() {
this.setFullScreenMode(true);
menuFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
MenuHeight = menuFont.getHeight() + 4;
topBottom = (this.getHeight() - menuItem.length * MenuHeight -
BarSpace * (menuItem.length - 1)) / 2;
currentIndex = 1;
bckImg = GameMIDlet.loadImage("/pic/tank.png");
rgbData = new int[bckImg.getWidth() * bckImg.getHeight()];
bckImg.getRGB(rgbData, 0, bckImg.getWidth(), 0, 0, bckImg.getWidth(),
bckImg.getHeight());
int alpha = 100;
for (int i = 0; i < rgbData.length; i++) {
rgbData[i] = (alpha << 24) | (rgbData[i] & 0X00FFFFFF);
}
}
protected void paint(Graphics g) {
g.setColor(0x00000000);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
int imgW = bckImg.getWidth();
int imgH = bckImg.getHeight();
int cols = (this.getWidth() / imgW) + 1;
int rows = (this.getHeight() / imgH) + 1;
for (int w = 0; w <= cols; w++) {
for (int h = 0; h <= rows; h++) {
int gx = w * imgW;
int gy = h * imgH;
g.translate(gx, gy);
g.drawRGB(rgbData, 0, bckImg.getWidth(), 0, 0, bckImg.getWidth(),
bckImg.getHeight(), true);
g.translate(0 - g.getTranslateX(), 0 - g.getTranslateY());
}
}
int lineY = 0;
for (int n = 1; n <= menuItem.length; n++) {
lineY = n * MenuHeight + topBottom;
g.setColor(0X0081ae08);
g.fillRect(0, lineY, this.getWidth(), 2);
g.fillRoundRect(MenuHeight, lineY - 8, this.getWidth() - 40,
MenuHeight - 2, 6, 6);
g.setColor(0X00FFFFFF);
g.setFont(menuFont);
g.drawString(menuItem[n - 1], this.getWidth() / 2,
lineY - menuFont.getHeight() / 2 + 2, g.TOP | g.HCENTER);
}
setFocusItem(currentIndex, g);
}
private void getFocusIndex() {
currentIndex = (currentY - topBottom) / MenuHeight;
}
private void setFocusItem(int itemIndex, Graphics g) {
int lineY = 0;
lineY = itemIndex * MenuHeight + topBottom;
g.setColor(0x00d5fe67);
g.fillRect(0, lineY, this.getWidth(), 2);
g.fillRoundRect(MenuHeight, lineY - 8, this.getWidth() - 40, MenuHeight - 2,
6, 6);
g.setColor(0x00FF0000);
g.setFont(menuFont);
g.drawString(menuItem[itemIndex - 1], this.getWidth() / 2,
lineY - menuFont.getHeight() / 2 + 2, g.TOP | g.HCENTER);
currentY = lineY;
getFocusIndex();
}
protected void keyPressed(int keyCode) {
int keyAction = this.getGameAction(keyCode);
switch (keyAction) {
case UP:
if (currentIndex > 1) {
currentIndex = currentIndex - 1;
}
repaint();
break;
case DOWN:
if (currentIndex >= 1 && currentIndex < menuItem.length) {
currentIndex = currentIndex + 1;
}
repaint();
break;
case FIRE:
switch (currentIndex) {
case 1:
showMessage("提示", "為此菜單項(xiàng)編寫[" + menuItem[currentIndex - 1] + "]處理代碼",
0);
break;
case 2:
showMessage("提示", "為此菜單項(xiàng)編寫[" + menuItem[currentIndex - 1] + "]處理代碼",
0);
break;
case 3:
showMessage("提示", "為此菜單項(xiàng)編寫[" + menuItem[currentIndex - 1] + "]處理代碼",
0);
break;
case 4:
showMessage("提示", "為此菜單項(xiàng)編寫[" + menuItem[currentIndex - 1] + "]處理代碼",
0);
break;
case 5:
AboutCanvas about = new AboutCanvas();
Display.getDisplay(GameMIDlet.instance).setCurrent(about);
break;
case 6:
showMessage("提示", "確定要退出游戲嗎", 0);
break;
}
break;
}
}
protected void keyRepeated(int keyCode) {
keyPressed(keyCode);
}
private void showMessage(String TitleStr, String message, int type) {
yesCmd = new Command("是", Command.OK, 1);
noCmd = new Command("否", Command.SCREEN, 1);
Image logo = GameMIDlet.loadImage("/pic/tank20.png");
Alert aboutAlert = new Alert(TitleStr, message, logo,
AlertType.CONFIRMATION);
if (type == 0) {
aboutAlert.setTimeout(3000);
}
else {
aboutAlert.setTimeout(Alert.FOREVER);
aboutAlert.addCommand(yesCmd);
aboutAlert.addCommand(noCmd);
aboutAlert.setCommandListener(this);
}
Display.getDisplay(GameMIDlet.instance).setCurrent(aboutAlert);
}
public void commandAction(Command c, Displayable displayable) {
if (c == yesCmd) {
GameMIDlet.quitApp();
}
if (c == noCmd) {
Display.getDisplay(GameMIDlet.instance).setCurrent(this);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -