⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutil.java

📁  一款改造的J2me手机游戏项目
💻 JAVA
字号:
package com.ejoy;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
//Download by http://www.codefans.net

public class StringUtil {
	/**
	 * 在制定矩形内,绘制折行文本,超出矩形范围的字符不显示
	 * @param src 文本字串
	 * @param g Graphics对象
	 * @param x 矩形左上角x坐标
	 * @param y 矩形左上角y坐标
	 * @param w 矩形宽度
	 * @param h 矩形高度
	 * @param leftMargin 左边矩
	 * @param topMargin 上边距
	 * @param isVTight	行间距是否紧凑
	 * @param fontColor 文字颜色
	 * @param font 绘制所用字体
	 */
	public static final void drawWrapString(String src, Graphics g, int x, int y, int w,
			int h, int leftMargin,int topMargin, boolean isVTight, int fontColor, Font font) {
		g.setFont(font);
		g.setColor(fontColor);
		int count = src.length();
		int curCharIndex = 0;
		int curX = x+leftMargin;
		int curY = y+topMargin;
		int vDelta=font.getHeight();
		if(isVTight){
			vDelta=font.getBaselinePosition();
		}
		while (curCharIndex < count) {
			char c = src.charAt(curCharIndex);
			if (c == '\n') {
				curX = x+leftMargin;
				curY += vDelta;
				curCharIndex++;
				continue;
			} else if (c == '\t') {
				// 相当于2个空格
				curX += font.charWidth(' ')*2;
				curCharIndex++;

				continue;
			}

			if (curX + font.charWidth(c) < x + w) {

				g.drawChar(c, curX, curY, Graphics.LEFT | Graphics.TOP);
				curCharIndex++;
				curX += font.charWidth(c);
				
			} else {
				//考虑换行
				if(curY+2*font.getBaselinePosition()<y+h){
					curY += vDelta;
					curX = x+leftMargin;
					g.drawChar(c, curX, curY, Graphics.LEFT | Graphics.TOP);
					curCharIndex++;
					curX += font.charWidth(c);
				}else{
					break;
				}	
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -