📄 mainmenu.java
字号:
package unicoco;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.midlet.MIDletStateChangeException;
public class MainMenu extends Canvas {
Font lowFont;
Font highFont;
static MainMenu instance;
static final int SINGLEPALYER = 0;
static final int VSMODE = 1;
static final int OPTIONS = 2;
static final int EXIT = 3;
String[] mainMenu;
private Image banner;
byte menuIndex;
int menuY;
byte hfheight;
byte spacing;
Player musicPlayer;
MainMenu() {
setFullScreenMode(true);
try {
this.banner = Image.createImage("/mainMenu1.png");
} catch (IOException e) {
// e.printStackTrace();
MIDGame.game.notifyDestroyed();
}
lowFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
highFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
Font.SIZE_MEDIUM);
hfheight = (byte) highFont.getHeight();
spacing = (byte) (hfheight / 6);
mainMenu = new String[] { "Single Player", "VS Mode", "Options", "Exit" };
menuY = hfheight * mainMenu.length + (mainMenu.length - 1) * (spacing);
menuY = (getHeight() - banner.getHeight() - menuY) / 2
+ banner.getHeight();
}
protected void hideNotify() {
if (musicPlayer != null) {
try {
musicPlayer.stop();
} catch (MediaException e) {
}
}
}
protected void paint(Graphics g) {
// System.out.println(getWidth() + " " + getHeight());
g.setColor(0xFF000000);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(banner, 0, 0, Graphics.TOP | Graphics.LEFT);
for (int i = 0; i < mainMenu.length; i++) {
if (i == menuIndex) {
// 高亮块的颜色
g.setColor(192, 192, 192);
// 绘制高亮块
g.fillRect(0, menuY + i * hfheight + ((i - 1) * spacing),
getWidth(), hfheight);
g.setFont(highFont);
g.setColor(255, 0, 0);
g.drawString(mainMenu[i], (getWidth() - highFont
.stringWidth(mainMenu[i])) / 2, menuY + i * hfheight
+ ((i - 1) * spacing), Graphics.TOP | Graphics.LEFT);
} else {
g.setFont(lowFont);
g.setColor(213, 213, 0);
g.drawString(mainMenu[i], (getWidth() - lowFont
.stringWidth(mainMenu[i])) / 2, menuY + i * hfheight
+ ((i - 1) * spacing), Graphics.TOP | Graphics.LEFT);
}
}
}
protected void keyPressed(int code) {
if (getGameAction(code) == Canvas.UP) {
menuIndex = (byte) ((menuIndex + 3) % 4);
} else if (getGameAction(code) == Canvas.DOWN) {
menuIndex = (byte) ((menuIndex + 1) % 4);
} else if (getGameAction(code) == Canvas.FIRE) {
switch (menuIndex) {
case SINGLEPALYER:
MIDGame.display.setCurrent(GameMainCanvas.getInstance());
System.gc();
break;
case VSMODE:
MIDGame.display.setCurrent(SelectList.getInstance());
System.gc();
break;
case OPTIONS:
MIDGame.display.setCurrent(OptionsForm.getInstance());
break;
case EXIT:
MIDGame.game.notifyDestroyed();
try {
MIDGame.game.destroyApp(false);
} catch (MIDletStateChangeException e) {
// e.printStackTrace();
MIDGame.game.notifyDestroyed();
}
break;
default:
return;
}
}
// 只重画一定的范围,提高系统的效率
repaint(0, menuY - spacing, getWidth(), getHeight() - menuY + spacing);
}
public final static Displayable getInstance() {
if (MainMenu.instance == null) {
MainMenu.instance = new MainMenu();
}
instance.menuIndex = 0;
return MainMenu.instance;
}
protected void showNotify() {
if (MIDGame.game.music == 1) {
InputStream is = getClass().getResourceAsStream("/theme.mid");
try {
musicPlayer = javax.microedition.media.Manager.createPlayer(is,
"audio/midi");
musicPlayer.setLoopCount(-1);
musicPlayer.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
// e1.printStackTrace();
} catch (MediaException e1) {
// TODO Auto-generated catch block
// e1.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -