MenuCanvas.java
上传用户:pyly5030
上传日期:2021-09-17
资源大小:174k
文件大小:4k
- import java.io.IOException;
- import javax.microedition.lcdui.Alert;
- import javax.microedition.lcdui.AlertType;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- public class MenuCanvas extends Canvas implements Runnable{
- private GameCanvasMidlet midlet;
- private Display display;
- //set font
- static final Font lowFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL);
- static final Font highFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
-
- //set color
- static final int lowColor = 0x00FFFFFF;
- static final int highColor = 0x00FF0000;
- static final int highBgColor = 0x00CCCCCC;
-
- static int width;
- static int height = 320;
- static int startHeight;
- static final int spacing = highFont.getHeight()/2;
- static final String[] mainMenu = {"新游戏","最高分","设置","帮助","退出游戏"};
- static int menuIdx;
- private Alert alert;
- static Image bgImage;
-
- public MenuCanvas(GameCanvasMidlet midlet) throws IOException{
- this.midlet = midlet;
- width = getWidth();
- //height = getHeight();
- startHeight = (highFont.getHeight()* mainMenu.length) + (mainMenu.length-1)*spacing;
- startHeight = (height-startHeight)/2;
- menuIdx = 0;
- bgImage = Image.createImage("/menubg.jpg");
- Thread menuThread = new Thread(this);
- menuThread.start();
- }
- protected void paint(Graphics g) {
- try {
- //Image image = Image.createImage("/menubg.jpg");
- g.drawImage(bgImage, 0,0, g.TOP|g.LEFT);
- } catch (Exception e) {
- e.printStackTrace();
- }
- g.setColor(0x00FFFFFF);
- g.setFont(lowFont);
- //g.fillRect(0, 0, width, height);
- //g.setColor(0x00000000);
- g.drawString("v0.1", width-30, height-25, g.TOP|g.LEFT);
- 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[i], (width-highFont.stringWidth(mainMenu[i]))/2, startHeight+(i*highFont.getHeight()+spacing), 20);
- }else{
- g.setFont(lowFont);
- g.setColor(lowColor);
- g.drawString(mainMenu[i], (width-lowFont.stringWidth(mainMenu[i]))/2, startHeight+(i*highFont.getHeight()+spacing), 20);
- }
- }
- }
- public void run() {
- while(true){
- repaint();
- try {
- Thread.sleep(15L);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- protected void keyPressed(int code) {
- try {
- int key = getGameAction(code);
- if(key == Canvas.UP && menuIdx -1 >= 0){
- menuIdx--;
- }else if(key==Canvas.DOWN && menuIdx+1 < mainMenu.length){
- menuIdx++;
- }else if((key & Canvas.FIRE) != 0){
- switch(menuIdx){
- case 0:
- MyGameCanvas canvas = new MyGameCanvas();
- display = Display.getDisplay(midlet);
- display.setCurrent(canvas);
- break;
- case 1:
- showHighScore();
- break;
- case 2:
- showSetting();
- break;
- case 3:
- showHelp();
- break;
- case 4:
- midlet.exit();
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void showHighScore() {
- alert = new Alert("HighScore", "1.12000",
- null, null);
- alert.setTimeout(Alert.FOREVER);
- alert.setType(AlertType.INFO);
- midlet.mainMenuScreenShow(alert);
- }
-
- private void showSetting() {
- alert = new Alert("Setting", "游戏设置",
- null, null);
- alert.setTimeout(Alert.FOREVER);
- alert.setType(AlertType.INFO);
- midlet.mainMenuScreenShow(alert);
- }
-
- private void showHelp() {
- alert = new Alert("Help", "选择产品支持和帮助中心查看产品支持信息,包括常见问题、"
- + "有用链接、提示和“如何”文章以及最新下载资源。从以下类别中选择产品或访问按字母顺序排列的产品支持中心索引。",
- null, null);
- alert.setTimeout(Alert.FOREVER);
- alert.setType(AlertType.INFO);
- midlet.mainMenuScreenShow(alert);
- }
- }