JavaMe开发:低级界面绘图之点阵字

开发 后端
JavaMe自带的字体只有三种:Font.SIZE_SMALL、Font.SIZE_MEDIA和Font.SIZE_LARGE,所谓的Font.SIZE_LARGE其实也不是很大。如果要绘制其他字号的字体该怎么办呢?运用低级界面Canvas绘制的点阵字是一个不错的选择。

【问题描述】JavaMe自带的字体只有三种:Font.SIZE_SMALL、Font.SIZE_MEDIA和Font.SIZE_LARGE,所谓的Font.SIZE_LARGE其实也不是很大。如果要绘制其他字号的字体该怎么办呢?运用低级界面Canvas绘制的点阵字是一个不错的选择。

【字模工具】

晓奇工作室

取模方式:从左到右从上到下,横向8点左高位

【分析】

所谓“点阵字”就是文字是由点阵构成的。点阵字有两个基本属性,那就是宽(width)和高(height)。先看一个例子:

 

如图1所示,Asc5x8表示宽度为5,高度为8的点阵字集。一个char型的字符能表示8位。取模工具实际上是用二进制“1”,表示点阵的文字。用二进制的“0”,表示非文字的空白块。以Asc5x8的“!”为例进行讲解:

0x20,0x20,0x20,0x20,0x20,0x00,0x20,0x00, // -!-

对应的点阵为:

00100000

00100000

00100000

00100000

00100000

00000000

00100000

00000000

注意到,如果将“1”替换为像素点显示,而“0”替换为空白块。则刚好显示为一个“!”号。宽度为5,所以用一个字节表示就可以了。如果宽度超过一个字节,那么如何计算需要多少个字节才能表示宽度呢?原则是,宽度的表示必须为字节的倍数。如宽度为9,那么超过一个字节,所以用2个字节表示。

总结宽度所需字节计算公式如下:

n = (width-1)/8+1

Asc5x8中width即为5.

那么高度如何计算呢?高度其实不用进行计算,由取模软件完成了。

明白上述原理后,就很简单了。且看代码。

 

【代码清单】

  1. package com.token.view.components;  
  2.  
  3. import javax.microedition.lcdui.Graphics;  
  4. import javax.microedition.lcdui.game.GameCanvas;  
  5.  
  6. public class CustomFont {  
  7.       
  8.     public final char font24x48[][] =           // ASCII  
  9.     {  
  10.         {  
  11.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  // - -  
  12.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  13.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  14.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  15.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  16.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  17.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  18.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  19.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  20.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  21.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  22.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  23.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  24.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  25.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  26.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  27.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  28.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 
  29.         },  
  30.  
  31.         {  
  32.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  // -!-  
  33.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  34.             0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,  
  35.             0x00,0x78,0x00,0x00,0x78,0x00,0x00,0x78,  
  36.             0x00,0x00,0x78,0x00,0x00,0x78,0x00,0x00,  
  37.             0x78,0x00,0x00,0x78,0x00,0x00,0x78,0x00,  
  38.             0x00,0x78,0x00,0x00,0x78,0x00,0x00,0x30,  
  39.             0x00,0x00,0x30,0x00,0x00,0x30,0x00,0x00,  
  40.             0x30,0x00,0x00,0x30,0x00,0x00,0x30,0x00,  
  41.             0x00,0x30,0x00,0x00,0x30,0x00,0x00,0x30,  
  42.             0x00,0x00,0x30,0x00,0x00,0x30,0x00,0x00,  
  43.             0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  44.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  45.             0x00,0x00,0x70,0x00,0x00,0xF8,0x00,0x00,  
  46.             0xF8,0x00,0x00,0xF8,0x00,0x00,0x70,0x00,  
  47.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  48.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  49.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 
  50.         },  
  51.  
  52.         {  
  53.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  // -"-  
  54.             0x00,0x00,0xC0,0xC0,0x01,0xE1,0xE0,0x03,  
  55.             0xE3,0xE0,0x03,0xC3,0xC0,0x07,0x87,0x80,  
  56.             0x07,0x07,0x00,0x0E,0x0E,0x00,0x0C,0x0C,  
  57.             0x00,0x18,0x18,0x00,0x10,0x10,0x00,0x20,  
  58.             0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  59.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  60.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  61.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  62.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  63.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  64.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  65.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  66.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  67.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  68.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  69.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  70.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 
  71.         },  
  72.           
  73.         ...  
  74.  
  75.         {  
  76.             0x07,0x80,0x00,0x1F,0xC0,0x00,0x10,0xE0,  // --  
  77.             0x04,0x20,0x70,0x04,0x20,0x38,0x08,0x40,  
  78.             0x1C,0x08,0x40,0x0F,0x10,0x00,0x07,0xF0,  
  79.             0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,  
  80.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  81.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  82.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  83.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  84.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  85.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  86.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  87.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  88.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  89.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  90.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  91.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  92.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  
  93.             0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 
  94.         }  
  95.         };  
  96.               
  97.     public CustomFont()  
  98.     {  
  99.           
  100.     }  
  101.       
  102.     public void drawmat(GameCanvas canvas, Graphics g, char mat[], int w, int h, int x, int y, int color)  
  103.     {  
  104.         int i,j,k,n;  
  105.           
  106.         n = (w-1)/8+1;  
  107.         for(j=0; j<h; j++)  
  108.         {  
  109.             for(i=0; i<n; i++)  
  110.             {  
  111.                 for(k=0; k<8; k++)  
  112.                 {  
  113.                     if (!((mat[j * n + i] & (0x80 >> k)) == 0)) /* 测试为1的位则显示 */    
  114.                              {     
  115.                         g.setColor(color);     
  116.                                     g.drawLine(x + i * 8 + k, y + j, x + i * 8 + k, y + j);     
  117.                                 }     
  118.  
  119.                 }  
  120.             }  
  121.         }  
  122.     }  
  123.  
  124. }  

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

 

【调用方法】

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

(1)声明对象

  1. private CustomFont cf; 

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

  1. cf = new CustomFont(); 

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

  1. for(int i=0; i<passwd.length(); i++)  
  2. {  
  3.                       
  4.     cf.drawmat(this, graphics, cf.font24x48[passwd.charAt(i)-' '], 2448, (width-6*24)/2+i*24, (height-48)/3, Color.WHITE);  

【效果】

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

 

【设计参考】 

(1) http://aiquantong.iteye.com/blog/1457906

(2) http://blog.csdn.net/alleniversoncomputer/article/details/7016368

原文链接:http://blog.csdn.net/tandesir/article/details/7506065

【系列文章】

  1. JavaMe开发:低级界面绘图之点阵字
  2. JavaMe开发:低级界面绘图之菜单
  3. JavaMe开发:也说MVC设计模式
  4. JavaMe开发:绘制可自动换行文本
  5. JavaMe开发:绘制文本框TextEdit
  6. JavaM开发:自适应滚动显示
责任编辑:林师授 来源: tandesir的博客
相关推荐

2012-05-09 10:03:08

JavaMEJava

2012-05-09 10:09:18

JavaMEJava

2012-05-09 10:58:25

JavaMEJava

2012-05-09 10:35:36

JavaMEJava

2012-05-09 10:22:44

JavaMEJava

2010-06-03 14:01:26

JavaSEJavaMEiPhone

2009-09-22 17:53:32

C# COM组件开发

2011-08-10 18:24:22

iPhone 图形 绘图

2013-09-16 15:50:04

Android优化界面UI

2013-09-16 15:33:28

Android优化界面UI

2013-09-16 15:42:00

Android优化界面UI

2011-08-29 15:30:53

JavaMELua

2022-08-26 08:18:04

软件开发高级系统设计低级系统设计

2013-04-23 17:11:47

Windows Pho用Express Bl

2014-02-26 09:13:39

2011-05-03 16:41:04

BlackBerry

2022-09-02 14:56:24

Python界面开发库框架

2023-05-30 14:50:20

界面开发鸿蒙

2011-06-21 14:53:25

QT 界面美化 QStyle

2009-08-20 16:41:30

unixLinux低级IO函数
点赞
收藏

51CTO技术栈公众号