"Problem description"
The drawstring in the Javame graphics class does not support text wrapping, so when you draw a longer string, the text is drawn on the same line, and the string that exceeds the screen portion is truncated. How do I make text that is drawn automatically wrap?
Analysis
DrawString cannot implement wrapping, but it can be positioned to draw text. Therefore, you can consider splitting the text into multiple substrings and then drawing the strings. The split strategy is as follows:
1 The line break is encountered, split;
2 Split if the string length is greater than the set length (typically the width of the screen).
Steps
1 defines a string and string [] object;
2 Implementation of string wrap-line Split function
Stringdealmethod.java
Package com.token.util;
Import Java.util.Vector;
Import Javax.microedition.lcdui.Font; public class Stringdealmethod {public Stringdealmethod () {}//String cut to implement string wrap public static Stri
ng[] Format (String text, int maxwidth, Font ft) {string[] result = NULL;
Vector tempr = new vector ();
int lines = 0;
int len = Text.length ();
int index0 = 0;
int index1 = 0;
Boolean wrap;
while (true) {int widthes = 0;
Wrap = false;
for (index0 = index1; index1 < Len; index1++) {if (text.charat) = = ' \ n ') {index1;
Wrap = true;
Break
} widthes = Ft.charwidth (Text.charat (index1)) + widthes;
if (Widthes > MaxWidth) {break;
}} lines++;
if (wrap) {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 ... .... ...") is a very, very.
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<nodes.size (); loop++) {Result[loop] = (String) Nodes.elem
Entat (loop);
System.out.println (Result[loop]);
} return result;
}
}
3 calling the Split function to implement the split of the string
int width = getwidth ();
Font ft = Font.getfont (font.face_proportional,font.style_bold,font.size_large);
info = "Welcome to use!" \ n "
+" 1 MVC test; \ n "
+" 2 Automatic line-wrapping test to draw a string that automatically recognizes line wrapping. \ n ";
Info_wrap = Stringdealmethod.format (info, width-10, ft);
4 Drawing strings
int width = getwidth ();
Font ft = Font.getfont (font.face_proportional,font.style_bold,font.size_large);
info = "Welcome to use!" \ n "
+" 1 MVC test; \ n "
+" 2 Automatic line-wrapping test to draw a string that automatically recognizes line wrapping. \ n ";
Info_wrap = Stringdealmethod.format (info, width-10, ft);
The effect drawn is as shown in Figure 1: