漂亮的游戲開始畫面
介紹一個游戲開始的畫面。
先建兩個類 MenuScreen.java SimpleCustomMenuWithBGFont.java用于測試
首先說下SimpleCustomMenuWithBGFont.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
//import java.util.*;
public class SimpleCustomMenuWithBGFont extends MIDlet implements CommandListener {
Display display;
Display pauseDisplay;
boolean isSplash = true;
MenuScreen menuScreen;
public SimpleCustomMenuWithBGFont() {
MenuScreen menuScreen = new MenuScreen();
display = Display.getDisplay(this);
display.setCurrent(menuScreen);
}
protected void startApp() throws MIDletStateChangeException {
}
protected void pauseApp() { }
protected void destroyApp (boolean flag) throws MIDletStateChangeException {}
public void commandAction (Command cmd, Displayable dis) {
}
}
這個類很簡單,就是作為了測試使用的。
主要來介紹一下MenuScreen.java
import javax.microedition.lcdui.*;
public class MenuScreen extends Canvas implements Runnable {
// 設置字體
static final Font lowFont = Font.getFont (Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
static final Font highFont = Font.getFont (Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
static final int NEW_GAME = 0;
static final int HIGH_SCORE = 1;
static final int SETTINGS = 2;
static final int HELP = 3;
static final int ABOUT = 4;
static final int MENU_ITEM_COUNT = 5;
// 設置顏色
static final int lowColor = 0x0000FF00; // Not Highlighted
static final int highColor = 0x000000FF; // Highlighted
static final int highBGColor = 0x00CCCCCC; // Highlighted Background
static int width; //屏幕寬
static int height; // 屏幕高
static int startHeight; // 菜單開始的高度
static final int spacing = highFont.getHeight()/2; // 菜單項間的距離
// 菜單項
static final String[] mainMenu = {"New Game","High Score","Settings","Help","About"};
// 當前高亮顯示的索引號
static int menuIdx;
Thread thread;
// 背景圖
Image bgImage;
//構造
public MenuScreen() {
width = getWidth();
height = getHeight();
// 計算菜單開始的高度
startHeight = (highFont.getHeight() * mainMenu.length) + ((mainMenu.length-1) * spacing);
startHeight = (height - startHeight) / 2;
// 默認所選為菜單的第一項
menuIdx = 0;
try {
bgImage = Image.createImage("/res/bg.png");
} catch (Exception e) {}
thread = new Thread(this);
thread.start();
}
public void run() {
while(true) {
repaint();
}
}
public void paint(Graphics g) {
//清屏
g.setColor(0x00000000);
g.fillRect(0,0,width,height);
// 背景
g.drawImage(bgImage,(width - bgImage.getWidth()) / 2, (height - bgImage.getHeight())/2,20);
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,
(width - highFont.stringWidth(mainMenu)) / 2,
startHeight + (i*highFont.getHeight()) + spacing,
20
);
} else {
g.setFont(lowFont);
g.setColor(lowColor);
g.drawString(mainMenu,
(width - lowFont.stringWidth(mainMenu) ) / 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++;
}else if (getGameAction(code) == Canvas.FIRE)
switch(menuIdx) {
case NEW_GAME: System.out.println("Start New Game"); break;
case HIGH_SCORE: System.out.println("Display High Score"); break;
case SETTINGS: System.out.println("Display Settings"); break;
case HELP: System.out.println("Display Help"); break;
case ABOUT: System.out.println("Display About Info."); break;
}
}
}
順便這里介紹一下J2ME中的字體:
字體的屬性由:字體類型,風格和字體大小構成,請注意顏色并不是字體的屬性。字體
類型由Form類中的靜態常量進行定義,可能的取值:
FACE_MONOSPACE: 等寬字體
FACE_PROPORTIONAL: 比例字體,非常寬
FACE_SYSTEM: 系統字體
字體風格由Font 類中的靜態常量進行定義,字體風格是可以多選的,可能的取值為:
STYLE_BOLD : 加粗
STYLE_ITALIC :斜體
STYLE_PLAIN :常規
STYLE_UNDERLINED:帶下劃線字體
字體由Font 類中的靜態常量進行定義,可能的取值為:
SIZE_LARGE: 大號字體
SIZE_MEDIUM: 中號字體
SIZE_SMALL: 小號字體
創建字體時并不是通過Font 類的構造方法來創建,而是利用Font類的靜態方法
static Font getFont(int face,int style,int size)來創建字體。或者利用
static font getDefauleFont()來創建系統默認字體。
在MIDP v2.0中,為Font類增加了新的一個方法用于創建字體,即 static Font
getFont(int fontSpecifier),參數fontSpecifier的取值范圍被定義為Font類
的靜態常量,目前只能由倆種取值:
FONT_STATIC_TEXT:靜態文本字體,定義了屏幕顯示時設備采用的字體
FONT_INPUT_TEXT:輸入文體,定義了在用戶輸入時設備采用的字體
在顯示文字時,如果文字過長而超過當前屏幕的寬度,那么多余的部分將無法顯示,
所以為了有效地顯示文字可以用檢測字符或者字符串的寬度的方法,由程序判斷每
行輸出的字符數,來達到更好的顯示效果.