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

📄 newline.java

📁 J2ME程序:手动设置URL主机地址和端口
💻 JAVA
字号:


import java.util.Vector;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * @author lily
 *
 * TODO 文字自动换行
 */
public class NewLine {
	
	
	public static byte StringLineNumber = 0;
	
	  /**
	   * TODO 文字自动换行
	   * @param g Graphics  绘制对象
	   * @param str String  文字内容
	   * @param x int 绘制的起始x
	   * @param y int 绘制的起始y
	   * @param length int 文字的最大长度
	   * @param anchor int 绘制时的对齐方式
	   * @param color int 文字的颜色
	   */
	  public static void drawString(Graphics g, String str, int x, int y,
	                                int length, int anchor, int color) {
	    //g.setFont(gameConfig.defaultFont);
	    g.setColor(color);
	    Vector vector = getSubsection(str, g.getFont(), length, " ,?!"); //空格 , ?  !作为英文单词的分隔符
	    StringLineNumber = (byte)vector.size();
	    for (int i = 0; i < vector.size(); i++) {
	      g.drawString( (String) vector.elementAt(i), x,
	                   y + (g.getFont().getHeight()) * i,
	                   anchor);
	    }
	    vector = null;

	  }

	  /**
	   * TODO 拆分字符串
	   * @param strSource String
	   * @param font Font 字体设置
	   * @param width int 最大宽度
	   * @param strSplit String 单词的分隔符集合
	   * @return Vector 返回拆分的结果
	   */
	  public static Vector getSubsection(String strSource, Font font, int width,
	                                     String strSplit) {
	    Vector vector = new Vector();
	    String temp = strSource;
	    int i, j;
	    int LastLength = 1;
	    int step = 0;
	    try {
	      while (!temp.equals("")) {
	        i = temp.indexOf("\n");
	        if (i > 0) {
	          if (font.stringWidth(temp.substring(0, i - 1)) >= width) {
	            i = -1;
	          }
	        }
	        if (i == -1) {
	          if (LastLength > temp.length()) {
	            i = temp.length();
	          }
	          else {
	            i = LastLength;
	            step = font.stringWidth(temp.substring(0, i)) > width ? -1 : 1;
	            if (i < temp.length()) {
	              while (! (font.stringWidth(temp.substring(0, i)) <= width
	                        && font.stringWidth(temp.substring(0, i + 1)) > width)) {
	                i = i + step;
	                if (i == temp.length())
	                  break;
	              }
	            }
	          }
	          if (!strSplit.equals("")) {
	            j = i;
	            if (i < temp.length()) {
	              while (strSplit.indexOf(temp.substring(i - 1, i)) == -1) {
	                i--;
	                if (i == 0) {
	                  i = j;
	                  break;
	                }
	              }
	            }
	          }
	        }
	        LastLength = i;
	        vector.addElement(temp.substring(0, i));
	        if (i == temp.length()) {
	          temp = "";
	        }
	        else {
	          temp = temp.substring(i);
	          if (temp.substring(0, 1).equals("\n")) {
	            temp = temp.substring(1);
	          }
	        }
	      }
	    }
	    catch (Exception e) {
	      System.out.println("getSubsection:" + e);
	    }
	    return vector;
	  }
			
}

⌨️ 快捷键说明

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