📄 gamemenu.java
字号:
/*
* 创建日期 2005-9-11
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package cn.org.matrix.gmatrix.gameLab.util.ui;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Font;
import cn.org.matrix.gmatrix.gameLab.control.*;
/**
* 游戏菜单类
* @author cleverpig
*
*/
public class GameMenu extends GameUI implements Runnable{
//屏幕背景图片
private Image img=null;
//用户当前选择的菜单项所在的位置
private int currentX;
private int currentY;
//菜单项水平间隔
// private int itemVStuffing=2;
//菜单项竖直间隔
private int itemHStuffing=5;
//菜单字体
private Font menuFont=null;
//菜单项文字数组
private Description[] items=null;
//游戏标题
private Description title=null;
//用户选择的菜单项下标
private int selectIndex=0;
//运行标志
private boolean running=true;
/**
* 装载菜单
* @param imgResURL 背景图片的URL
* @param menuItemArray 菜单项数组
* @param titleParam 游戏名称
* @param bgColor 背景颜色
* @param uiBgColor ui背景颜色
* @param uiBorderColor ui边框颜色
*/
public void load(String imgResURL,
Description[] menuItemArray,Description titleParam,
int bgColor,int uiBgColor,int uiBorderColor
) {
//设置配色方案
this.setColorScheme(new UIColorScheme(bgColor,uiBgColor,uiBorderColor));
this.title=titleParam;
if (imgResURL!=null){
try{
img=Image.createImage(imgResURL);
}
catch(Exception ex){
ex.printStackTrace();
}
}
this.items=menuItemArray;
//统一文字字体:使用菜单项的第一项的字体作为统一使用的字体
menuFont=items[0].getFont();
for(int i=0;i<items.length;i++){
items[i].setFont(menuFont);
}
//计算菜单的总高度
this.getScope().setUIHeight(menuFont.getHeight()*(items.length));
//计算菜单项中最大的宽度
int stringLen=0;
for(int i=0;i<items.length;i++){
if (items[i].getContent().length()>stringLen){
this.getScope().setUIWidth(menuFont.stringWidth(items[i].getContent()));
stringLen=items[i].getContent().length();
}
}
//赋值菜单的左上角坐标
this.getScope().setLeftTopCoordinates(
getWidth()/2-this.getScope().getUIWidth()/2,getHeight()/3
);
//赋值菜单的右下角坐标
this.getScope().setRightBottomCoordinates(
getWidth()/2+this.getScope().getUIWidth()/2,
this.getScope().getLeftTopCoordinates().getY()+this.getScope().getUIHeight()-menuFont.getHeight()
);
//赋值当前被选择的菜单项所在位置
currentX=this.getScope().getLeftTopCoordinates().getX();
currentY=this.getScope().getLeftTopCoordinates().getY();
//设置游戏Title位置
title.setDescriptionPosition(getWidth()/2-title.getFont().stringWidth(title.getContent())/2,getHeight()/3-itemHStuffing*2);
System.out.println("this.getScope().getUIWidth()/2="+this.getScope().getUIWidth()/2);
System.out.println("getWidth()/2="+getWidth()/2);
selectIndex=0;
}
/**
* 绘画
* @param g Graphics对象
*/
public void paint(Graphics g) {
int xPos;
int yPos;
//画背景
if (img!=null){
g.drawImage(img,0,0,Graphics.TOP|Graphics.LEFT);
}
else{
g.setColor(this.getColorScheme().getBgColor());
g.fillRect(0,0,getWidth(),getHeight());
xPos=title.getDescriptionPosition().getX();
yPos=title.getDescriptionPosition().getY();
//画出游戏标题
g.setColor(title.getContentColor());
g.setFont(title.getFont());
g.drawString(title.getContent(),xPos,yPos,Graphics.BOTTOM|Graphics.LEFT);
}
//画出菜单项
for(int i=0;i<items.length;i++){
xPos=currentX;
yPos=this.getScope().getLeftTopCoordinates().getY()+i*menuFont.getHeight()+i*+itemHStuffing;
if (i==selectIndex){
g.setColor(this.getColorScheme().getUIBgColor());
g.fillRect(xPos,yPos,this.getScope().getUIWidth(),menuFont.getHeight());
g.setColor(this.getColorScheme().getUIBorderColor());
g.drawRect(xPos,yPos,this.getScope().getUIWidth(),menuFont.getHeight());
}
g.setFont(items[i].getFont());
g.setColor(items[i].getContentColor());
yPos=this.getScope().getLeftTopCoordinates().getY()+i*menuFont.getHeight()+i*+itemHStuffing;
g.drawString(items[i].getContent(),xPos,yPos,Graphics.TOP|Graphics.LEFT);
}
}
/**
* 处理键盘事件
* @keycode 键盘代码
*/
protected void keyPressed(int keycode){
//根据用户输入的键盘代码对当前选择的菜单项进行变换,并赋值selectIndex
switch(KeyManager.getKeyValue(getGameAction(keycode))){
case KeyManager.DOWN_KEYPRESS:
if (currentY<(this.getScope().getRightBottomCoordinates().getY())){
currentY+=menuFont.getHeight()+itemHStuffing;
selectIndex++;
}
else{
currentY=this.getScope().getLeftTopCoordinates().getY();
selectIndex=0;
}
repaint();
break;
case KeyManager.UP_KEYPRESS:
if (currentY>(this.getScope().getLeftTopCoordinates().getY())){
currentY-=menuFont.getHeight()-itemHStuffing;
selectIndex--;
}
else{
currentY=this.getScope().getRightBottomCoordinates().getY();
selectIndex=items.length-1;
}
repaint();
break;
case KeyManager.FIRE_KEYPRESS:
running=false;
break;
}
}
/**
* 线程的run方法
*/
public void run(){
System.out.println("菜单启动");
while(running){
Thread.yield();
}
System.out.println("菜单退出");
}
public int getSelectIndex() {
return selectIndex;
}
public boolean isRunning() {
return running;
}
public void setSelectIndex(int selectIndex) {
this.selectIndex = selectIndex;
}
public void setRunning(boolean running) {
this.running = running;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -