游戏中记时的另一方法

代码如下:

/*
 * TestMidlet.java
 */

package com.ceun.timer;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class TestMidlet extends MIDlet {
    private TimerCanvas canvas;
 public TestMidlet() {
  canvas=new TimerCanvas(this);
 }

 protected void startApp() throws MIDletStateChangeException {
  Display.getDisplay(this).setCurrent(canvas);
 }

 protected void pauseApp() {
  // TODO Auto-generated method stub

 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }
 void exit(){
  try {
   destroyApp(false);
   notifyDestroyed();
  } catch (MIDletStateChangeException e) {
   e.printStackTrace();
  }
 }

}
/*
 * TimerCanvas.java
 */
package com.ceun.timer;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class TimerCanvas extends Canvas implements CommandListener, Runnable {
    private boolean isPause;
    /**
     * a screen dimension.
     */
    static int CORNER_X;

    /**
     * a screen dimension.
     */
    static int CORNER_Y;

    /**
     * a screen dimension.
     */
    static int DISP_WIDTH;

    /**
     * a screen dimension.
     */
    static int DISP_HEIGHT;

    /**
     * a font dimension.
     */
    static int FONT_HEIGHT;

    /**
     * the default font.
     */
    static Font FONT;

    /**
     * a font dimension.
     */
    //static int SCORE_WIDTH;

    /**
     * The width of the string that displays the time,
     * saved for placement of time display.
     */
    static int TIME_WIDTH;
    /**
     * How many ticks we start with.
     */
    int myInitialGameTicks = 950;

    /**
     * this is saved to determine if the time string needs
     * to be recomputed.
     */
    int myOldGameTicks = myInitialGameTicks;

    /**
     * the number of game ticks that have passed.
     */
    int myGameTicks = myOldGameTicks;

    /**
     * whether or not this has been painted once.
     */
    boolean myInitialized;

    /**
     * we save the time string to avoid recreating it
     * unnecessarily.
     */
    static String myInitialString = "1:00";
    /**
     * we save the time string to avoid recreating it
     * unnecessarily.
     */
    String myTimeString = myInitialString;
    private Command start=new Command("Start",Command.SCREEN,1);
    private Command pause=new Command("Pause",Command.SCREEN,1);
    private Command exit=new Command("Exit",Command.EXIT,5);
    private TestMidlet midlet;
 public TimerCanvas(TestMidlet midlet) {
  this.midlet=midlet;
  addCommand(start);
  addCommand(exit);
  setCommandListener(this);
  isPause=true;
 }

 protected void paint(Graphics g) {
   if(!myInitialized) {
        CORNER_X = g.getClipX();
        CORNER_Y = g.getClipY();
        DISP_WIDTH = g.getClipWidth();
        DISP_HEIGHT = g.getClipHeight();
        FONT = g.getFont();
        FONT_HEIGHT = FONT.getHeight();
        //SCORE_WIDTH = FONT.stringWidth("Score: 000");
        TIME_WIDTH = FONT.stringWidth("Time: " + myInitialString);
        myInitialized = true;
      }
      // clear the screen:
      g.setColor(0xffffff);
      g.fillRect(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT);
      g.setColor(0);
      g.drawString("Time: " + formatTime(),
        (DISP_WIDTH - TIME_WIDTH)/2,
        CORNER_Y + FONT_HEIGHT, g.TOP|g.LEFT);
 }

 public void commandAction(Command cmd, Displayable arg1) {
  if(cmd==start){
   removeCommand(start);
   addCommand(pause);
   startTimer();
  }else if(cmd==pause){
   removeCommand(pause);
   addCommand(start);
   stopTimer();
  }else if(cmd==exit){
   midlet.exit();
  }
 }

 public void run() {
  while(!isPause){
  myGameTicks--;
     // paint the display
     try {
       repaint();
     } catch(Exception e) {
   
     }
     // we do a very short pause to allow the other thread
     // to update the information about which keys are pressed:
     synchronized(this) {
       try {
  wait(1);
       } catch(Exception e) {}
     }
   }
 }
    public void startTimer(){
     isPause=false;
     new Thread(this).start();
    }
    public void stopTimer(){
     isPause=true;
    }
    /**
     * a simple utility to make the number of ticks look like a time...
     */
    public String formatTime() {
      if((myGameTicks / 16) + 1 != myOldGameTicks) {
        myTimeString = "";
        myOldGameTicks = (myGameTicks / 16) + 1;
        int smallPart = myOldGameTicks % 60;
        int bigPart = myOldGameTicks / 60;
        myTimeString += bigPart + ":";
        if(smallPart / 10 < 1) {
   myTimeString += "0";
        }
        myTimeString += smallPart;
      }
      return(myTimeString);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值