Handling text line breaks in MIDP

Source: Internet
Author: User
In games, especially in scenario games, a large number of texts are often required. To display the text on a small cell phone screen, you must process the text so that it can correctly wrap the text within the range of the width you want to display. Next I will introduce in detail how to process text line breaks.
Calculate the position where the line feed is needed. Here we use linewd, the width to be displayed, and "/N" as the line feed sign.

Static public int changline (string STR, Font font, int linewd, Boolean fullword)
{// Calculate the position where the line feed is needed. Str: the text to be displayed Font: The text font linewd: the width to be displayed
Int Len = 0, WD = 0;
For (INT I = 0; I <Str. Length (); I ++)
{
If (Str. charat (I) = '/N ')
{
If (I = 0)
Return Len + 1;
Else
Return Len + 2;
}
WD + = font. charwidth (Str. charat (I ));
If (WD> linewd)
{
If (fullword)
{
For (Int J = Len; j> = 0; j --)
{
If (Str. charat (j) <0x30 | Str. charat (j) >=128)
{
Len = J;
Break;
}
}
}
Return Len + 1;
}
Len = I;
}
Return 0;
}

After calculating the location, it starts to be a text branch.

Static public void doline (string infostr, int Len)

{// String branch for ease of Display
String tmpstr;
Vector infolines = NULL;
Infolines = new vector ();
Int tmpint; // the position where the line feed is required.
While (true)
{
Tmpint = changline (infostr, defaultfont, Len, false );
If (tmpint = 0)
{
Infolines. addelement (infostr );
Break;
}
Else
{
If (infostr. charat (tmpint-1) = '/N ')
Tmpstr = infostr. substring (0, tmpint-1 );
Else
Tmpstr = infostr. substring (0, tmpint );

Infolines. addelement (tmpstr );
Infostr = infostr. substring (tmpint, infostr. Length ());

}
}
}

The above is the code for processing text branches. Next I will introduce the scrolling Effect of text in the program. Please pay attention to subsequent articles.

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.