Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw automatic Wrap text in JavaMe Development

Shulou Source: shulou.com Published: 2022-06-02 20:47:54 10月01日 Update

In this issue, the editor will bring you about how to draw line-wrapping text in JavaMe development. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

[problem description]

DrawString in the JavaMe Graphics class does not support text wrapping, so when a longer string is drawn, the text is drawn on the same line, and the string beyond the screen is truncated. How to make the drawn text wrap automatically?

[analysis]

DrawString can not achieve automatic line wrapping, but it can achieve the positioning of text drawing. Therefore, it can be considered that the text is divided into multiple substrings, and then the substrings are drawn. The strategy for splitting is as follows:

1 when a newline character is encountered, split it

When the length of the string is greater than the set length (usually the width of the screen), it is split.

[steps]

1 define a String and String [] object

Private String info; private String info_wrap []

2 to realize the automatic line wrapping split function of string

StringDealMethod.java

Package com.token.util; import java.util.Vector; import javax.microedition.lcdui.Font; public class StringDealMethod {public StringDealMethod () {} / / string cutting to achieve automatic string wrapping public static String [] 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(index1) == '\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."); / / 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)

Tags: Character string text analysis development content function screen effect length test line professional medium and small rich in content multiple width pair object that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information MySQL MariaDB NVidia