最近在学习C的过程中无意发现了点阵字库的东西。整理了下,在J2ME实现了下相关的算法。
对于使用少量的汉字图形程序,完全没有必要带巨大的汉字库来运行,只要内存允许,你可以把汉字字模直接定义为数组的信息存储在EXE文件里。通过这个工具,你可以把单个字模信息直接取出来,然后在源代码中粘贴。该工具的强大在于不仅可以生成12×12、16×16宋体这些存在于DOS字库的字模,而且可以生成16×16楷体、黑体甚至自定义字体的字模,而且可以提供6种字模大小供您选择并即时预览效果!对使用少量汉字的图形编程能够提供极大方便。
相对于绘制这些字体的J2ME函数是:
首先,我们要通过工具来生成点阵字库的GB2312字模,然后在图像中绘制出来,下面我给出一些字体的字模,
废话少说,具体看例子吧。
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Graphics;
- class FontCanvas extends Canvas {
- protected void paint(Graphics g) {
- g.setColor(0x0);
- g.fillRect(0, 0, this.getWidth(), this.getHeight());
- drawFont(g);
- }
- void drawFont(Graphics g) {
- int i, j;
- drawmat(yi12, g,12, 50, 20, 0xFFFFFF);
- drawmat(er12, g,12, 50, 50, 0xFFFFFF);
- drawmat(dian12, g,12, 50, 80, 0xFFFFFF);
- drawmat(zhen12, g,12, 50, 110, 0xFFFFFF);
- drawmat(song16, g,16, 50, 130, 0xFF0000);
- drawmat(ti16, g,16, 50, 150, 0xFF0000);
- drawmat(zi24, g,24, 50, 180, 0x0000FF);
- drawmat(mo24, g,24, 50, 200, 0x00FF00);
- drawmat(guan48, g,48, 50, 250, 0x0000FF);
- }
- void drawmat(char[] mat, Graphics g, int matsize, int x, int y, int color)
-
- {
- int i, j, k, n;
- n = (matsize - 1) / 8 + 1;
- for (j = 0; j < matsize; j++) {
- for (i = 0; i < n; i++) {
- for (k = 0; k < 8; k++) {
- if (!((mat[j * n + i] & (0x80 >> k)) == 0))
- {
- g.setColor(color);
- g.drawLine(x + i * 8 + k, y + j, x + i * 8 + k, y + j);
- }
- }
- }
- }
- }
- char yi12[] = {
-
- 0x00, 0x00, 0x0C, 0x00, 0x1C, 0x00, 0x3C, 0x00,
- 0x0C, 0x00, 0x0C, 0x00, 0x0C, 0x00, 0x0C, 0x00,
- 0x0C, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- char er12[] = {
-
- 0x00, 0x00, 0x3E, 0x00, 0x63, 0x00, 0x03, 0x00,
- 0x06, 0x00, 0x0C, 0x00, 0x18, 0x00, 0x30, 0x00,
- 0x63, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- char dian12[] = {
-
- 0x04, 0x00, 0x07, 0xC0, 0x04, 0x00, 0x04, 0x00,
- 0x3F, 0x80, 0x20, 0x80, 0x20, 0x80, 0x3F, 0x80,
- 0x00, 0x00, 0x49, 0x40, 0xC9, 0x20, 0x00, 0x00,
- };
- char zhen12[] = {
-
- 0xF2, 0x00, 0x9F, 0xE0, 0x92, 0x00, 0xA5, 0x00,
- 0xA5, 0x00, 0x97, 0xE0, 0x91, 0x00, 0xB1, 0x00,
- 0x8F, 0xE0, 0x81, 0x00, 0x81, 0x00, 0x00, 0x00,
- };
- char song16[] = {
-
- 0x02, 0x00, 0x01, 0x00, 0x7F, 0xFE, 0x41, 0x04,
- 0x81, 0x08, 0x01, 0x00, 0x7F, 0xFC, 0x03, 0x80,
- 0x05, 0x80, 0x05, 0x40, 0x09, 0x20, 0x11, 0x10,
- 0x21, 0x0E, 0x41, 0x04, 0x01, 0x00, 0x00, 0x00,
- };
- char ti16[] = {
-
- 0x10, 0x40, 0x18, 0x40, 0x10, 0x40, 0x20, 0x40,
- 0x37, 0xFC, 0x60, 0xC0, 0xA0, 0xE0, 0x21, 0x60,
- 0x21, 0x50, 0x22, 0x48, 0x24, 0x4E, 0x2B, 0xF4,
- 0x20, 0x40, 0x20, 0x40, 0x20, 0x40, 0x00, 0x00,
- };
- char zi24[] = {
-
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x18, 0x00, 0x08, 0x1C, 0x10,
- 0x1F, 0xFF, 0xF0, 0x18, 0x00, 0x30, 0x18, 0xFF,
- 0x10, 0x09, 0xC3, 0x90, 0x00, 0x3C, 0x00, 0x00,
- 0x1C, 0x00, 0x3F, 0xFF, 0xFE, 0x7E, 0x0C, 0xFE,
- 0x00, 0x1C, 0x04, 0x00, 0x1C, 0x00, 0x04, 0x3C,
- 0x00, 0x03, 0xF8, 0x00, 0x00, 0xE0, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- char mo24[] = {
-
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x02, 0x00,
- 0x30, 0x03, 0x0C, 0x20, 0x03, 0x04, 0x3C, 0x02,
- 0x07, 0xE0, 0x02, 0x1C, 0x40, 0x02, 0x00, 0x40,
- 0x03, 0xC0, 0x70, 0x7E, 0x1F, 0x98, 0x02, 0x08,
- 0x10, 0x06, 0x0F, 0x90, 0x07, 0x88, 0x10, 0x0A,
- 0xCB, 0xE0, 0x1A, 0x05, 0x00, 0x12, 0x01, 0x1E,
- 0x22, 0x0F, 0xFE, 0x42, 0x32, 0x80, 0x02, 0x02,
- 0x40, 0x02, 0x04, 0x60, 0x02, 0x0C, 0x30, 0x02,
- 0x18, 0x1C, 0x02, 0x20, 0x0F, 0x02, 0x00, 0x00,
- };
- char guan48[] = {
-
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x38, 0x00,
- 0x03, 0x00, 0x03, 0xC0, 0x38, 0x00, 0x07, 0x80,
- 0x03, 0xC0, 0x38, 0x00, 0x07, 0xC0, 0x03, 0xC0,
- 0x38, 0x00, 0x03, 0xF1, 0xFF, 0xFF, 0xFF, 0xFC,
- 0x01, 0xF9, 0xFF, 0xFF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x3C, 0x03,
- 0x80, 0x00, 0x07, 0x80, 0x3C, 0x03, 0x80, 0x00,
- 0x0F, 0x80, 0x3C, 0x03, 0x80, 0x00, 0x0F, 0x00,
- 0x3F, 0xFF, 0xFF, 0xF8, 0x1F, 0x00, 0x3F, 0xFF,
- 0xFF, 0xF8, 0x1F, 0x00, 0x3C, 0x00, 0x00, 0x00,
- 0x06, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- }
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
class FontCanvas extends Canvas {
protected void paint(Graphics g) {
g.setColor(0x0);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
drawFont(g);
}
void drawFont(Graphics g) {
int i, j;
drawmat(yi12, g,12, 50, 20, 0xFFFFFF);
drawmat(er12, g,12, 50, 50, 0xFFFFFF);
drawmat(dian12, g,12, 50, 80, 0xFFFFFF);
drawmat(zhen12, g,12, 50, 110, 0xFFFFFF);
drawmat(song16, g,16, 50, 130, 0xFF0000);
drawmat(ti16, g,16, 50, 150, 0xFF0000);
drawmat(zi24, g,24, 50, 180, 0x0000FF);
drawmat(mo24, g,24, 50, 200, 0x00FF00);
drawmat(guan48, g,48, 50, 250, 0x0000FF);
// for (i = 0; i < 15; i++) {
// drawmat(matarray16k + i*32, g,16, 200 + i * 15, 200, 0xFFFFFF);
// }
// drawmat(matarray16k + 8*32, g,16, 200 + i * 15, 200, 0xFFFFFF);
// drawmat(matarray16k + 9*32,g, 16, 200 + (++i) * 15, 200, 0xFFFFFF);
// drawmat(matarray16k + 10 * 32, g,16, 200 + (++i) * 15, 200, 0xFFFFFF);
// for (i = 18; i < 25; i++) {
// drawmat(matarray16k + (i - 3) * 32, 16, 200 + i * 15, 200, 0xFFFFFF);
// }
}
void drawmat(char[] mat, Graphics g, int matsize, int x, int y, int color)
/* 依次:字模指针、点阵大小、起始坐标(x,y)、颜色 */
{
int i, j, k, n;
n = (matsize - 1) / 8 + 1;
for (j = 0; j < matsize; j++) {
for (i = 0; i < n; i++) {
for (k = 0; k < 8; k++) {
if (!((mat[j * n + i] & (0x80 >> k)) == 0)) /* 测试为1的位则显示 */
{
g.setColor(color);
g.drawLine(x + i * 8 + k, y + j, x + i * 8 + k, y + j);
// putpixel(x + i * 8 + k, y + j, color);
}
// else
// g.setColor(0xFFFFFF);
}
}
}
}
char yi12[] = {
/* 以下是 '1' 的 12点阵宋体 字模,24 byte */
0x00, 0x00, 0x0C, 0x00, 0x1C, 0x00, 0x3C, 0x00,
0x0C, 0x00, 0x0C, 0x00, 0x0C, 0x00, 0x0C, 0x00,
0x0C, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00,
};
char er12[] = {
/* 以下是 '2' 的 12点阵宋体 字模,24 byte */
0x00, 0x00, 0x3E, 0x00, 0x63, 0x00, 0x03, 0x00,
0x06, 0x00, 0x0C, 0x00, 0x18, 0x00, 0x30, 0x00,
0x63, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00,
};
char dian12[] = {
/* 以下是 '点' 的 12点阵宋体 字模,24 byte */
0x04, 0x00, 0x07, 0xC0, 0x04, 0x00, 0x04, 0x00,
0x3F, 0x80, 0x20, 0x80, 0x20, 0x80, 0x3F, 0x80,
0x00, 0x00, 0x49, 0x40, 0xC9, 0x20, 0x00, 0x00,
};
char zhen12[] = {
/* 以下是 '阵' 的 12点阵宋体 字模,24 byte */
0xF2, 0x00, 0x9F, 0xE0, 0x92, 0x00, 0xA5, 0x00,
0xA5, 0x00, 0x97, 0xE0, 0x91, 0x00, 0xB1, 0x00,
0x8F, 0xE0, 0x81, 0x00, 0x81, 0x00, 0x00, 0x00,
};
char song16[] = {
/* 以下是 '宋' 的 16点阵宋体 字模,32 byte */
0x02, 0x00, 0x01, 0x00, 0x7F, 0xFE, 0x41, 0x04,
0x81, 0x08, 0x01, 0x00, 0x7F, 0xFC, 0x03, 0x80,
0x05, 0x80, 0x05, 0x40, 0x09, 0x20, 0x11, 0x10,
0x21, 0x0E, 0x41, 0x04, 0x01, 0x00, 0x00, 0x00,
};
char ti16[] = {
/* 以下是 '体' 的 16点阵宋体 字模,32 byte */
0x10, 0x40, 0x18, 0x40, 0x10, 0x40, 0x20, 0x40,
0x37, 0xFC, 0x60, 0xC0, 0xA0, 0xE0, 0x21, 0x60,
0x21, 0x50, 0x22, 0x48, 0x24, 0x4E, 0x2B, 0xF4,
0x20, 0x40, 0x20, 0x40, 0x20, 0x40, 0x00, 0x00,
};
char zi24[] = {
/* 以下是 '字' 的 24点阵隶书 字模,72 byte */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x18, 0x00, 0x08, 0x1C, 0x10,
0x1F, 0xFF, 0xF0, 0x18, 0x00, 0x30, 0x18, 0xFF,
0x10, 0x09, 0xC3, 0x90, 0x00, 0x3C, 0x00, 0x00,
0x1C, 0x00, 0x3F, 0xFF, 0xFE, 0x7E, 0x0C, 0xFE,
0x00, 0x1C, 0x04, 0x00, 0x1C, 0x00, 0x04, 0x3C,
0x00, 0x03, 0xF8, 0x00, 0x00, 0xE0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
char mo24[] = {
/* 以下是 '模' 的 24点阵楷体_GB2312 字模,72 byte */
0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x02, 0x00,
0x30, 0x03, 0x0C, 0x20, 0x03, 0x04, 0x3C, 0x02,
0x07, 0xE0, 0x02, 0x1C, 0x40, 0x02, 0x00, 0x40,
0x03, 0xC0, 0x70, 0x7E, 0x1F, 0x98, 0x02, 0x08,
0x10, 0x06, 0x0F, 0x90, 0x07, 0x88, 0x10, 0x0A,
0xCB, 0xE0, 0x1A, 0x05, 0x00, 0x12, 0x01, 0x1E,
0x22, 0x0F, 0xFE, 0x42, 0x32, 0x80, 0x02, 0x02,
0x40, 0x02, 0x04, 0x60, 0x02, 0x0C, 0x30, 0x02,
0x18, 0x1C, 0x02, 0x20, 0x0F, 0x02, 0x00, 0x00,
};
char guan48[] = {
/* 以下是 '灌' 的 48点阵黑体 字模,288 byte */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x38, 0x00,
0x03, 0x00, 0x03, 0xC0, 0x38, 0x00, 0x07, 0x80,
0x03, 0xC0, 0x38, 0x00, 0x07, 0xC0, 0x03, 0xC0,
0x38, 0x00, 0x03, 0xF1, 0xFF, 0xFF, 0xFF, 0xFC,
0x01, 0xF9, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x79,
0xF1, 0xC0, 0x39, 0xFC, 0x00, 0x30, 0x01, 0xC0,
0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xF0, 0xFF, 0xE0, 0x00, 0x00, 0xFF, 0xF0,
0xFF, 0xE0, 0x00, 0x00, 0xE0, 0x70, 0xE0, 0xE0,
0x0E, 0x00, 0xE0, 0x70, 0xE0, 0xE0, 0x1F, 0x80,
0xE0, 0x70, 0xE0, 0xE0, 0x1F, 0xE0, 0xFF, 0xF0,
0xFF, 0xE0, 0x07, 0xF0, 0xFF, 0xF0, 0xFF, 0xE0,
0x01, 0xF0, 0xFF, 0xF1, 0xFF, 0xE0, 0x00, 0x60,
0x08, 0x07, 0x00, 0x00, 0x00, 0x20, 0x1E, 0x03,
0x80, 0x00, 0x00, 0x00, 0x1E, 0x03, 0x00, 0x00,
0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xF0, 0x00, 0x00,
0x3F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x3F, 0xFF,
0xFF, 0xF0, 0x00, 0x00, 0x7C, 0x03, 0x80, 0x00,
0x00, 0x20, 0x7C, 0x03, 0x80, 0x00, 0x00, 0x70,
0xFC, 0x03, 0x80, 0x00, 0x00, 0x71, 0xFF, 0xFF,
0xFF, 0xE0, 0x00, 0x73, 0xFF, 0xFF, 0xFF, 0xE0,
0x00, 0xF3, 0xBC, 0x03, 0x80, 0x60, 0x00, 0xE3,
0xBC, 0x03, 0x80, 0x00, 0x01, 0xE1, 0x3C, 0x03,
0x80, 0x00, 0x01, 0xC0, 0x3F, 0xFF, 0xFF, 0xE0,
0x01, 0xC0, 0x3F, 0xFF, 0xFF, 0xE0, 0x03, 0xC0,
0x3F, 0xFF, 0xFF, 0xE0, 0x03, 0x80, 0x3C, 0x03,
0x80, 0x00, 0x07, 0x80, 0x3C, 0x03, 0x80, 0x00,
0x0F, 0x80, 0x3C, 0x03, 0x80, 0x00, 0x0F, 0x00,
0x3F, 0xFF, 0xFF, 0xF8, 0x1F, 0x00, 0x3F, 0xFF,
0xFF, 0xF8, 0x1F, 0x00, 0x3C, 0x00, 0x00, 0x00,
0x06, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
}
- import javax.microedition.lcdui.Display;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- public class FontMIDlet extends MIDlet {
- public FontMIDlet() {
- }
- protected void destroyApp(boolean unconditional)
- throws MIDletStateChangeException {
- }
- protected void pauseApp() {
- }
- protected void startApp() throws MIDletStateChangeException {
- Display.getDisplay(this).setCurrent(new FontCanvas());
- }
- }
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class FontMIDlet extends MIDlet {
public FontMIDlet() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(new FontCanvas());
}
}
运行截图
上述内容是从http://wuhua.iteye.com/blog/159107上copy的。
此仅仅是提供一种在J2ME上的自定义的点阵字库的实现,但是我们要是想把它集成在J2ME游戏引擎中,还相差甚远。现在摆在我们面前的至少有两大难题:
1,绘制汉字的函数效率不高。放在实际的游戏中影响整体的效果,使游戏“卡”??
2,我们应该如何管理点阵信息,如何保存,如何读取??
我会在以后的连载中逐个解决这两个问题!!