到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.javaresearch.org无关,不对该信息负责.通过http://www.javaresearch.org/article//showarticle.jsp?column=3&thread=39450访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
MIDP中一个简单的折行文本绘制办法
作者:Jagie 来源:www.javaresearch.org  发布时间:2006-02-27 15:35:36.953



正文


   在J2ME游戏中,我们通常要在一个指定的矩形区域内绘制一段文本,比如游戏介绍和指导信息等。每次都要重复写代码实在很烦,这里给出一个简单的折行文本的绘制方法,希望能省去你的重复劳动,不过如果你想实现按键翻页的话,那还需要你自己再加点代码:)
  1. import javax.microedition.lcdui.Font;
  2. import javax.microedition.lcdui.Graphics;
  3. /**
  4.  * GraphicsUtil
  5.  * 
  6.  * @author Jagie
  7.  * 
  8.  */
  9. public class GraphicsUtil {
  10.     /**
  11.      * 在制定矩形内,绘制折行文本,超出矩形范围的字符不显示
  12.      * @param src 文本字串
  13.      * @param g Graphics对象
  14.      * @param x 矩形左上角x坐标
  15.      * @param y 矩形左上角y坐标
  16.      * @param w 矩形宽度
  17.      * @param h 矩形高度
  18.      * @param leftMargin 左边矩
  19.      * @param topMargin 上边距
  20.      * @param isVTight    行间距是否紧凑
  21.      * @param fontColor 文字颜色
  22.      * @param font 绘制所用字体
  23.      */
  24.     public static final void drawWrapString(String src, Graphics g, int x, int y, int w,
  25.             int h, int leftMargin,int topMargin, boolean isVTight, int fontColor, Font font) {
  26.         g.setFont(font);
  27.         g.setColor(fontColor);
  28.         int count = src.length();
  29.         int curCharIndex = 0;
  30.         int curX = x+leftMargin;
  31.         int curY = y+topMargin;
  32.         int vDelta=font.getHeight();
  33.         if(isVTight){
  34.             vDelta=font.getBaselinePosition();
  35.         }
  36.         while (curCharIndex < count) {
  37.             char c = src.charAt(curCharIndex);
  38.             if (c == '\n') {
  39.                 curX = x+leftMargin;
  40.                 curY += vDelta;
  41.                 curCharIndex++;
  42.                 continue;
  43.             } else if (c == '\t') {
  44.                 // 相当于2个空格
  45.                 curX += font.charWidth(' ')*2;
  46.                 curCharIndex++;
  47.                 continue;
  48.             }
  49.             if (curX + font.charWidth(c) < x + w) {
  50.                 g.drawChar(c, curX, curY, Graphics.LEFT | Graphics.TOP);
  51.                 curCharIndex++;
  52.                 curX += font.charWidth(c);
  53.                 
  54.             } else {
  55.                 //考虑换行
  56.                 if(curY+2*font.getBaselinePosition()<y+h){
  57.                     curY += vDelta;
  58.                     curX = x+leftMargin;
  59.                     g.drawChar(c, curX, curY, Graphics.LEFT | Graphics.TOP);
  60.                     curCharIndex++;
  61.                     curX += font.charWidth(c);
  62.                 }else{
  63.                     break;
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }

比如一下代码:
  1. public void paint(Graphics g) {
  2.         g.setColor(0);
  3.         g.fillRect(0, 0, w, h);
  4.         g.setColor(0x00ff00);
  5.         Font f= Font.getFont(
  6.                 Font.FACE_SYSTEM, Font.STYLE_PLAIN,
  7.                 Font.SIZE_SMALL);
  8.         g.setFont(f);
  9.         g.drawRect(10,25,w-20,100);
  10.         GraphicsUtil
  11.                 .drawWrapString(
  12.                         "     大多数的人在作决定时都只考虑眼前而不顾未来,结果快乐没得到却得到痛苦,事实上人世间一切有意义的事若想成功,那就必须忍受一时的痛苦。你必须熬过眼前的恐惧和引诱,按照自己的价值观或标准面把目光放在未来。你要记住,任何事都不会使我们痛苦,而真正使我们痛苦的是对于痛苦的恐惧,同样的道理,也没有任何事会使我们快乐,而真正能使 我们快乐的是对于快乐的把握。",
  13.                         g,10, 25, w-20, 100,6,6, false,0xffffff,f);
  14.     }

的绘制效果如下图:

 

作者简介


陈万飞,网名Jagie,培训师,爱好java技术.可通过chen_cwf@163.com与他联系 

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读