LPFormatCHNString.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:

J2ME

开发平台:

Java

  1. /**
  2.  * <p>Title: lipeng</p>
  3.  * <p>Description:
  4.  * You cannot remove this copyright and notice.
  5.  * You cannot use this file without the express permission of the author.
  6.  * All Rights Reserved</p>
  7.  * <p>Copyright: lizhenpeng (c) 2004</p>
  8.  * <p>Company: LP&P</p>
  9.  * @author lizhenpeng
  10.  * @version 1.0.0
  11.  * <p>
  12.  * Revise History
  13.  * </p>
  14.  */
  15. package lipeng;
  16. import javax.microedition.lcdui.*;
  17. public class LPFormatCHNString
  18. {
  19.   public LPFormatCHNString(String str,int width,int height,Font font)
  20.   {
  21.     helpStr=str;
  22.     strLen = str.length();
  23.     beginLine=0;
  24.     this.font=font;
  25.     String benchStr="鹏";
  26.     char[] bufferStr=benchStr.toCharArray();
  27.     int charWidth=font.charsWidth(bufferStr,0,bufferStr.length);
  28.     lineStrLen=width/charWidth;
  29.     strHeightNum=height/font.getHeight();
  30.     endLine=strHeightNum;
  31.     maxLine =  strLen/lineStrLen + (strLen%lineStrLen==0? 0:1);
  32.   }
  33.   public void paint(Graphics g)
  34.   {
  35.     int i;
  36.     for(i=beginLine;i<endLine;++i)
  37.     {
  38.       if((i+1)*lineStrLen<=strLen)
  39.       {
  40.         g.drawString(helpStr.substring(i*lineStrLen,(i+1)*lineStrLen),0,
  41.                      font.getHeight()*(i-beginLine),g.LEFT|g.TOP);
  42.       }
  43.       else
  44.       {
  45.         g.drawString(helpStr.substring(i*lineStrLen,strLen),0,
  46.                      font.getHeight()*(i-beginLine),g.LEFT|g.TOP);
  47.       }
  48.     }
  49.   }
  50.   public void action()
  51.   {
  52.     if((allAction&LPKeyMask.MASK_KEY_UP_FLAG)!=0)
  53.     {
  54.       allAction &= ~LPKeyMask.MASK_KEY_UP_FLAG;
  55.       if(beginLine != 0)
  56.       {
  57.         --beginLine;
  58.         --endLine;
  59.       }
  60.     }
  61.     else if((allAction&LPKeyMask.MASK_KEY_DOWN_FLAG)!=0)
  62.     {
  63.       allAction &= ~LPKeyMask.MASK_KEY_DOWN_FLAG;
  64.       if(endLine != maxLine)
  65.       {
  66.         ++beginLine;
  67.         ++endLine;
  68.       }
  69.     }
  70.   }
  71.   public void judgeKeyCode(int keyCode,int gameKeyCode)
  72.   {
  73.     switch(gameKeyCode)
  74.     {
  75.       case Canvas.UP:
  76.         if((((allAction&LPKeyMask.MASK_KEY_UP)==0)
  77.             &&((allAction&LPKeyMask.MASK_KEY_UP_FLAG)==0)))
  78.         {
  79.           allAction|=LPKeyMask.MASK_KEY_UP_FLAG|LPKeyMask.MASK_KEY_UP;
  80.         }
  81.         break;
  82.       case Canvas.DOWN:
  83.         if((((allAction&LPKeyMask.MASK_KEY_DOWN)==0)
  84.             &&((allAction&LPKeyMask.MASK_KEY_DOWN_FLAG)==0)))
  85.         {
  86.           allAction|=LPKeyMask.MASK_KEY_DOWN_FLAG|LPKeyMask.MASK_KEY_DOWN;
  87.         }
  88.         break;
  89.     }
  90.   }
  91.   public void reInit()
  92.   {
  93.     allAction = 0;
  94.     beginLine = 0;
  95.     endLine = strHeightNum;
  96.   }
  97.   public void freeKey(int keyCode,int gameKeyCode)
  98.   {
  99.     switch(gameKeyCode)
  100.     {
  101.       case Canvas.UP:
  102.         allAction&=~LPKeyMask.MASK_KEY_UP;
  103.         break;
  104.       case Canvas.DOWN:
  105.         allAction&=~LPKeyMask.MASK_KEY_DOWN;
  106.         break;
  107.     }
  108.   }
  109.   private int beginLine;
  110.   private int endLine;
  111.   private int allAction;
  112.   private int lineStrLen;
  113.   private int strHeightNum;
  114.   private String helpStr;
  115.   private int strLen;
  116.   private Font font;
  117.   private int maxLine;
  118. }