Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5406223
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: Java

2010-10-25 13:27:29

J2ME画图,以锚点定位,各锚点位置示意图如下:



说明:
1. 通过测试验证:
 g.drawString("文字", x, y, 0); 
 =  
g.drawString("文字", x, y, Graphics.LEFT | Graphics.TOP);
0是个默认值,默认的意思就是默认手机空屏时,第一个点可以正常显示的位置,即:Graphics.LEFT | Graphics.TOP


2. 参数“vcenter”为图片专用——这个锚点示意图是以“Image”为例的,当要画的是“String”时,定位参数“vcenter”非法;
3. 定位坐标为 两个参数一组,每组为一个int值,具体的值如下所列;
        int a = Graphics.LEFT | Graphics.TOP;
        
int b = Graphics.LEFT |
 Graphics.BASELINE;
        
int c = Graphics.LEFT |
 Graphics.VCENTER;
        
int d = Graphics.LEFT |
 Graphics.BOTTOM;
        
        
int e = Graphics.RIGHT |
 Graphics.TOP;
        
int f = Graphics.RIGHT |
 Graphics.BASELINE;
        
int g = Graphics.RIGHT |
 Graphics.VCENTER;
        
int h = Graphics.RIGHT |
 Graphics.BOTTOM;
        
        
int i = Graphics.HCENTER |
 Graphics.TOP;
        
int j = Graphics.HCENTER |
 Graphics.BASELINE;
        
int k = Graphics.HCENTER |
 Graphics.VCENTER;
        
int l = Graphics.HCENTER |
 Graphics.BOTTOM;
        
        System.out.println(
"a is : "+
a);
        System.out.println(
"b is : "+
b);
        System.out.println(
"c is : "+
c);
        System.out.println(
"d is : "+
d);
        
        System.out.println(
"e is : "+
e);
        System.out.println(
"f is : "+
f);
        System.out.println(
"g is : "+
g);
        System.out.println(
"h is : "+
h);
        
        System.out.println(
"i is : "+
i);
        System.out.println(
"j is : "+
j);
        System.out.println(
"k is : "+
k);
        System.out.println(
"l is : "+l);


打印结果为:
a is : 20
b is : 68
c is : 6
d is : 36
e is : 24
f is : 72
g is : 10
h is : 40
i is : 17
j is : 65
k is : 3
l is : 33

--End--

 



public void drawString(String str,
                       int x,
                       int y,
                       int anchor)
参数:
str - 要绘制的字符串
x - X坐标
y - Y坐标
anchor - 传说中的锚点
锚点的应用还是比较广泛的,当然用的最多的还是左上角,也就是
Graphics.LEFT | Graphics.TOP = 0
它画出来以后,就是把字符串的左上角定位到你写的坐标(x,y)上,如果是
Graphics.LEFT | Graphics.BOTTOM
就是指你画出来的字符串的左下角会定位到你写的坐标(x,y)上
还有Graphics.RIGHT | Graphics.TOP等等组合,具体的你可以参照文档嘛,记得文档的目录好像是在WTK安装目录下的doc下,如果找不到的话就去下载一个
我说过了,你必须用下面这些常量的组合,比如
Graphics.LEFT | Graphics.BOTTOM 表示左下点
或者
Graphics.BASELINE | Graphics.RIGHT 表示基线右点
你那个20 就是 Graphics.RIGHT | BOTTOM 也就是右下角的意思,默认情况下,直接用0,表示左上角,在你不明白这些用法以前,直接就0就得了~
下面是常量表
 public static final int BASELINE 64
 public static final int BOTTOM 32
 public static final int DOTTED 1
 public static final int HCENTER 1
 public static final int LEFT 4
 public static final int RIGHT 8
 public static final int SOLID 0
 public static final int TOP 16
 public static final int VCENTER 2
看下面的图片,红点是你设置的坐标,设置锚点为0,也就是你据说的第四个参数,这时画出的"张三"的左上角位置就是红点位置,也就是Graphics.LEFT | Graphics.TOP, 如果把锚点设置成 Graphics.RIGHT | Graphics.BOTTOM,绘制李四,这时的字符串的右下角位置和红点是重合的
下面那个孙二的位置比较特殊,是用BASELINE(基线)为坐标进行绘制的,具体怎么回事儿我也不清楚,大概是为了让中英文能在同一条线上吧,它的锚点是Graphics.BASELINE | LEFT
注意,在drawString中,不可使用VCENTER……

 

阅读(2567) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~