【问题标题】:Wrap text on Canvas in J2ME [closed]在 J2ME 中的 Canvas 上换行 [关闭]
【发布时间】:2010-08-06 04:50:50
【问题描述】:

我要开发 j2me 应用程序。我想知道,如何根据 J2ME 中的屏幕宽度大小在画布上包装文本。

【问题讨论】:

  • 请更具体地回答您的问题。你想实现一个可以签名的特定功能吗?

标签: text java-me midp lcdui


【解决方案1】:

这是我在应用程序中使用的代码,它将包裹线矢量,您可以在画布中的任何 X 点进行绘制。

public static Vector wrapToLines(String text, Font f, int maxWidth) {
        Vector lines = new Vector ();
        boolean paragraphFormat = false;
        if (text == null) {
            return lines;
        }
        if (f.stringWidth(text) < maxWidth) {
            lines.add(text);
            return lines;
        } else {

            char chars[] = text.toCharArray();
            int len = chars.length;
            int count = 0;
            int charWidth = 0;
            int curLinePosStart = 0;
            while (count < len) {
                if ((charWidth += f.charWidth(chars[count])) > (maxWidth - 4) || count == len - 1) // wrap to next line
                {
                    if (count == len - 1) {
                        count++;
                    }
                    String line = new String(chars, curLinePosStart, count - curLinePosStart);
                    if (paragraphFormat) {
                        int lastSpacePosition = line.lastIndexOf(" ");
                        String l = new String(chars, curLinePosStart, (lastSpacePosition != -1) ? lastSpacePosition + 1 : (count == len - 1) ? count - curLinePosStart + 1 : count - curLinePosStart);
                        lines.add(l);
                        curLinePosStart = (lastSpacePosition != -1) ? lastSpacePosition + 1 : count;
                    } else {
                        lines.add(line);
                        curLinePosStart = count;
                    }
                    charWidth = 0;
                }
                count++;
            }
            return lines;

        }
    }

while 只是在 for 循环中运行

int y=0;
int linespacing=4;
  for(int i=0;i<lines.size();i++)
  {
     g.drawString((String)lines.get(i),10,y,0);
     y+=(i!=lines.size()-1)?(font.getHeight()+linespacing):0;
   }

享受:)

【讨论】:

    【解决方案2】:

    需要自己计算要绘制的字符串的宽度并开始换行 (拆分字符串)每次达到画布的最大宽度时。

    void paint(Graphics _g) {
      String t = "text to draw";
      int px_consumed = _g.getFont().substringWidth(t, 0, t.length())}
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      相关资源
      最近更新 更多