MIDP中一个简单的折行文本绘制办法

博客展示了J2ME中GraphicsUtil类的代码,定义了drawWrapString方法用于在指定矩形内绘制折行文本,超出矩形范围的字符不显示。还给出了paint方法的示例,展示如何调用该绘制方法。
  在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.     }

的绘制效果如下图:
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值