java 绘制点阵_[Java基础知识]点阵字库在JAVA中的实现

该博客介绍了一个Java程序,用于从'gb.dat'文件中读取点阵字库数据,并使用这些数据在屏幕上绘制点阵汉字。程序通过将汉字转换为字节并解析字节来创建BufferedImage对象,然后在GUI上显示。
摘要由CSDN通过智能技术生成

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值