JavaMe连载(1)-低级界面绘图之点阵字(二)

2014-11-24 07:56:15 · 作者: · 浏览: 1
0xE3,0xE0,0x03,0xC3,0xC0,0x07,0x87,0x80,
0x07,0x07,0x00,0x0E,0x0E,0x00,0x0C,0x0C,
0x00,0x18,0x18,0x00,0x10,0x10,0x00,0x20,
0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
},

...

{
0x07,0x80,0x00,0x1F,0xC0,0x00,0x10,0xE0, // --
0x04,0x20,0x70,0x04,0x20,0x38,0x08,0x40,
0x1C,0x08,0x40,0x0F,0x10,0x00,0x07,0xF0,
0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
}
};

public CustomFont()
{

}

public void drawmat(GameCanvas canvas, Graphics g, char mat[], int w, int h, int x, int y, int color)
{
int i,j,k,n;

n = (w-1)/8+1;
for(j=0; j {
for(i=0; 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);
}

}
}
}
}

}
n就是表示整个宽度需要的字节数,由于宽度为24,故需要3个字节表示。j记录纵向(高度)位置。代码的处理,就是通过一个画线函数进行的。每次以字节为单位(变量k处理),对“1”进行检测,检测到1后,记录k的位置。

【调用方法】

CustomFont主要用于GameCanvas派生的类。在继承GameCanvas的类中:

(1)声明对象

[html]
private CustomFont cf;

(2) 初始化对象(在构造函数中)

[html]
cf = new CustomFont();

(3) 调用方法(画字符串passwd)

[html]
for(int i=0; i {

cf.drawmat(this, graphics, cf.font24x48[passwd.charAt(i)-' '], 24, 48, (width-6*24)/2+i*24, (height-48)/3, Color.WHITE);
}

【效果】

图2中,框中的文字就是通过绘制得到的。

\

图2 实现效果




摘自 tandesir的专栏