【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-Custom_Font.java,希望本篇文章对您学习有所帮助。
package ch10;
import javax.microedition.lcdui.*;
//该类实现了一个具有Font类同样功能的自定义字体类
public class Custom_Font {
//声明一个代表字体风格的int型变量
private int style;
//声明一个代表字体大小的int型变量
private int size;
//声明一个代表字体基线的int型变量
private int baseline;
//声明一个代表字体高度的int型变量
private int height;
//声明一个代表字体宽度的int型变量
private int width;
//声明一个代表字库的Image型变量
private Image image;
/*
5.构造器
*/
private Custom_Font(
Image inImage, int inStyle, int inSize) {
image = inImage;
style = inStyle;
size = inSize;
try {
height = image.getHeight();
width = image.getWidth() / 128;
baseline = calculateBaseline();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
/*
6.获取字体
*/
public static Custom_Font 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 Custom_Font(i, inSize, inStyle);
}
/*
7.计算基线
*/
private int calculateBaseline() {
int result = height;
int imageWidth = image.getWidth();
int max = 0;
int total;
int[] row = new int[imageWidth];
int background;
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) {
// monospaced font makes this an easy calculation
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);
}
/*
8.绘制多个字符
*/
public void drawChars(
Graphics g, char[] data,
int offset, int length, int x, int y, int anchor) {
if ( (anchor & g.RIGHT) != 0) {
x -= charsWidth(data, offset, length);
}
else
if ( (anchor & g.HCENTER) != 0) {
x -= (charsWidth(data, offset, length) / 2);
}
if ( (anchor & g.BOTTOM) != 0) {
y -= height;
}
else
if ( (anchor & g.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, g.TOP | g.LEFT);
x += width;
}
g.setClip(clipX, clipY, clipW, clipH);
}
/*
9.绘制图形字符
*/
private void drawCharInternal(
Graphics g, char character, int x, int y, int anchor) {
if ( (style & Font.STYLE_ITALIC) != 0) {
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 {
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);
}
}
上一篇:
CustomUserTypeMappingTest.java
下一篇:
邓小平精简机构理论探析