J2ME 点阵字库的使用
来源:互联网 发布:在哪里看加购数据 编辑:程序博客网 时间:2024/05/16 10:08
由于J2ME的字体大小限制,使用系统提供的三种格式无法满足要求,最先想到的办法是使用外部字库,网上找了半天资料,整理了下写了个测试程序。(部分源码来自开奇网www.kaiqi.com)
点阵字库生成工具网上到处都是,不想找的话也可以在我资源里下http://download.csdn.net/user/swingboy
使用的IDE 为 myeclipse 集成的ME插件,将生成的点阵字库放在res下
以下为源码:
package font;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class FontTest_2 extends MIDlet {
public FontTest_2() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
Display display=Display.getDisplay(this);
FontCanvas fc=new FontCanvas();
display.setCurrent(fc);
}
class FontCanvas extends Canvas
{
Font12 f12=new Font12();
protected void paint(Graphics g) {
f12.drawString(g, "我我我我我我我我我我我我我我我", 0, 0, 255);
}
}
}
===================================================================
package font;
import java.io.InputStream;
import javax.microedition.lcdui.Graphics;
public class Font12 {
public static final int[] mask={128,64,32,16,8,4,2,1};
public final static String ZK_PATH="/HZK12"; //生成的点阵字库文件,放在res下
public final static String ENCODE="GB2312";
public static InputStream in;
protected void drawString(Graphics g,String str,int x,int y,int color)
{
byte[] data=null;
int[] code=null;
int byteCount;
int lCount;
g.setColor(color);
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)<0x80)
{
g.drawString(str.substring(i,i+1), x+(i<<4),y, 0);
continue;
}
code=getByteCode(str.substring(i, i+1));
data=read(code[0],code[1]);
byteCount=0;
for(int line=0;line<12;line++)
{
lCount=0;
for(int k=0;k<2;k++)
{
for(int j=0;j<8;j++)
{
if((data[byteCount]&mask[j])==mask[j])
{
//i*3是自定义的偏移量,表示字与字之间的间距,可以设置为 i*4,i*5,间隔根据需要定
g.drawLine(x+lCount+(i<<4)-i*3, y+line, x+lCount+(i<<4)-i*3, y+line);
}
lCount++;
}
byteCount++;
}
}
}
}
protected byte[] read(int areaCode,int postCode)
{
byte[] data=null;
try{
int area=areaCode-0xa0;
int post=postCode-0xa0;
if(in==null)
{
in=getClass().getResourceAsStream(ZK_PATH);
}
long offset=24*((area-1)*94+post-1);
in.skip(offset);
data=new byte[24];
in.read(data, 0, 24);
in.reset();
}catch(Exception e){}
return data;
}
protected int[] getByteCode(String str)
{
int[] byteCode=new int[2];
try{
byte[] data=str.getBytes(ENCODE);
byteCode[0]=data[0]<0?256+data[0]:data[0];
byteCode[1]=data[1]<0?256+data[1]:data[1];
}catch(Exception e){}
return byteCode;
}
public void closeStream()
{
try{
in.close();
}catch(Exception e){}
}
}
结束
程序的弊端是占用大量内存和读取字库效率颇低。入过的高手们不知道能不能优化这代码,或者有其他的方案来替换点阵字库,比如用图片啊什么的。有更好的方案请留言哦,非常感谢
- J2ME 点阵字库的使用
- 关于点阵字库在J2ME的实现。
- [分享]点阵字库分析及在J2ME上的实现
- 16*16点阵字库HZK16的使用
- 点阵字库在J2ME中的实现
- 使用UCDOS的HZK16点阵字库的演示程序
- ASCII字符点阵字库的制作和使用
- GBK点阵显示字库的制作和使用
- GBK点阵显示字库的制作和使用
- ASCII字符点阵字库的制作和使用
- GBK点阵显示字库的制作和使用
- ASCII字符点阵字库的制作和使用
- ASCII字符点阵字库的制作和使用
- GBK点阵显示字库的制作和使用
- ASCII字符点阵字库的制作和使用
- ASCII字符点阵字库的制作和使用
- GBK点阵显示字库的制作和使用
- 汉字点阵字库的实现
- C Primer Plus学习计划
- 通过汇编看调用协定
- hack tools
- 动态sql语句基本语法
- 一个生成和解析XML的c++程序
- J2ME 点阵字库的使用
- PKU_3461
- 终极解决端口被占用问题
- Hibernate设计得失分析
- 使用Live Writer试一下
- 向Oracle中插入日期
- Java高手之路
- VC++中的一些小问题的解决方法
- malloc的用法