JavaMe低级界面绘图之菜单
【问题描述】
手机程序中,菜单使用很普遍,JavaMe中,如何绘制菜单呢?
下面分享一下实现的菜单功能,实现效果如图1所示:
【代码清单】
Color.java(借鉴 了kf156(亚日),J2ME canvas 多级菜单的实现)
-
package com.token.view.components;
-
-
/**
-
* 颜色类
-
*
-
* @author kf156(亚日)
-
*
-
* @Email kf156@QQ.com
-
*
-
* @CopyrightNotice 此代码仅供学习交流,您可以使用并传播本源代码,但请保留作者信息。切勿商用,若有需要,请先于本人联系。
-
*/
-
public class Color {
-
-
public static int strong = 0xFF8C00;//令牌显示颜色
-
-
public static int warning1 = 0xFF4500;// 警告颜色
-
-
public static int warning2 = 0xFF0000;// 警告颜色
-
-
public static int error = 0xFF0000;// 错误颜色
-
-
public static int background = 0xF0FAE6;// 背景颜色
-
-
public static int menuFrame = 0xB8E58A;// 窗体框架颜色
-
-
public static int label = 0xF0F8FF;// 标题颜色
-
-
public static int text = 0x314F12;// 正文颜色
-
-
public static int selectText = 0xFFFFFF;// 被选择后的正文颜色
-
-
public static int selectBg = 0x548720;// 被选择后的背景颜色
-
-
public static int cmdBarTop = 0x9FDB63;// 命令栏上半部分颜色
-
-
public static int cmdBarBottom = 0x81D130;// 命令栏下半部分颜色
-
-
public static int menuBg = 0xE0F5CB;// 命令菜单主背景色
-
-
public static int cmdSelectTop = 0x7DAB4F;// 命令菜单选中项上半部分颜色
-
-
public static int cmdSelectBottom = 0x5B9422;// 命令菜单选中项下半部分颜色
-
-
public static int linkClick = 0xFF0000;// 超链接点击后的颜色
-
-
public static int frame = 0x458B00;//边框颜色
-
-
public static int frameBg = 0x556B2F;//边框背景颜色
-
-
public static final int GREY = 0xBBBBBB;// 灰边框
-
-
public static final int BLACK = 0x0;// 黑色
-
-
public static final int WHITE = 0xFFFFFF;// 白色
-
-
public static final int TRANSLUCENCY_WRITE = 0x55FFFFFF;// 白色半透明
-
-
public static final int TRANSLUCENCY_BLACK = 0x55000000;// 黑色半透明
-
-
public static final byte STYLE_RED = 0;// 红色
-
-
public static final byte STYLE_ORANGE = 1;// 橙色
-
-
public static final byte STYLE_YELLOW = 2;// 黄色
-
-
public static final byte STYLE_GREEN = 3;// 绿色
-
-
public static final byte STYLE_LIME = 4;// 酸橙色
-
-
public static final byte STYLE_CYAN = 5;// 青色
-
-
public static final byte STYLE_AQUA = 6;// 浅绿色
-
-
public static final byte STYLE_SKY_BLUE = 7;// 天蓝色
-
-
public static final byte STYLE_BLUE = 8;// 蓝色
-
-
public static final byte STYLE_PURPLE = 9;// 紫色
-
-
public static final byte STYLE_FUCHSIA = 10;// 紫红色
-
-
public static final byte STYLE_MAGENTA = 11;// 红紫色
-
-
public static final byte STYLE_BLACK = 12;// 黑色
-
-
public static int colorName = STYLE_GREEN;// 风格名称
-
-
// 红色风格H0(#FF0000)
-
private static void redColor() {
-
background = 0xFAE7E7;// 背景颜色
-
label = 0xFAEBD7;// 标题颜色
-
text = 0x4F1212;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0xE58A8A;// 窗体框架颜色
-
menuBg = 0xF5CBCB;// 命令菜单主背景色
-
selectBg = 0x872020;// 被选择后的背景颜色
-
cmdBarTop = 0xDB6363;// 命令栏上半部分颜色
-
cmdBarBottom = 0xD13030;// 命令栏下半部分颜色
-
cmdSelectTop = 0xAB4F4F;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x942222;// 命令菜单选中项下半部分颜色
-
frame = 0xF70909;//边框颜色
-
frameBg = 0xD52B2B;//边框背景颜色
-
}
-
-
// 橙色风格H30(#FF7F00)
-
private static void orangeColor() {
-
background = 0xFAF0E6;// 背景颜色
-
label = 0xFFE4B5;// 标题颜色
-
text = 0x4F3112;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0xE5B88A;// 窗体框架颜色
-
menuBg = 0xF5E0CB;// 命令菜单主背景色
-
selectBg = 0x875420;// 被选择后的背景颜色
-
cmdBarTop = 0xDB9F63;// 命令栏上半部分颜色
-
cmdBarBottom = 0xD18130;// 命令栏下半部分颜色
-
cmdSelectTop = 0xAB7D4F;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x945B22;// 命令菜单选中项下半部分颜色
-
frame = 0xF76809;//边框颜色
-
frameBg = 0xD56F2B;//边框背景颜色
-
}
-
-
// 黄色风格H60(#FFFF00)
-
private static void yellowColor() {
-
background = 0xFAFAE6;// 背景颜色
-
label = 0xFFFACD;// 标题颜色
-
text = 0x4F4F12;// 正文颜色
-
// selectText = 0xFFFF00;// 被选择后的正文颜色
-
menuFrame = 0xE5E58A;// 窗体框架颜色
-
menuBg = 0xF5F5CB;// 命令菜单主背景色
-
selectBg = 0x878720;// 被选择后的背景颜色
-
cmdBarTop = 0xDBDB63;// 命令栏上半部分颜色
-
cmdBarBottom = 0xD1D130;// 命令栏下半部分颜色
-
cmdSelectTop = 0xABAB4F;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x949422;// 命令菜单选中项下半部分颜色
-
frame = 0xF7C709;//边框颜色
-
frameBg = 0xD5D52B;//边框背景颜色
-
}
-
-
// 绿色风格H90(#80FF00)
-
private static void greenColor() {
-
background = 0xF0FAE6;// 背景颜色
-
label = 0xF0F8FF;// 标题颜色
-
text = 0x314F12;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0xB8E58A;// 窗体框架颜色
-
menuBg = 0xE0F5CB;// 命令菜单主背景色
-
selectBg = 0x548720;// 被选择后的背景颜色
-
cmdBarTop = 0x9FDB63;// 命令栏上半部分颜色
-
cmdBarBottom = 0x81D130;// 命令栏下半部分颜色
-
cmdSelectTop = 0x7DAB4F;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x5B9422;// 命令菜单选中项下半部分颜色
-
frame = 0x458B00;//边框颜色
-
frameBg = 0x556B2F;//边框背景颜色
-
}
-
-
// 酸橙色风格H120(0x00FF00)
-
private static void limeColor() {
-
background = 0xE6FAE6;// 背景颜色
-
label = 0xEEE9BF;// 标题颜色
-
text = 0x124F12;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0x8AE58A;// 窗体框架颜色
-
menuBg = 0xCBF5CB;// 命令菜单主背景色
-
selectBg = 0x208720;// 被选择后的背景颜色
-
cmdBarTop = 0x63DB63;// 命令栏上半部分颜色
-
cmdBarBottom = 0x30D130;// 命令栏下半部分颜色
-
cmdSelectTop = 0x4FAB4F;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x229422;// 命令菜单选中项下半部分颜色
-
frame = 0x09F738;//边框颜色
-
frameBg = 0x2BD54D;//边框背景颜色
-
}
-
-
// 青色风格H150(0x00FF80)
-
private static void cyanColor() {
-
background = 0xE6FAF0;// 背景颜色
-
label = 0xE0FFFF;// 标题颜色
-
text = 0x124F31;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0x8AE5B8;// 窗体框架颜色
-
menuBg = 0xCBF5E0;// 命令菜单主背景色
-
selectBg = 0x208754;// 被选择后的背景颜色
-
cmdBarTop = 0x63DB9F;// 命令栏上半部分颜色
-
cmdBarBottom = 0x30D181;// 命令栏下半部分颜色
-
cmdSelectTop = 0x4FAB7D;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x22945B;// 命令菜单选中项下半部分颜色
-
frame = 0x4DB34D;//边框颜色
-
frameBg = 0x8AB34D;//边框背景颜色
-
}
-
-
// 浅绿色风格H180(#00FFFF)
-
private static void aquaColor() {
-
background = 0xE6FAFA;// 背景颜色
-
label = 0xE0EEE0;// 标题颜色
-
text = 0x445500;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0x88AA55;// 窗体框架颜色
-
menuBg = 0xE6FAFA;// 命令菜单主背景色
-
selectBg = 0xAECC33;// 被选择后的背景颜色
-
cmdBarTop = 0xBDE61A;// 命令栏上半部分颜色
-
cmdBarBottom = 0xB8DD22;// 命令栏下半部分颜色
-
cmdSelectTop = 0xB8DD22;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0xB3D52B;// 命令菜单选中项下半部分颜色
-
frame = 0x8E8E38;//边框颜色
-
frameBg = 0x8B8B00;//边框背景颜色
-
}
-
-
// 天蓝色风格H210(#0080FF)
-
private static void skyBlueColor() {
-
background = 0xE6F0FA;// 背景颜色
-
label = 0xE0FFFF;// 标题颜色
-
text = 0x12314F;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0x8AB8E5;// 窗体框架颜色
-
menuBg = 0xCBE0F5;// 命令菜单主背景色
-
selectBg = 0x205487;// 被选择后的背景颜色
-
cmdBarTop = 0x639FDB;// 命令栏上半部分颜色
-
cmdBarBottom = 0x3081D1;// 命令栏下半部分颜色
-
cmdSelectTop = 0x4F7DAB;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x225B94;// 命令菜单选中项下半部分颜色
-
frame = 0x0997F7;//边框颜色
-
frameBg = 0x2BB3D5;//边框背景颜色
-
}
-
-
// 深蓝色风格H240(#0000FF)
-
private static void blueColor() {
-
background = 0xE6E6FA;// 背景颜色
-
label = 0xE0EEEE;// 标题颜色
-
text = 0x12124F;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0x8A8AE5;// 窗体框架颜色
-
menuBg = 0xCBCBF5;// 命令菜单主背景色
-
selectBg = 0x202087;// 被选择后的背景颜色
-
cmdBarTop = 0x6363DB;// 命令栏上半部分颜色
-
cmdBarBottom = 0x3030D1;// 命令栏下半部分颜色
-
cmdSelectTop = 0x4F4FAB;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x222294;// 命令菜单选中项下半部分颜色
-
frame = 0x0909F7;//边框颜色
-
frameBg = 0x2B2BD5;//边框背景颜色
-
}
-
-
// 紫色风格H270(#8000FF)
-
private static void purpleColor() {
-
background = 0xF0E6FA;// 背景颜色
-
label = 0xFDF5E6;// 标题颜色
-
text = 0x31124F;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0xB88AE5;// 窗体框架颜色
-
menuBg = 0xE0CBF5;// 命令菜单主背景色
-
selectBg = 0x542087;// 被选择后的背景颜色
-
cmdBarTop = 0x9F63DB;// 命令栏上半部分颜色
-
cmdBarBottom = 0x8130D1;// 命令栏下半部分颜色
-
cmdSelectTop = 0x7D4FAB;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x5B2294;// 命令菜单选中项下半部分颜色
-
frame = 0x6809F7;//边框颜色
-
frameBg = 0x6F2BD5;//边框背景颜色
-
}
-
-
// 紫红色风格H300(#FF00FF)
-
private static void fuchsiaColor() {
-
background = 0xFAE6FA;// 背景颜色
-
label = 0xFAF0E6;// 标题颜色
-
text = 0x4F124F;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0xE58AE5;// 窗体框架颜色
-
menuBg = 0xF5CBF5;// 命令菜单主背景色
-
selectBg = 0x872087;// 被选择后的背景颜色
-
cmdBarTop = 0xDB63DB;// 命令栏上半部分颜色
-
cmdBarBottom = 0xD130D1;// 命令栏下半部分颜色
-
cmdSelectTop = 0xAB4FAB;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x942294;// 命令菜单选中项下半部分颜色
-
frame = 0x9709F7;//边框颜色
-
frameBg = 0xB32BD5;//边框背景颜色
-
}
-
-
// 红紫色风格H330(#FF007F)
-
private static void magentaColor() {
-
background = 0xFAE6F0;// 背景颜色
-
label = 0xFFEFD5;// 标题颜色
-
text = 0x4F1231;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0xE58AB8;// 窗体框架颜色
-
menuBg = 0xF5CBE0;// 命令菜单主背景色
-
selectBg = 0x872054;// 被选择后的背景颜色
-
cmdBarTop = 0xDB639F;// 命令栏上半部分颜色
-
cmdBarBottom = 0xD13081;// 命令栏下半部分颜色
-
cmdSelectTop = 0xAB4F7D;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x94225B;// 命令菜单选中项下半部分颜色
-
frame = 0xC709F7;//边框颜色
-
frameBg = 0xD52BD5;//边框背景颜色
-
}
-
-
//黑色风格(#000000)
-
private static void blackColor() {
-
background = 0xEDEDED;// 背景颜色
-
label = 0xEAEAEA;// 标题颜色
-
text = 0x030303;// 正文颜色
-
// selectTextColor = 0xFFFFFF;// 被选择后的正文颜色
-
menuFrame = 0x2E2E2E;// 窗体框架颜色
-
menuBg = 0xC9C9C9;// 命令菜单主背景色
-
selectBg = 0x4A4A4A;// 被选择后的背景颜色
-
cmdBarTop = 0x3B3B3B;// 命令栏上半部分颜色
-
cmdBarBottom = 0x383838;// 命令栏下半部分颜色
-
cmdSelectTop = 0x2E2E2E;// 命令菜单选中项上半部分颜色
-
cmdSelectBottom = 0x0D0D0D;// 命令菜单选中项下半部分颜色
-
frame = 0x0A0A0A;//边框颜色
-
frameBg = 0x636363;//边框背景颜色
-
}
-
-
// 风格切换
-
public static void nextColor() {
-
set(++colorName);
-
-
}
-
-
/**
-
* 设置颜色
-
*
-
*/
-
public static void set(int color) {
-
// System.out.println(color);
-
switch (color) {
-
case STYLE_RED:// 红色
-
redColor();
-
break;
-
-
case STYLE_ORANGE:// 橙色
-
orangeColor();
-
break;
-
-
case STYLE_YELLOW:// 黄色
-
yellowColor();
-
break;
-
-
case STYLE_GREEN:// 绿色
-
greenColor();
-
break;
-
-
case STYLE_LIME:// 酸橙色
-
limeColor();
-
break;
-
-
case STYLE_CYAN:// 青色
-
cyanColor();
-
break;
-
-
case STYLE_AQUA:// 浅绿色
-
aquaColor();
-
break;
-
-
case STYLE_SKY_BLUE:// 天蓝色
-
skyBlueColor();
-
break;
-
-
case STYLE_BLUE:// 蓝色
-
blueColor();
-
break;
-
-
case STYLE_PURPLE:// 紫色
-
purpleColor();
-
break;
-
-
case STYLE_FUCHSIA:// 紫红色
-
fuchsiaColor();
-
break;
-
-
case STYLE_MAGENTA:// 红紫色
-
magentaColor();
-
break;
-
-
case STYLE_BLACK:// 红紫色
-
blackColor();
-
break;
-
-
default:// 颜色循环完毕,返回红色
-
colorName = STYLE_RED;
-
redColor();// RED
-
break;
-
}
-
}
-
-
// 设置新风格
-
public static void setNewColor(int newColor) {
-
background = newColor;
-
menuFrame = background - 0x3A155D;
-
label = background - 0x25A8E5;
-
text = background - 0xC0ACD5;
-
selectText = background + 0x100518;
-
selectBg = background - 0x9D72C7;
-
cmdBarTop = background - 0x531E84;
-
cmdBarBottom = background - 0x742AB8;
-
}
-
}
-
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,后续文章将给予解释。
【参考文献】
(1) J2ME canvas 多级菜单的实现
(2) http://www.iteye.com/topic/479217
原文链接:http://blog.csdn.net/tandesir/article/details/7536778