In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.