资源简介
点阵字库在JAVA中的实现源码
代码片段和文件信息
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class Test extends Jframe {
byte[] dotfont;
BufferedImage imgCH;
int[] verify = {128 64 32 16 8 4 2 1};
String test = “点阵汉字的测试“;
int imgWidth = 300;
int imgHeight = 200;
public Test() {
super(“DotFont“);
File file = new File(“gb.dat“);
try {
FileInputStream fis = new FileInputStream(file);
dotfont = new byte[fis.available()];
fis.read(dotfont);
fis.close();
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
this.setSize(300 240);
this.show();
}
void createCH(byte[] ch int off) {
int q1 = ch[off] & 0xff;
int q2 = ch[off + 1] & 0xff;
int offset = (q1 - 0xa1) * 94 * 24;
q2 -= 0xa1;
offset += q2 * 24;
imgCH = new BufferedImage(12 12 BufferedImage.TYPE_INT_RGB);
for (int h = 0; h < 12; h++) {
byte b = dotfont[offset++];
for (int w = 0; w < 8; w++) {
if ((b & verify[w]) == verify[w]) {
imgCH.setRGB(w h 0xffffffff);
} else {
imgCH.setRGB(w h 0);
}
}
b = dotfont[offset++];
for (int w = 0; w < 4; w++) {
if ((b & verify[w]) == verify[w]) {
imgCH.setRGB(w + 8 h 0xffffffff);
} else {
imgCH.setRGB(w + 8 h 0);
}
}
}
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0 0 getWidth() getHeight());
byte[] an = str2bytes(test);
int offset = 0;
int x = 10 y = 34;
while (y < imgHeight && offset < an.length) {
int b = an[offset] & 0xff;
if (b > 0x7f) {
createCH(an offset);
g.drawImage(imgCH x y null);
x += 12;
offset += 2;
} else { //英文暂时不考虑
x += 6;
offset++;
}
if (x > imgWidth) {
x = 10;
y += 14;
}
}
}
byte[] str2bytes(String s) {
if (null == s || ““.equals(s)) {
return null;
}
byte[] abytes = null;
try {
abytes = s.getBytes(“gb2312“);
} catch (UnsupportedEncodingException ex) {
}
return abytes;
}
public static void main(String[] args) {
new Test();
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2006-11-03 14:30 dotfont_src\
文件 2826 2006-07-18 16:31 dotfont_src\Test.java
文件 196272 2005-08-31 10:46 dotfont_src\gb.dat
文件 2401 2006-07-18 16:31 dotfont_src\Test.class
文件 9 2006-07-18 17:06 dotfont_src\run.bat
- 上一篇:基于java实现中国象棋小游戏升级版
- 下一篇:Java实现信息安全技术 密钥分配
评论
共有 条评论