KeyID.Java
- package com.token.vIEw.components;
- public class KeyID {
- public static final byte KEY_EDIT=-5;//選擇
- public static final byte SOFT_LEFT=-6;//左軟鍵
- public static final byte SOFT_RIGHT=-7;//右軟鍵
- private KeyID(){
- }
- }
Menu.Java
- package com.token.vIEw.components;
- import Javax.microedition.lcdui.*;
- import Javax.microedition.lcdui.game.*;
- public class Menu
- {
- private String leftOption;
- private String rightOption;
- private String cancelOption = "取消";
- private String okOption = "確定";
- private String[] menuOptions;
- private int padding;
- private Font ft;
- private int width;
- private int height;
- public int menuHeight;
- public Menu(GameCanvas canvas)
- {
- width = canvas.getWidth();
- height = canvas.getHeight();
- }
- public Menu(GameCanvas canvas, String leftOption, String rightOption, String[] menuOptions) {
- this.leftOption = leftOption;
- this.rightOption = rightOption;
- this.menuOptions = menuOptions;
- width = canvas.getWidth();
- height = canvas.getHeight();
- } // end constructor
- //繪制軟鍵選項
- public void drawMenu(GameCanvas canvas, Graphics graphics, String leftOption, String rightOption)
- {
- width = canvas.getWidth();
- height = canvas.getHeight();
- padding = 5;
- ft = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
- Font.SIZE_LARGE);
- int fontHeight = ft.getHeight();
- menuHeight = fontHeight + 2 * padding;
- graphics.setColor(Color.cmdSelectTop);
- graphics.fillRect(0, height-menuHeight, width, menuHeight/2+1);
- graphics.setColor(Color.cmdSelectBottom);
- graphics.fillRect(0, height-menuHeight/2, width, menuHeight/2);
- graphics.setFont(ft);
- graphics.setColor(Color.label);
- if(leftOption!=null)
- {
- graphics.drawString(leftOption, padding, height-padding, Graphics.LEFT | Graphics.BOTTOM);
- }
- if(rightOption!=null)
- {
- graphics.drawString(rightOption, width-padding, height-padding, Graphics.RIGHT | Graphics.BOTTOM);
- }
- }
- public void drawSubMenu(Graphics graphics, int selectedOptionIndex)
- {
- padding = 6;
- ft = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
- Font.SIZE_MEDIUM);
- int fontHeight = ft.getHeight();
- //確定菜單的最大寬度、最大高度
- int submenuMaxWidth = 0;
- int submenuMaxHeight = 0;
- int currentWidth = 0;
- for (int i = 0; i < menuOptions.length; i++)
- {
- currentWidth = ft.stringWidth(menuOptions[i]);
- if (currentWidth > submenuMaxWidth)
- {
- submenuMaxWidth = currentWidth;
- }
- submenuMaxHeight += fontHeight + padding;
- }
- submenuMaxWidth += 6 * padding;//5個padding用於顯示字符
- int sub_padding = 3;
- int sub_margin_left = 2;
- int sub_margin_bottom = 2;
- int submenuFrameMaxWidth = submenuMaxWidth + 2*sub_padding;
- int submenuFrameMaxHeight = submenuMaxHeight + 2*sub_padding;
- graphics.setColor(Color.menuFrame);
- graphics.drawRoundRect(sub_margin_left, // x
- height - menuHeight - submenuFrameMaxHeight-sub_margin_bottom, // y
- submenuFrameMaxWidth, submenuFrameMaxHeight,5,5);
- graphics.setColor(Color.menuBg);
- graphics.fillRoundRect(sub_margin_left+1, // x
- height - menuHeight - submenuFrameMaxHeight-sub_margin_bottom+1, // y
- submenuFrameMaxWidth-1, submenuFrameMaxHeight-1,5,5);
- graphics.setFont(ft);
- int menuOptionX = sub_margin_left + sub_padding + padding;
- int menuOptionY = height - menuHeight - submenuMaxHeight-sub_margin_bottom-sub_padding+padding/2;
- for (int i = 0; i < menuOptions.length; i++)
- {
- if (i != selectedOptionIndex)
- {
- // draw unselected menu option
- graphics.setColor(Color.text); // black
- graphics.drawString(menuOptions[i], menuOptionX, menuOptionY, Graphics.LEFT
- | Graphics.TOP);
- }
- else
- {
- // draw selected menu option
- graphics.setColor(Color.selectBg);
- int highOptionY=height - menuHeight - sub_margin_bottom - submenuMaxHeight + i*(fontHeight + padding)-sub_padding;
- graphics.fillRect(sub_margin_left+sub_padding,highOptionY,submenuMaxWidth+1,fontHeight + padding);
- graphics.setColor(Color.selectText);
- graphics.drawString(menuOptions[i], menuOptionX, menuOptionY, Graphics.LEFT
- | Graphics.TOP);
- }
- menuOptionY += padding + fontHeight;
- }
- }
- public void drawInactiveMenu(GameCanvas canvas, Graphics graphics)
- {
- drawMenu(canvas, graphics,leftOption,rightOption);
- canvas.flushGraphics();
- } // end drawInactiveMenu
- public void drawActiveMenu(GameCanvas canvas, Graphics graphics, int selectedOptionIndex)
- {
- drawMenu(canvas, graphics,cancelOption,okOption);
- // draw menu options
- if (menuOptions != null)
- {
- drawSubMenu(graphics,selectedOptionIndex);
- canvas.flushGraphics();
- }
- }
- }
MainScreen.Java(程序有刪減,僅供學習交流)
- package com.token.vIEw;
- import Javax.microedition.lcdui.*;
- import Javax.microedition.lcdui.game.GameCanvas;
- import com.token.util.*;
- import com.token.vIEw.components.*;
- public class MainScreen extends GameCanvas
- {
- private UIController controller;
- private Font ft;
- private int width;
- private int height;
- static final String[] menuOptions = { "1.查看","2.更新","3.用戶管理","4.幫助","5.換膚","6.退出" };
- static int menuIdx;// 記錄是在第幾個菜單處按下的確定鍵。
- private int currentlySelectedIndex = 0;
- private boolean menuIsActive = false;
- private String leftOption;
- private String rightOption;
- private Graphics graphics;
- private Menu menu;
- public MainScreen(UIController control)
- {
- super(false);
- this.controller = control;
- setFullScreenMode(true);
- ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
- width = getWidth();
- height = getHeight();
- graphics = getGraphics();
- menuIdx = 0;
- leftOption = "選項";
- rightOption = "";
- menu = new Menu(this, leftOption, rightOption, menuOptions);
- }
- public void show(Object[] args) {
- draw();
- //flushGraphics();
- }
- private void draw()
- {
- menu.drawInactiveMenu(this, graphics);
- }
- public void drawBase()
- {
- //clearScreen();
- }
- public void clearScreen()
- {
- graphics.setColor(0xff,0xff,0xff);
- graphics.fillRect(0, 0, width, height);
- flushGraphics();
- }
- protected void keyPressed(int keyCode)
- {
- if (menuIsActive)
- {
- if (keyCode == KeyID.SOFT_LEFT)
- {
- // cancel pressed, draw inactive menu again
- drawBase();
- menu.drawInactiveMenu(this, graphics);
- menuIsActive = false;
- }
- switch(keyCode)
- {
- case KEY_NUM1:
- currentlySelectedIndex = 0;
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- break;
- case KEY_NUM2:
- currentlySelectedIndex = 1;
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- break;
- case KEY_NUM3:
- currentlySelectedIndex = 2;
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- break;
- case KEY_NUM4:
- currentlySelectedIndex = 3;
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- break;
- case KEY_NUM5:
- currentlySelectedIndex = 4;
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- break;
- case KEY_NUM6:
- currentlySelectedIndex = 5;
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- break;
- case KeyID.SOFT_RIGHT:
- keyCode = FIRE;
- break;
- default:;
- }
- keyCode = (keyCode != FIRE)?getGameAction(keyCode):keyCode;
- switch(keyCode)
- {
- case UP:
- if(currentlySelectedIndex==0)
- {
- currentlySelectedIndex = menuOptions.length-1;
- }
- else
- {
- currentlySelectedIndex--;
- if (currentlySelectedIndex < 0)
- {
- currentlySelectedIndex = 0; // stay within limits
- }
- }
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- break;
- case DOWN:
- if(currentlySelectedIndex==menuOptions.length-1)
- {
- currentlySelectedIndex = 0;
- }
- else
- {
- currentlySelectedIndex++;
- if (currentlySelectedIndex >= menuOptions.length)
- {
- currentlySelectedIndex = menuOptions.length - 1;
- }
- }
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- break;
- case FIRE:
- {
- drawBase();
- graphics.setColor(0x000000);
- menu.drawInactiveMenu(this, graphics);
- menuIsActive = false;
- //按鍵處理
- switch(currentlySelectedIndex)
- {
- case 0:
- {
- //Object []args = {"vIEwScreen", "userAuth_name",null,null};
- //controller.handleEvent(UIController.EventID.EVENT_VIEW_AUTH,args);
- break;
- }
- case 1:
- {
- //Object []args = {"updateScreen", "userAuth_name",null,null};
- //controller.handleEvent(UIController.EventID.EVENT_UPDATE_AUTH, args);
- break;
- }
- case 2:
- {
- //Object []args = {"manageScreen", "userAuth_name",null,null};
- //controller.handleEvent(UIController.EventID.EVENT_USER_MANAGE_AUTH, args);
- break;
- }
- case 3:
- {
- //controller.handleEvent(UIController.EventID.EVENT_SHOW_HELP,null);
- break;
- }
- case 4:
- {
- break;
- }
- case 5:
- {
- //controller.handleEvent(UIController.EventID.EVENT_EXIT,null);
- break;
- }
- default:;
- }
- break;
- }
- default:;
- }
- }
- else
- {
- if (keyCode == KeyID.SOFT_LEFT)
- { // "Options" pressed
- menu.drawActiveMenu(this, graphics, currentlySelectedIndex);
- menuIsActive = true;
- }
- else if (keyCode == KeyID.SOFT_RIGHT)
- {
- }
- }
- }
- }
*關於UIController,後續文章將給予解釋。