DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 移动应用 > JavaME

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

【问题描述】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)-' '], 24, 48, (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

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号