JavaMe连载(4)-绘制可自动换行文本 (二)

2014-11-24 07:56:20 · 作者: · 浏览: 1
tempR.addElement(text.substring(index0, index1 - 1));
} else {
tempR.addElement(text.substring(index0, index1));
}
if (index1 >= len) {
break;
}
}
result = new String[lines];
tempR.copyInto(result);
return result;
}

public static String[] split(String original, String separator) {
Vector nodes = new Vector();
//System.out.println("split start...................");
//Parse nodes into vector
int index = original.indexOf(separator);
while(index>=0) {
nodes.addElement( original.substring(0, index) );
original = original.substring(index+separator.length());
index = original.indexOf(separator);
}
// Get the last node
nodes.addElement( original );

// Create splitted string array
String[] result = new String[ nodes.size() ];
if( nodes.size()>0 ) {
for(int loop=0; loop {
result[loop] = (String)nodes.elementAt(loop);
//System.out.println(result[loop]);
}

}

return result;
}
}

3 调用拆分函数,实现字符串的拆分

[html] int width = getWidth();

Font ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);

info = "欢迎使用!\n"
+"1 MVC测试;\n"
+"2 自动换行测试,绘制可自动识别换行的字符串。\n";
info_wrap = StringDealMethod.format(info, width-10, ft);
int width = getWidth();

Font ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);

info = "欢迎使用!\n"
+"1 MVC测试;\n"
+"2 自动换行测试,绘制可自动识别换行的字符串。\n";
info_wrap = StringDealMethod.format(info, width-10, ft);

4 绘制字符串

[html] graphics.setColor(Color.text);
graphics.setFont(ft);
for(int i=0; i {
graphics.drawString(info_wrap[i], 5, i * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);
}
graphics.setColor(Color.text);
graphics.setFont(ft);
for(int i=0; i {
graphics.drawString(info_wrap[i], 5, i * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);
}

绘制的效果如图1所示:

\

图1 自动换行字符串绘制效果


摘自 tandesir的专栏