⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyeventtest.java

📁 此文件是关于手机游戏开发的理论
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * A MIDlet demonstrating how to read and interpret key events from a device.
 * @author Martin J. Wells
 */
public class KeyEventTest extends MIDlet implements CommandListener
{
   private MyCanvas myCanvas;
   private Command quit;

   /**
    * A custom Canvas class we use to draw a string based on a screen
    * position modified by key events.
    */
   class MyCanvas extends Canvas
   {
      private String lastKeyName = "Hit a Key"; // name of the last key they hit
      private int x = 0; // current position
      private int y = 0;

      /**
       * Overriden Canvas.paint method that draws a string at the current
       * position (x, y).
       * @param graphics The graphics context for this Canvas
       */
      protected void paint(Graphics graphics)
      {
         // draw a black rectangle the size of the screen in order to wipe
         // all previous contents
         graphics.setColor(255, 255, 255);
         graphics.fillRect(0, 0, getWidth(), getHeight());

         // draw the string (the name of the last key that was hit)
         graphics.setColor(0, 0, 0);
         graphics.drawString(lastKeyName, x, y, Graphics.LEFT | Graphics.TOP);
      }

      /**
       * Overriden Canvas method called when a key is pressed on the MID. This
       * method sets the key name string and then modifies the position if a
       * directional key was hit.
       * @param keyCode the code of the key that was pressed
       */
      protected void keyPressed(int keyCode)
      {
         if (keyCode > 0)
            lastKeyName = getKeyName(keyCode);

         switch (getGameAction(keyCode))
         {
            case UP: y--; break;
            case DOWN: y++; break;
            case RIGHT: x++; break;
            case LEFT: x--; break;
         }

         // request a repaint of the canvas
         repaint();
      }
   }

   /**
    * MIDlet constructor that creates the custom canvas (MyCanvas) and adds
    * a quit command to it.
    */
   public KeyEventTest()
   {
      // Construct a the canvas
      myCanvas = new MyCanvas();

      // we still need a way to quit
      quit = new Command("Quit", Command.EXIT, 2);
      myCanvas.addCommand(quit);
      myCanvas.setCommandListener(this);
   }

   /**
    * Called by the Application Manager when the MIDlet is starting or resuming
    * after being paused. In this example it acquires the current Display object
    * and uses it to set the Form object created in the MIDlet constructor as
    * the active Screen to display.
    * @throws MIDletStateChangeException
    */
   protected void startApp() throws MIDletStateChangeException
   {
      // upon starting up we display the canvas
      Display.getDisplay(this).setCurrent(myCanvas);
   }

   /**
    * Called by the MID's Application Manager to pause the MIDlet. A good
    * example of this is when the user receives an incoming phone call whilst
    * playing your game. When they're done the Application Manager will call
    * startApp to resume. For this example we don't need to do anything.
    */
   protected void pauseApp()
   {
   }

   /**
    * Called by the MID's Application Manager when the MIDlet is about to
    * be destroyed (removed from memory). You should take this as an opportunity
    * to clear up any resources and save the game. For this example we don't
    * need to do anything.
    * @param unconditional if false you have the option of throwing a
    * MIDletStateChangeException to abort the destruction process.
    * @throws MIDletStateChangeException
    */
   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }

   /**
    * The CommandListener interface method called when the user executes a
    * Command, in this case it can only be the quit command we created in the
    * consutructor and added to the Canvas.
    * @param command
    * @param displayable
    */
   public void commandAction(Command command, Displayable displayable)
   {
      try
      {
         if (command == quit)
         {
            destroyApp(true);
            notifyDestroyed();
         }
      }

      catch (MIDletStateChangeException me)
      {
         System.out.println(me + " caught.");
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -