当前页面: 开发资料首页 → J2ME 专题 → 游戏开发小技巧—低级界面下的文本自动换行
摘要: 游戏开发小技巧—低级界面下的文本自动换行
final int CharacterNumber = 6;
public Vector getSubsection(String str){
Vector vector = new Vector();
int i=0;
while(!str.equals(""){
if(str.length>6){
vector.addElement(str.substring(0,CharacterNumber));
str = str.substring(CharacterNumber);
}
else{
vector.addElement(str);
str = "";
}
}
return vector;
}
Vector vector = getSubsection(strGamehelp);
for(int i=0;ig.drawString((String)vector.elementAt(i),5,5+20*i,Graphics.TOP|Graphics.LEFT);
}
public Vector getSubsection(String strSource,Font font,int width,String strSplit){
Vector vector = new Vector();
String temp=strSource;
int i,j;
int LastLength = 1;
int step = 0;
try{
while (!temp.equals("")) {
i=temp.indexOf("\n");
if(i>0){
if(font.stringWidth(temp.substring(0,i-1)) >= width){
i = -1;
}
}
if(i==-1){
if(LastLength>temp.length()){
i = temp.length();
}else{
i = LastLength;
step = font.stringWidth(temp.substring(0, i)) > width ? -1 : 1;
if(i while (! (font.stringWidth(temp.substring(0, i)) <= width
&& font.stringWidth(temp.substring(0, i + 1)) > width)) {
i = i + step;
if (i == temp.length())
break;
}
}
}
if(!strSplit.equals("")){
j = i;
if (i < temp.length()) {
while (strSplit.indexOf(temp.substring(i-1,i))==-1) {
i--;
if (i == 0) {
i = j;
break;
}
}
}
}
}
LastLength = i;
vector.addElement(temp.substring(0, i));
if (i == temp.length()) {
temp = "";
}
else{
temp = temp.substring(i);
if (temp.substring(0, 1).equals("\n")) {
temp = temp.substring(1);
}
}
}
}catch(Exception e)
{
System.out.println("getSubsection:"+e);
}
return vector;
}Font font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN, Font.SIZE_SMALL);
g.setFont(font);
Vector vector = getSubsection(strGamehelp,font,getWidth()-10," ,.?!");