Menu.java
上传用户:sh2222
上传日期:2009-12-31
资源大小:84k
文件大小:12k
源码类别:

J2ME

开发平台:

Java

  1. package paopao;
  2. import javax.microedition.lcdui.*;
  3. import com.nokia.mid.ui.*;
  4. public class Menu extends FullCanvas implements Runnable
  5. {
  6. int state;
  7. //主菜单
  8. final int screenw = getWidth();
  9. final int screenh = getHeight();
  10. final int paintw  = 168;
  11. final int painth  = 182;
  12. final int midx      = paintw / 2;
  13. final int midy      = painth / 2;
  14. final int orgx = (screenw - paintw) / 2;
  15. final int orgy = (screenh - painth) / 2;
  16. final int MENU_INIT       = 0;
  17. final int MENU_SELECT     = 1000;
  18. final int MENU_LOADING    = 2000;
  19. final int MENU_SETKEY     = 3000;
  20. final int MENU_HELP_RANK  = 4000;
  21. final int MENU_HELP       = 5000;
  22. final int MENU_RANK       = 6000;
  23. Image[] img = new Image[8];
  24. final int helpStrHeight=-150;
  25. //主选择菜单
  26. int fingerCount;
  27. int selectNum;
  28. //this is loading
  29. int progress;
  30. //this is setkey
  31. boolean isSettingKey = false;
  32. int nowKey;
  33. //this is help and rank
  34. boolean isHelp = true;
  35. //this is help
  36. int helpStrY;                         //帮助的起始y坐标
  37. final int minHelpTop = -300;         //帮助的y坐标上限
  38. int heightPerLine = 20;
  39. //this is rank
  40. //this is thread
  41. Thread thread = null;
  42. int interval = 50;
  43. boolean killThread = false;
  44. int pressedKeyCode = 10000;
  45. static final String DIR = "/resource/";
  46. public Menu()
  47.   {
  48. Resource.initrecord();
  49. Resource.init(16);
  50. state = MENU_INIT;
  51.   }
  52. protected void keyPressed(int KeyCode)
  53.   {
  54. pressedKeyCode = KeyCode;
  55. System.out.println("" + KeyCode);
  56.   }
  57. protected void doKeyEvent(int keyCode)
  58.   {
  59.     if(keyCode == 10000)
  60.       return;
  61.     else
  62.     switch(state)
  63.     {
  64.     case MENU_INIT:
  65.       return;
  66.     case MENU_SELECT:
  67.     if(keyCode == Resource.KEY_UP)
  68.       {
  69.       selectNum --;
  70.       selectNum = (selectNum < 0 ? 3 : selectNum);
  71.       }
  72.     else if(keyCode == Resource.KEY_DOWN)
  73.          {
  74.       selectNum ++;
  75.       selectNum %= 4;
  76.           }
  77.          else if(keyCode == Resource.KEY_FIRE)
  78.          {
  79.       switch(selectNum)
  80.           {
  81.        case 0:
  82.         state = MENU_LOADING;
  83.          break;
  84.        case 1:
  85.         state = MENU_SETKEY;
  86.          break;
  87.        case 2:
  88.         state = MENU_HELP_RANK;
  89.         break;
  90.        case 3:
  91.        PaoPao.quitApp();
  92.         break;
  93.           }
  94.          }
  95.      break;
  96.     case MENU_LOADING:
  97.       break;
  98.     case MENU_SETKEY:
  99.       if(!isSettingKey)
  100.              {
  101.                if (keyCode == Resource.KEY_UP)
  102.                {
  103.                nowKey ++;
  104.               nowKey = (nowKey > Resource.setupKeyNum ? 0 : nowKey);
  105.                }
  106.               if (keyCode == Resource.KEY_DOWN)
  107.                {
  108.                 nowKey --;
  109.                nowKey = (nowKey < 0 ? Resource.setupKeyNum : nowKey);
  110.                }
  111.                 if(nowKey > 0 && keyCode==Resource.KEY_LS)
  112.             {
  113.               isSettingKey = true;
  114.               Resource.key[nowKey - 1]=0;
  115.             }
  116.             if(nowKey == 0 && keyCode == Resource.KEY_LS)
  117.               Resource.isSoundOn=!Resource.isSoundOn;
  118.             if(keyCode==Resource.KEY_RS)
  119.             {
  120.               this.serviceRepaints();
  121.               state = MENU_HELP_RANK;
  122.               repaint();
  123.               Resource.saveOption();
  124.             }
  125.              }
  126.              else
  127.              {
  128.                if(Resource.GetUserKeyName(keyCode)!=" ")
  129.                {
  130.                  Resource.key[nowKey - 1] = (byte)keyCode;
  131.                  isSettingKey = false;
  132.                }
  133.              }
  134.              break;
  135.     case MENU_HELP_RANK:
  136. //选择进入帮助或者排行榜
  137. if(keyCode == Resource.KEY_DOWN || keyCode == Resource.KEY_UP)
  138. //选择好
  139. isHelp = !isHelp;
  140. else if(keyCode == Resource.KEY_LS || keyCode == Resource.KEY_FIRE)
  141.       {
  142.         if(isHelp)
  143.           state = MENU_HELP;
  144.         else
  145.           state = MENU_RANK;
  146.       }
  147. else if(keyCode == Resource.KEY_RS)
  148.         state = MENU_SELECT;
  149. break;
  150. case MENU_HELP:
  151.       if(keyCode==Resource.KEY_UP)
  152.               {
  153.                 helpStrY += 100;
  154.                 if (helpStrY > 0)
  155.                   helpStrY = 0;
  156.               }
  157.       else if(keyCode==Resource.KEY_DOWN)
  158.               {
  159.                 helpStrY -= 100;
  160.                 if(helpStrY<helpStrHeight)helpStrY=helpStrHeight;
  161.               }
  162.       else if(keyCode==Resource.KEY_RS)
  163.               {
  164.                 this.serviceRepaints();
  165.                 state = MENU_HELP_RANK;
  166.                 repaint();
  167.               }
  168.               break;
  169. case MENU_RANK:
  170. if(keyCode == Resource.KEY_RIGHT || keyCode == Resource.KEY_FIRE)
  171.  state = MENU_HELP_RANK;
  172. break;
  173.     }
  174. pressedKeyCode = 10000;
  175.   }
  176. protected void show()
  177.   {
  178.   if(thread == null)
  179.    {
  180.   thread = new Thread(this);
  181.   thread.start();
  182.    }
  183.   }
  184. protected void paint(Graphics g)
  185.   {
  186. g.setColor(0,0,0);
  187. g.fillRect(0,0,screenw,screenh);
  188. g.translate(orgx,orgy);
  189. g.setClip(0,0,paintw,painth);
  190. g.fillRect(0, 0, paintw, painth);
  191.     switch(state)
  192.        {
  193.        case MENU_INIT:
  194.        g.setColor(0,0,0);
  195.        g.fillRect(0,0,paintw,painth);
  196.        break;
  197.        case MENU_SELECT:
  198.          g.setColor(0,0,0);
  199.          g.fillRect(0, 0, paintw, painth);
  200.        g.drawImage(img[0],midx,0,Graphics.TOP|Graphics.HCENTER);
  201.        g.drawImage(img[1],midx,130,Graphics.VCENTER|Graphics.HCENTER);
  202.        g.drawImage(img[2 + fingerCount],18,103 + selectNum * 18,Graphics.VCENTER|Graphics.HCENTER);
  203.        break;
  204.        case MENU_LOADING:
  205.        g.drawImage(img[6],midx,15,Graphics.TOP|Graphics.HCENTER);
  206.        g.drawImage(img[7],midx,150,Graphics.TOP|Graphics.HCENTER);
  207.        g.setColor(162,28,50);
  208.        g.fillRect(45,151,progress * 7,4);
  209.        break;
  210.       case MENU_SETKEY:
  211.         g.setColor(255,255,255);
  212.          g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,
  213.                                 Font.SIZE_SMALL));
  214.          g.drawString("音效:"+(Resource.isSoundOn?"开":"关"), 10, 10, g.TOP | g.LEFT);
  215.          for(int i=0;i<Resource.setupKeyNum;i++)
  216.          g.drawString(Resource.keyDes[i]+Resource.GetUserKeyName(Resource.key[i]),10,25+i*15,20);
  217.          g.drawRect(7,9+15*nowKey,paintw-14,14);
  218.          g.setColor(248,248,53);
  219.          if(!isSettingKey)
  220.          {
  221.            g.drawString("请按左软件修改设置", paintw / 2, 90, g.TOP | g.HCENTER);
  222.            g.drawImage(img[4],paintw - 5,painth - 5, g.RIGHT | g.BOTTOM );
  223.          }
  224.          else
  225.            g.drawString("请按下要设置的按键",paintw / 2,90,g.TOP | g.HCENTER );
  226.          break;
  227.        case MENU_HELP_RANK:
  228.          drawFrame(g);
  229.          g.setColor(175,165,52);
  230.          if(isHelp)
  231.          g.fillRect(0,10,paintw,15);
  232.          else
  233.          g.fillRect(0,25,paintw,15);
  234.         g.setColor(255,255,255);
  235.         g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,
  236.                        Font.SIZE_SMALL));
  237.         g.drawString("帮助信息", 10, 10, g.TOP | g.LEFT);
  238.         g.drawString("本地排行", 10, 25, g.TOP | g.LEFT);
  239.        break;
  240.        case MENU_HELP:
  241.          drawFrame(g);
  242.                  g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,
  243.                                         Font.SIZE_SMALL));
  244.                  g.setClip(5, 5, paintw-10, painth-10);
  245.                  g.setColor(255,255,255);
  246.                  drawHelpString(g, Resource.helpStr, 8,helpStrY+6);
  247.                  g.setClip(0,0,paintw,painth);
  248.                  //g.drawImage(img[4],paintw-5,painth-5,g.RIGHT  |g.BOTTOM );
  249.        break;
  250.        case MENU_RANK:
  251.          drawFrame(g);
  252.                  g.setColor(255,255,255);
  253.                  g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,
  254.                                 Font.SIZE_SMALL));
  255.                  g.drawString("本地分数排行",paintw / 2,10,g.HCENTER |g.TOP  );
  256.                  for(int i=0;i<5;i++)
  257.                  {
  258.                    g.drawString((i + 1)+".",10,30 + i*15,g.TOP | g.LEFT );
  259.                  }
  260.                //  g.drawImage(img[4],paintw - 5,painth - 5,g.RIGHT | g.BOTTOM );
  261.        break;
  262.        default :
  263.        break;
  264.        }
  265.   }
  266. public void run()
  267.   {
  268.  /*
  269. while(!killThread)
  270.     {
  271.       switch(state)
  272.        {
  273.     case MENU_INIT:
  274.            try
  275.        {
  276.        img[0] = Image.createImage(Resource.DIR + "logo.png");
  277.        img[1] = Image.createImage(Resource.DIR + "select.png");
  278.        img[2] = Image.createImage(Resource.DIR + "finger0.png");
  279.        img[3] = Image.createImage(Resource.DIR + "finger1.png");
  280.        img[4] = Image.createImage(Resource.DIR + "finger2.png");
  281.        img[5] = Image.createImage(Resource.DIR + "finger3.png");
  282.        img[6] = Image.createImage(Resource.DIR + "boy.png");
  283.        img[7] = Image.createImage(Resource.DIR + "progress.png");
  284.      }
  285.      catch(Exception ex)
  286.      {
  287.      System.out.println("Error int create new img!");
  288.      }
  289.      state = MENU_SELECT;
  290.            break;
  291.        case MENU_SELECT:
  292.        //this is for lover circle act
  293.         fingerCount ++;
  294.         fingerCount %= 4;
  295.         break;
  296.         case MENU_LOADING:
  297.          Resource.setImage(Resource.PLAYER,Resource.DIR + "player.png");
  298.          progress += 1;
  299.          repaint();
  300.          Resource.setImage(Resource.BOARD,Resource.DIR + "board.png");
  301.          progress += 2;
  302.          repaint();
  303.          Resource.setImage(Resource.BG,Resource.DIR + "bg.png");
  304.          Resource.setImage(Resource.PH,Resource.DIR + "ph.png");
  305.          Resource.setImage(Resource.WALL_LEFT,Resource.DIR + "wall_left.png");
  306.          progress += 2;
  307.          repaint();
  308.          Resource.setImage(Resource.TOOTH,Resource.DIR + "tooth.png");
  309.          Resource.setImage(Resource.LOGO,Resource.DIR + "logo.png");
  310.          progress += 2;
  311.          repaint();
  312.          Resource.setImage(Resource.SELECT,Resource.DIR + "select.png");
  313.          Resource.setImage(Resource.FINGER0,Resource.DIR + "finger0.png");
  314.          Resource.setImage(Resource.FINGER1,Resource.DIR + "finger1.png");
  315.          Resource.setImage(Resource.FINGER2,Resource.DIR + "finger2.png");
  316.          Resource.setImage(Resource.FINGER3,Resource.DIR + "finger3.png");
  317.          progress += 1;
  318.          repaint();
  319.          Resource.setImage(Resource.BOY,Resource.DIR + "boy.png");
  320.          Resource.setImage(Resource.PROGRESS,Resource.DIR + "progress.png");
  321.          Resource.setImage(Resource.WALL_RIGHT,Resource.DIR + "wall_right.png");
  322.          progress += 2;
  323.          repaint();
  324.          Resource.setImage(Resource.OVER,Resource.DIR + "over.png");
  325.          progress += 1;
  326.          repaint();
  327.          killThread = true;
  328.         break;
  329.        case MENU_SETKEY:
  330.          break;
  331.        case MENU_HELP_RANK:
  332.          break;
  333.        case MENU_HELP:
  334.          break;
  335.        case MENU_RANK:
  336.          break;
  337.        }                      //switch(state)
  338.      try
  339.      {
  340.      this.doKeyEvent(pressedKeyCode);
  341.      repaint();
  342.      thread.sleep(interval);
  343.      }
  344.      catch(Exception ex){}
  345.            }                        //while(!killThread)
  346. serviceRepaints();
  347. img = null;
  348. System.gc();
  349. PaoPao.shiftCanvas(PaoPao.GAMEWORLD_ID,6);
  350. */
  351.      }                           //run
  352.      void drawFrame(Graphics g)
  353.        {
  354.          g.setColor(46,49,146);
  355.          g.fillRect(2,2,paintw-4,painth-4);
  356.          g.setColor(128,128,128);
  357.          g.drawLine(2,2,2,painth-2);
  358.          g.drawLine(2,4,paintw-2,4);
  359.          g.drawLine(2+paintw-7,2,2+paintw-7,painth-2);
  360.          g.drawLine(2,4+painth-6,paintw-2,4+painth-6);
  361.          g.setColor(184,184,184);
  362.          g.drawLine(3,2,3,painth-2);
  363.          g.drawLine(2,3,paintw-2,3);
  364.          g.drawLine(3+paintw-7,2,3+paintw-7,painth-2);
  365.          g.drawLine(2,3+painth-6,paintw-2,3+painth-6);
  366.          g.setColor(248,248,248);
  367.          g.drawLine(4,2,4,painth-2);
  368.          g.drawLine(2,2,paintw-2,2);
  369.          g.drawLine(4+paintw-7,2,4+paintw-7,painth-2);
  370.          g.drawLine(2,2+painth-6,paintw-2,2+painth-6);
  371.          //g.drawImage(img[1],3,3,g.HCENTER|g.VCENTER );
  372.         // g.drawImage(img[1],3,painth-3,g.HCENTER|g.VCENTER );
  373.         // g.drawImage(img[1],paintw-3,3,g.HCENTER|g.VCENTER );
  374.          //g.drawImage(img[1],paintw-3,painth-3,g.HCENTER|g.VCENTER );
  375.        }
  376.        public static void drawHelpString(Graphics g,String str,int x,int y)
  377.          {
  378.            System.gc();
  379.            StringBuffer output = new StringBuffer(30);
  380.            int j=0;
  381.            for(int i=0;i<str.length();i++)
  382.            {
  383.              if(str.charAt(i)!='&')
  384.              {
  385.                 output.append(str.charAt(i));
  386.              }
  387.              else
  388.              {
  389.                if(y+12*j>g.getClipY()-20 && y+12*j<g.getClipY()+g.getClipHeight())
  390.                 g.drawString(output.toString(),x,y+12*j,g.TOP|g.LEFT );
  391.                 j++;
  392.                 output.delete(0,29);
  393.              }
  394.            }
  395.          }
  396. }