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

📄 customfont.java

📁 j2me下的方块游戏 直接放到wtk下即可运行
💻 JAVA
字号:
package CustomFont;

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

public class CustomFont {
	private int style;

	private int size;

	private int baseline;

	private int height;

	private int width;

	private Image image;

	/**
	 * 返回一个自定义字体的对象
	 */
	public static CustomFont getFont(String inName, int inStyle, int inSize) {
		Image i;
		String filename = inName;
		try {
			i = Image.createImage(filename);
		} catch (Throwable t) {
			t.printStackTrace();
			throw new IllegalArgumentException("Could not locate font: "
					+ filename + " : " + t);
		}

		return new CustomFont(i, inSize, inStyle);
	}

	private CustomFont(Image inImage, int inStyle, int inSize) {
		image = inImage;
		style = inStyle;
		size = inSize;

		try {
			height = image.getHeight();
			width = image.getWidth() / 128;
			baseline = calculateBaseline();
		} catch (Throwable t) {
			t.printStackTrace();
			throw new IllegalArgumentException("Specified font is invalid: "
					+ t);
		}
	}

	private int calculateBaseline() {
		// get baseline: defaults to last row
		int result = height;
		int imageWidth = image.getWidth();
		int max = 0;
		int total;
		int[] row = new int[imageWidth];
		int background;

		//确定背景颜色,假设(0,0)坐标的象素为背景颜色
		image.getRGB(row, 0, 1, 0, 0, 1, 1);
		background = row[0];

		// 按每行搜索象素
		for (int y = height / 2; y < height; y++) {
			total = 0;
			image.getRGB(row, 0, imageWidth, 0, y, imageWidth, 1);
			for (int x = 0; x < imageWidth; x++) {
				if (row[x] != background)
					total++;
			}
			if (total > max) {
				max = total;
				result = y;
			}
		}

		return result;
	}

	public int charsWidth(char[] ch, int offset, int length) {
		// 确定字符间距离
		return length * width;
	}

	public int charWidth(char ch) {
		return width;
	}

	public int getBaselinePosition() {
		return baseline;
	}

	public int getHeight() {
		return height;
	}

	public int stringWidth(String str) {
		return charsWidth(str.toCharArray(), 0, str.length());
	}

	public int substringWidth(String str, int offset, int len) {
		return charsWidth(str.toCharArray(), offset, len);
	}

	public int getSize() {
		return size;
	}

	public int getStyle() {
		return style;
	}

	public boolean isBold() {
		return ((style & Font.STYLE_BOLD) != 0);
	}

	public boolean isItalic() {
		return ((style & Font.STYLE_ITALIC) != 0);
	}

	public boolean isPlain() {
		return (style == 0);
	}

	public boolean isUnderlined() {
		return ((style & Font.STYLE_UNDERLINED) != 0);
	}

	/**
	 * 画单个字符
	 */
	public void drawChar(Graphics g, char character, int x, int y, int anchor) {
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();

		drawCharInternal(g, character, x, y, anchor);

		g.setClip(clipX, clipY, clipW, clipH);
	}

	/**
	 * 画字符数组
	 */
	public void drawChars(Graphics g, char[] data, int offset, int length,
			int x, int y, int anchor) {
		if ((anchor & Graphics.RIGHT) != 0) {
			x -= charsWidth(data, offset, length);
		} else if ((anchor & Graphics.HCENTER) != 0) {
			x -= (charsWidth(data, offset, length) / 2);
		}

		if ((anchor & Graphics.BOTTOM) != 0) {
			y -= height;
		} else if ((anchor & Graphics.VCENTER) != 0) {
			y -= height / 2;
		}

		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();

		char c;
		for (int i = 0; i < length; i++) {
			c = data[offset + i];
			drawCharInternal(g, c, x, y, Graphics.TOP | Graphics.LEFT);
			x += width;
		}

		g.setClip(clipX, clipY, clipW, clipH);
	}

	/**
	 * 绘制时间图片的字符,不用考虑内存等各种因素
	 */
	private void drawCharInternal(Graphics g, char character, int x, int y,
			int anchor) {
		if ((style & Font.STYLE_ITALIC) != 0) {
			// draw italicized: top half is shifted right
			g.setClip(x + 1, y, width, height / 2);
			g.drawImage(image, x - width * character + 1, y, anchor);
			g.setClip(x, y + height / 2, width, height / 2);
			g.drawImage(image, x - width * character, y, anchor);

			if ((style & Font.STYLE_BOLD) != 0) {
				g.setClip(x, y, width, height / 2);
				g.drawImage(image, x - width * character + 2, y, anchor);
				g.setClip(x, y + height / 2, width, height / 2);
				g.drawImage(image, x - width * character + 1, y, anchor);
			}
		} else {
			// draw normally
			g.setClip(x, y, width, height);
			g.drawImage(image, x - width * character, y, anchor);

			if ((style & Font.STYLE_BOLD) != 0) {
				g.drawImage(image, x - width * character + 1, y, anchor);
			}
		}

		if ((style & Font.STYLE_UNDERLINED) != 0) {
			g.drawLine(x, y + baseline + 2, x + width, y + baseline + 2);
		}
	}

	/**
	 * 绘制字符串.
	 */
	public void drawString(Graphics g, String str, int x, int y, int anchor) {
		drawChars(g, str.toCharArray(), 0, str.length(), x, y, anchor);
	}

	/**
	 * 绘制子字符串
	 */
	public void drawSubstring(Graphics g, String str, int offset, int len,
			int x, int y, int anchor) {
		drawChars(g, str.toCharArray(), offset, len, x, y, anchor);
	}

}

⌨️ 快捷键说明

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