GUI.java
上传用户:szhnad
上传日期:2020-12-30
资源大小:2k
文件大小:3k
- package com.cpt;
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- class GUI extends GameCanvas {
- private Key601 midlet;
- private Graphics g;
- private Font font;
- private int width = 0;
- private int height = 0;
- private Menu menu;
- private String leftOption;
- private String rightOption;
- private String[] menuOptions;
- private int currentlySelectedIndex = 0;
- private boolean menuIsActive = false;
- public GUI(Key601 midlet) {
- super(false);
- this.midlet = midlet;
- font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
- setFullScreenMode(true);
- width = getWidth();
- height = getHeight();
- g = getGraphics();
- leftOption = "Options";
- rightOption = "Exit";
- menuOptions = new String[] {"Play", "Option", "Highscore", "Exit"};
- menu = new Menu(leftOption, rightOption, menuOptions);
- }
- public void start() {
- clearScreen();
- menu.drawInactiveMenu(this, g);
- }
- private int LEFT_SOFTKEY_CODE = -6;
- private int RIGHT_SOFTKEY_CODE = -7;
- protected void keyPressed(int keyCode) {
- if (menuIsActive) {
- if(keyCode == RIGHT_SOFTKEY_CODE) {
- clearScreen();
- menu.drawInactiveMenu(this, g);
- menuIsActive = false;
- }
- keyCode = getGameAction(keyCode);
- if (keyCode == UP) {
- currentlySelectedIndex--;
- if (currentlySelectedIndex < 0) {
- currentlySelectedIndex = 0;
- }
- clearScreen();
- menu.drawActiveMenu(this, g, currentlySelectedIndex);
- } else if (keyCode == DOWN) {
- currentlySelectedIndex++;
- if (currentlySelectedIndex >= menuOptions.length) {
- currentlySelectedIndex = menuOptions.length - 1;
- }
- clearScreen();
- menu.drawActiveMenu(this, g, currentlySelectedIndex);
- } else if (keyCode == FIRE) {
- clearScreen();
- checkOptions(currentlySelectedIndex);
- menu.drawInactiveMenu(this, g);
- menuIsActive = false;
- }
- } else {
- if (keyCode == LEFT_SOFTKEY_CODE) {
- clearScreen();
- currentlySelectedIndex = 0;
- menu.drawActiveMenu(this, g, currentlySelectedIndex);
- menuIsActive = true;
- } else if (keyCode == RIGHT_SOFTKEY_CODE) {
- exitGUI();
- }
- }
- }
- public void exitGUI() {
- midlet.destroyApp(false);
- midlet.notifyDestroyed();
- }
- public void clearScreen() {
- g.setColor(0xffffff);
- g.fillRect(0, 0,width,height);
- flushGraphics();
- }
- private void checkOptions(int index) {
- switch(index){
- case 0:
- System.out.println("Play");
- break;
- case 1:
- System.out.println("Options");
- break;
- case 2:
- System.out.println("Highscore");
- break;
- case 3:
- exitGUI();
- }
- }
- }