📄 canvashelper.java
字号:
package org.klings.wireless.j2me;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
/**
* Formats <code>String</code>s so they can be printed in a
* <code>Canvas</code> without exceeding the <code>Canvas</code> width.
*/
public class CanvasHelper {
/**
* Splits a <code>String</code>. Each fragment will not be longer than
* maxWidth when printed with the <code>Font</code> font.
*
* @param s
* The <code>String</code> to chop.
* @param font
* The <code>Font</code> used when calculating <code>String
* </code>
* lengths.
*
* @param maxWidth
* The maximum width of the resulting strings measured in pixels.
*
* @return <code>String</code> array containing one or more formatted
* strings. <code>null</code> if the supplied <code>String</code>
* or <code>
* Font</code> is <code>null</code>
*/
public static String[] splitString(String s, Font font, int maxWidth) {
if (s == null || font == null)
return null;
/* Return String immediately if it does not need formatting */
if (font.stringWidth(s) < maxWidth) {
String[] res = { s };
return res;
}
maxWidth -= font.charWidth('i');
/*
* Compute avg. number of characters on a line by getting the average
* between the 'i' and 'M';
*/
int avgChars = maxWidth
/ ((font.charWidth('i') + font.charWidth('M')) / 2);
int offset1 = 0;
int len = 0;
boolean longer = false;
boolean keepOn = false;
Vector v = new Vector();
while (s != null) {
/* The remaining String is short enough, so we are done */
if (font.stringWidth(s) < maxWidth) {
v.addElement(s);
s = null;
break;
}
/*
* Check that the String length is bigger than avgChars to avoid
* nullpointer exception.
*/
len = s.length();
if (len > avgChars)
len = avgChars;
/*
* If the String is to short, make it longer. Else make it shorter.
*/
if (font.substringWidth(s, 0, len) < maxWidth) {
len++;
while (font.substringWidth(s, 0, len++) < maxWidth) {
}
v.addElement(s.substring(0, len - 1));
s = s.substring(len - 1);
} else {
len--;
while (font.substringWidth(s, 0, len) > maxWidth)
len--;
v.addElement(s.substring(0, len));
s = s.substring(len);
}
}
/*
* Copy the results from the Vector to a String array and return the
* results.
*/
String[] result = new String[v.size()];
for (int i = 0; i < result.length; i++) {
result[i] = (String) v.elementAt(i);
}
return result;
}
/**
* Formats and prints a <code>String</code>, possibly over several lines.
*
* @param s
* The <code>String</code> to be printed.
* @param x
* The x coordinate of the anchor point.
* @param y
* The y coordinate of the anchor point.
* @param anchor
* The anchor point for positioning the text.
* @param f
* The <code>Font</code> to be used when drawing the text.
* @param maxWidth
* Maximum width of the strings, measured in pixels.
* @param g
* The <code>Graphics</code> object used when drawing text.
* @return The number of pixels used to draw the text in the y direction,
* given by: number of lines drawn * the <code>Font</code> height.
* @see javax.microedition.lcdui.Graphics#drawString(String str,int x, int
* y,int anchor)
*/
public static int printString(String s, int x, int y, int anchor, Font f,
int maxWidth, Graphics g) {
int yDelta = 0;
int fontHeight = f.getHeight();
g.setFont(f);
/*
* If the String does not need formatting, just print it. Else, chop it
* and print the Strings from the returned array.
*/
if (f.stringWidth(s) < maxWidth) {
g.drawString(s, x, y, anchor);
yDelta = fontHeight;
} else {
String[] content = splitString(s, f, maxWidth);
for (int i = 0; i < content.length; i++) {
g.drawString(content[i], x, y + yDelta, anchor);
yDelta += fontHeight;
}
}
/* Return the amount of pixels we have moved in the y direction */
return yDelta;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -