How to easily implement the natural branch display of various text in the low-level interface of J2ME

Source: Internet
Author: User
Tags implement
Show the time to do the game, especially in a number of dialogue more cases, such as RPG games, often encounter the word break display problem
How to do no matter how the text can be better to display, there is a function of the text can be branch, and then you can print a line of

The fourth parameter is mainly set for the language of a word such as English, because like English you can't divide a word into two lines, you can pass in some sign of participle, such as space, punctuation, etc., such as ",.?!"
Chinese doesn't matter
But I am not very clear in Japanese, I do not know how it is participle, is the same as Chinese, or the same as English

/** @todo A branch of text that is specific to a font
* @author Efei
* @param strsource String for Branch
* @param font used by font font
* @param width int branch needs to meet the widths
* @param strsplit String to judge characters, such as spaces and some punctuation marks. Chinese does not need to break words, then the empty string
* @return Vector
*/
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;
Get the tipping point
if (I<temp.length ()) {
while (! (Font.stringwidth (temp.substring (0, i)) <= width
&& font.stringwidth (temp.substring (0, i + 1)) > width) {
i = i + step;
if (i = = Temp.length ())
Break
}
}
}
Word breaker, if necessary.
if (!strsplit.equals ("")) {
j = i; The initial value is recorded because there is a possibility that there is only one word in this line that will search the end
if (I < temp.length ()) {
while (Strsplit.indexof (temp.substring (i-1,i)) ==-1) {
i--;
if (i = = 0) {
i = j; Recovery
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;
}


Use examples:
int gintlineheight = 15;//global variable, row height
int gintbeginindex = 0;//global variable, change its value in keypressed, and then page up and down

String str = "For convenience, you can define a row height as a global variable, so it's better to write a simple example:"
Vector vector = getsubsection (Str,font.getdefaultfont (), getwidth (), "");

for (int i=gintbeginindex;i<vector.size (); i++) {
g.DrawString ((String) Vector.elementat (i), 0,gintlineheight*i,0);
if ((i-mintbeginindex+1) *gintlineheight>getheight ()) break;
}
vector = null;





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.