📄 maincanvas.java
字号:
//J2ME MIDlet 程序设计
//主画面
//翁荣建
//2005-06-11
//mainCanvas.java
package gamepuzzle;
import javax.microedition.lcdui.*;
import menu.*;
import game.gameBoard;
public class mainCanvas extends Canvas implements CommandListener {
//Canvas
private Command Menucommand;
private Font font;
private Image image;
private gameBoard GM;
//private CreateImage createImage;
private Image[] img;
public static mainCanvas instance;
//menuGroup1 主菜单组及成员的声明
private MenuGroup menuGroup1;
private int menuGroup1Left;
private int menuGroup1Top;
private int menuGroup1Width;
private int menuGropu1Length;
//主菜单组共有6个成员
private Menu menuGroup1Menu1;
private Menu menuGroup1Menu2;
private Menu menuGroup1Line1;//水平线
private Menu menuGroup1Menu3;
private Menu menuGroup1Line2;//水平线
private Menu menuGroup1Menu4;
//menuGroup2 子菜单组1及成员的声明
private MenuGroup menuGroup2;
private int menuGroup2Left;
private int menuGroup2Top;
private int menuGroup2Width;
private int menuGropu2Length;
//子菜单组1共有两个成员
private Menu menuGroup2Menu1;
private Menu menuGroup2Menu2;
//menuGroup3 子菜单组2及成员的声明
private MenuGroup menuGroup3;
private int menuGroup3Left;
private int menuGroup3Top;
private int menuGroup3Width;
private int menuGropu3Length;
//子菜单组2共有4个成员
private Menu menuGroup3Menu1;
private Menu menuGroup3Line1;//水平线
private Menu menuGroup3Menu2;
private Menu menuGroup3Menu3;
public mainCanvas() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
setCommandListener(this);
addCommand(Menucommand=new Command("菜单",Command.OK,1));
font=Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL);
GM=new gameBoard();
instance=this;
image=Image.createImage("/icon/icon1.png");
img=new Image[7];
img[0]=CreateImage.instance.getImage("/icon/App.png");
img[1]=CreateImage.instance.getImage("/icon/PushPuzzle.png");
img[2]=CreateImage.instance.getImage("/icon/text_ico.png");
img[3]=CreateImage.instance.getImage("/icon/earth_ico.png");
img[4]=CreateImage.instance.getImage("/icon/Desktop.png");
img[5]=CreateImage.instance.getImage("/icon/ManyBalls.png");
img[6]=CreateImage.instance.getImage("/icon/virtualpc_ico.png");
//menuGroup1 构造主菜单主
this.menuGroup1Left = 5;
this.menuGroup1Top = this.getHeight()-4*3-3*2-18*4;
this.menuGroup1Width = 80;
this.menuGropu1Length = 6;//6个成员
this.menuGroup1 = new MenuGroup(menuGroup1Left,menuGroup1Top,menuGroup1Width,menuGropu1Length);
//构造出6个成员
this.menuGroup1Menu1 = new Menu(img[0],"开始游戏 >");
this.menuGroup1Menu2 = new Menu(img[1],"游戏设置 >");
this.menuGroup1Line1 = new Menu();//水平线
this.menuGroup1Menu3 = new Menu(img[2],"关于游戏...");
this.menuGroup1Line2 = new Menu();//水平线
this.menuGroup1Menu4 = new Menu("退出游戏");//没图标的菜单项
//加入菜单组中
this.menuGroup1.Add(this.menuGroup1Menu1);
this.menuGroup1.Add(this.menuGroup1Menu2);
this.menuGroup1.Add(this.menuGroup1Line1);
this.menuGroup1.Add(this.menuGroup1Menu3);
this.menuGroup1.Add(this.menuGroup1Line2);
this.menuGroup1.Add(this.menuGroup1Menu4);
//设主菜单组为不可见
this.menuGroup1.setVisable(false);
//menuGroup2 构造子菜单组1
this.menuGroup2Width = 72;
this.menuGroup2Left = this.menuGroup1Left + this.menuGroup1Width - 4;
if (this.menuGroup2Left + this.menuGroup2Width > this.getWidth() - 1)//如果菜单的右端超出了手机Screen的右端
{this.menuGroup2Left = this.getWidth() - this.menuGroup2Width - 4;
}
this.menuGroup2Top = this.menuGroup1Top + 2;
this.menuGropu2Length = 2;
this.menuGroup2 = new MenuGroup(menuGroup2Left,menuGroup2Top,menuGroup2Width,menuGropu2Length);
//构造成员
this.menuGroup2Menu1 = new Menu(img[3],"新游戏");
this.menuGroup2Menu2 = new Menu("载入游戏");
//加入菜单组中
this.menuGroup2.Add(this.menuGroup2Menu1);
this.menuGroup2.Add(this.menuGroup2Menu2);
//menuGroup3 构造子菜单组2
this.menuGroup3Width = 80;
this.menuGroup3Left = this.menuGroup1Left+this.menuGroup1Width-4;
if (this.menuGroup3Left + this.menuGroup3Width > this.getWidth()-1)//如果菜单的右端超出了手机Screen的右端
{this.menuGroup3Left = this.getWidth()-this.menuGroup3Width-4;
}
this.menuGroup3Top =this.menuGroup1Top+20;
this.menuGropu3Length =4;
this.menuGroup3 = new MenuGroup(menuGroup3Left,menuGroup3Top,menuGroup3Width,menuGropu3Length);
//构造4个成员
this.menuGroup3Menu1 = new Menu(img[4],"场影设置...");
this.menuGroup3Line1 = new Menu();
this.menuGroup3Menu2 = new Menu(img[5],"声音设置...");
this.menuGroup3Menu3 = new Menu(img[6],"语言设置...");
//加入菜单组中
this.menuGroup3.Add(this.menuGroup3Menu1);
this.menuGroup3.Add(this.menuGroup3Line1);
this.menuGroup3.Add(this.menuGroup3Menu2);
this.menuGroup3.Add(this.menuGroup3Menu3);
this.menuGroup3Menu2.setEnabled(false);
}
public void commandAction(Command command, Displayable displayable) {
if (command == Menucommand)
{ this.menuGroup1.setVisable(!this.menuGroup1.getVisable());//按一次Menucommand打开主菜单,再一次关闭
this.menuGroup1.setFocus(true);//当打开主菜单时,把焦点设在主菜单上
this.menuGroup2.setFocus(false);//其它子菜单的焦点设为false
this.menuGroup3.setFocus(false);
if(!this.GM.instance.hasGameStore())
this.menuGroup2Menu2.setEnabled(false);
else this.menuGroup2Menu2.setEnabled(true);
this.repaint();//每按一次Menucommand,从画Screen
}
}
/////////////////////////////////////////////////////////////////////////
protected void paint(Graphics g) {
//字体
g.setFont(font);
//兰色背景
g.setColor(133,179,241);
g.fillRect(0,0,this.getWidth(),this.getHeight());
//在Screen的最中间画背景图
g.drawImage(image,(this.getWidth()-image.getWidth())/2,(this.getHeight()-image.getHeight())/2,g.TOP|g.LEFT);
if(this.menuGroup1.getVisable())//如果主菜单为可见.注意:只设了主菜单可见和不可见,其它子菜单都是可见的
{//菜单的焦点只能存在一个菜单组中,而不能共存.也就是说有一个的菜单组 焦点 为true,其它的菜单焦点都为false
menuGroup1.Display(g,font);//显示主菜单
if(this.menuGroup2.getFocus())//如果当前焦点在子菜单1上,子菜单1是打开的
{menuGroup2.Display(g,font);//显示子菜单1
}
if(this.menuGroup3.getFocus())//如果当前焦点在子菜单2上
{menuGroup3.Display(g,font);//显示子菜单2
}
}
}
////////////////////////////////////////////////////
protected void keyPressed(int keyCode) {
switch(keyCode) {
case -5://OK键
case -10://电话拨号键
if(menuGroup1.getFocus()&&menuGroup1.getVisable())
{//如果主菜单可见且在焦点上
//设其它焦点为false
menuGroup2.setFocus(false);menuGroup3.setFocus(false);
switch(menuGroup1.getSelectIndex())//对主菜单的selectIndex做选择
{case 0:
//打开子菜单1
menuGroup2.setFocus(true);
menuGroup1.setFocus(false);
break;
case 1:
//打开子菜单2
menuGroup3.setFocus(true);
menuGroup1.setFocus(false);
break;
case 3:
//显示about对话框
Display.getDisplay(puzzleMIDlet.instance).setCurrent(puzzleMIDlet.instance.about);
this.menuGroup1.setVisable(false);
break;
case 5:
//退出程序
puzzleMIDlet.quitApp();
}
}
else if(menuGroup2.getFocus())//如果子菜单1在焦点上
{ //设其它的焦点为false
menuGroup1.setVisable(false);menuGroup2.setFocus(false);
switch(menuGroup2.getSelectIndex())//对子菜单1的selectIndex进行选择
{case 0:
Display.getDisplay(puzzleMIDlet.instance).setCurrent(this.GM=new gameBoard());
break;
case 1:
GM.loadGame();
Display.getDisplay(puzzleMIDlet.instance).setCurrent(GM);
break;
}
}
else if(menuGroup3.getFocus())//子菜单2在焦点上
{
switch(menuGroup3.getSelectIndex())
{case 0:if(this.menuGroup3Menu1.getEnabled())
{menuGroup1.setVisable(false);menuGroup3.setFocus(false);
}
break;
case 2:if(this.menuGroup3Menu1.getEnabled())
{menuGroup1.setVisable(false);menuGroup3.setFocus(false);
}
break;
case 3:if(this.menuGroup3Menu1.getEnabled())
{menuGroup1.setVisable(false);menuGroup3.setFocus(false);
}
break;
}
}
break;
case -1://UP键
//按UP键
if(this.menuGroup1.getFocus())//焦点在主菜单上
{menuGroup1.changeSelectMenu(keyCode);
}
else if(this.menuGroup2.getFocus())//焦点在子菜单1上
{menuGroup2.changeSelectMenu(keyCode);
}
else if(this.menuGroup3.getFocus())//焦点在主菜单2上
{menuGroup3.changeSelectMenu(keyCode);
}
break;
case -2://DOWN键
//按向下键
if(this.menuGroup1.getFocus())//焦点在主菜单上
{menuGroup1.changeSelectMenu(keyCode);
}
else if(this.menuGroup2.getFocus())//焦点在子菜单1上
{menuGroup2.changeSelectMenu(keyCode);
}
else if(this.menuGroup3.getFocus())//焦点在主菜单2上
{menuGroup3.changeSelectMenu(keyCode);
}
break;
case -3://左键
if(this.menuGroup2.getFocus()|this.menuGroup3.getFocus())
{//如果焦点在子菜单上,取消焦点,把焦点设在主菜单上
this.menuGroup2.setFocus(false);
this.menuGroup3.setFocus(false);
this.menuGroup1.setFocus(true);
}break;
case -4://右键
if(menuGroup1.getFocus()&&menuGroup1.getVisable())
{//如果主菜单可见且在焦点上
//设其它焦点为false
menuGroup2.setFocus(false);menuGroup3.setFocus(false);
switch(menuGroup1.getSelectIndex())//对主菜单的selectIndex做选择
{case 0:
//打开子菜单1
menuGroup2.setFocus(true);
menuGroup1.setFocus(false);
break;
case 1:
//打开子菜单2
menuGroup3.setFocus(true);
menuGroup1.setFocus(false);
break;
}
}
}
this.repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -