GUI.java
上传用户:szhnad
上传日期:2020-12-30
资源大小:2k
文件大小:3k
源码类别:

游戏

开发平台:

Java

  1. package com.cpt;
  2. import javax.microedition.lcdui.*;
  3. import javax.microedition.lcdui.game.*;
  4. class GUI extends GameCanvas {
  5.     private Key601 midlet;
  6.     private Graphics g;
  7.     private Font font;
  8.     private int width = 0;
  9.     private int height = 0;
  10.     private Menu menu;
  11.     private String leftOption;
  12.     private String rightOption;
  13.     private String[] menuOptions;
  14.     private int currentlySelectedIndex = 0;
  15.     private boolean menuIsActive = false;
  16.     public GUI(Key601 midlet) {
  17.         super(false);
  18.         this.midlet = midlet;
  19.         font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
  20.         setFullScreenMode(true);
  21.         width = getWidth();
  22.         height = getHeight();
  23.         g = getGraphics();
  24.         leftOption = "Options";
  25.         rightOption = "Exit"; 
  26.         menuOptions = new String[] {"Play", "Option", "Highscore", "Exit"};
  27.         menu = new Menu(leftOption, rightOption, menuOptions);
  28.     }
  29.     public void start() {
  30.         clearScreen();
  31.         menu.drawInactiveMenu(this, g);
  32.     }
  33.     private int LEFT_SOFTKEY_CODE = -6; 
  34.     private int RIGHT_SOFTKEY_CODE = -7;
  35.     protected void keyPressed(int keyCode) {     
  36.         if (menuIsActive) {
  37.             if(keyCode == RIGHT_SOFTKEY_CODE) {               
  38.                 clearScreen();
  39.                 menu.drawInactiveMenu(this, g);
  40.                 menuIsActive = false;
  41.             }            
  42.             keyCode = getGameAction(keyCode);
  43.             if (keyCode == UP) {
  44.                 currentlySelectedIndex--;
  45.                 if (currentlySelectedIndex < 0) {
  46.                     currentlySelectedIndex = 0; 
  47.                 }
  48.                 clearScreen();
  49.                 menu.drawActiveMenu(this, g, currentlySelectedIndex);
  50.             } else if (keyCode == DOWN) {
  51.                 currentlySelectedIndex++;
  52.                 if (currentlySelectedIndex >= menuOptions.length) {
  53.                     currentlySelectedIndex = menuOptions.length - 1;
  54.                 }
  55.                 clearScreen();
  56.                 menu.drawActiveMenu(this, g, currentlySelectedIndex); 
  57.             } else if (keyCode == FIRE) {
  58.                 clearScreen();
  59.                 checkOptions(currentlySelectedIndex);              
  60.                 menu.drawInactiveMenu(this, g);
  61.                 menuIsActive = false;
  62.             }
  63.         } else {          
  64.             if (keyCode == LEFT_SOFTKEY_CODE) { 
  65.                 clearScreen();
  66.                 currentlySelectedIndex = 0;
  67.                 menu.drawActiveMenu(this, g, currentlySelectedIndex);
  68.                 menuIsActive = true;
  69.             } else if (keyCode == RIGHT_SOFTKEY_CODE) {
  70.                 exitGUI();
  71.             }
  72.         } 
  73.     } 
  74.     public void exitGUI() {
  75.         midlet.destroyApp(false);
  76.         midlet.notifyDestroyed();
  77.     } 
  78.     public void clearScreen() {
  79.         g.setColor(0xffffff);
  80.         g.fillRect(0, 0,width,height);
  81.         flushGraphics();
  82.     }
  83.     private void checkOptions(int index) {
  84.         switch(index){
  85.             case 0:
  86.                 System.out.println("Play");
  87.                 break;
  88.             case 1:
  89.                 System.out.println("Options");
  90.                 break;
  91.             case 2:
  92.                 System.out.println("Highscore");
  93.                 break;
  94.             case 3:
  95.                 exitGUI();               
  96.         }
  97.     }