package test;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
public class Menu extends Canvas{
private Displayable parentDisplay=null;
private String LeftMenuTitle="选项";
private String RightMenuTitle="取消";
private byte left_right=0;
private byte up_down=0;
private int menuBarBgColor=0XBCBCBC;
private int menuBarFgColor=0;
private Font menuBarFont=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
private String[]radiuoButtonName=null;
private byte indexSelected=0;//选中
private byte indexFource=0;//焦点
private byte maxFource=0;
private boolean isShowMenu=false;
public Menu(){
}
protected void drawRGB(Graphics g) {
int ai[] = new int[this.getWidth()];
for (int j1 = 0; j1 < ai.length; j1++)
ai[j1] = 0x90000000;
g.drawRGB(ai, 0, 0, 0, 0,this.getWidth(),this.getHeight(), true); // 绘制透明景色
}
protected final void paint(Graphics arg0) {
painMenuBar(arg0);
if(isShowMenu)
paintContent(arg0);
}
private void painMenuBar(Graphics arg0){
// arg0.setColor(255,255, 255);
// arg0.fillRect(0,0,this.getWidth(), this.getHeight()-20);
arg0.setColor(menuBarBgColor);
arg0.fillRect(0,this.getHeight()-20,this.getWidth(),20);
arg0.setColor(menuBarFgColor);
arg0.setFont(menuBarFont);
arg0.drawString(LeftMenuTitle,5,this.getHeight()-10-menuBarFont.getHeight()/2,0);
arg0.drawString(RightMenuTitle,this.getWidth()-menuBarFont.stringWidth(RightMenuTitle),this.getHeight()-20+(20-menuBarFont.getHeight())/2,0);
}
private int MenuHight=120;
private int MenuWidth=100;
private int MenuBgColor=0X3eFeB;
private void paintContent(Graphics arg0){
arg0.setColor(MenuBgColor);
arg0.fillRect(0,this.getHeight()-20-MenuHight,MenuWidth+4,MenuHight);
for(int i=0;i<radiuoButtonName.length;i++){
if(i==indexFource){
arg0.setColor(0X1035);
arg0.fillRect(2,this.getHeight()-20-(menuBarFont.getHeight()+menuBarFont.getHeight()*i),MenuWidth, menuBarFont.getHeight()) ;
}
arg0.setColor(0XBBBBB);
arg0.drawString(radiuoButtonName[i],3,this.getHeight()-20-menuBarFont.getHeight()*(i+1),0);
}
}
private Display display=null;//窗口容器
public void add(Display display){
this.display=display;
}
protected void keyPressed(int keyCode) {
if(keyCode==-2){
if(++indexFource>maxFource-1)
indexFource=0;
}
else if(keyCode==-1){
if(--indexFource<0)
indexFource=(byte) (maxFource-1);
}
else if(keyCode==-5){
}
else if(keyCode==-6)
isShowMenu=true;
else if(keyCode==-7){
isShowMenu=false;
setVisible(false);
}
repaint();
}
public void add(String[] buttonName){
radiuoButtonName=buttonName;
maxFource+=radiuoButtonName.length;
}
public void setMenuBarBgColor(int titleBgColor){
this.menuBarBgColor=titleBgColor;
repaint();
}
public void setMenuBarFont(Font font){//设置字体
menuBarFont=font;
repaint();
}
public void setVisible(boolean isVisible){
// this.setFullScreenMode(true);
if(isVisible){
parentDisplay=display.getCurrent();
display.setCurrent(this);
}
else{
display.setCurrent(parentDisplay);
}
}
public String getLeftMenuTitle() {
return LeftMenuTitle;
}
public void setLeftMenuTitle(String leftMenuTitle) {
LeftMenuTitle = leftMenuTitle;
}
public String getRightMenuTitle() {
return RightMenuTitle;
}
public void setRightMenuTitle(String rightMenuTitle) {
RightMenuTitle = rightMenuTitle;
}
}