2008年(3500)
分类:
2008-05-04 20:33:16
public void drawChar (char character,
int x,
int y,
int anchor)
使用当前设置的颜色和Font,绘制出一个字符(character)。(x,y)为显示起点,anchor为锚点。
public void drawChars(char[] data,
int offset,
int length,
int x,
int y,
int anchor)
使用当前颜色和Font设置,绘制出数组中的部分字符(data)。以offset为字符起始值,长度为length,(x,y)为显示起点,anchor为锚点。
public void drawString (String str,
int x,
int y,
int anchor)
使用当前颜色和Font设置,绘制出字符串(str)。
public void drawString (String str,
int offset
int len
int x,
int y,
int anchor)
使用当前颜色和Font设置,绘制出部分字符串(str)。
示例6-5
/×
第一个安装JDK的ava程序测试
×/
import javax.microedition.midlet.×;
import javax.microedition.lcdui.×;
public class canvasDemo2 extends MIDlet
{
Display display;
canvasAnchor canvasanchor;
public canvasDemo2()
{
canvasanchor=new canvasAnchor();
display=Display.getDisplay(this);
}
public void startApp()
{
display.setCurrent(canvasanchor);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
class canvasAnchor extends Canvas
{
int gameaction=0;
int moveX=0;
int moveY=0;
int[] moveLR={Graphics.RIGHT, Graphics.HCENTER, Graphics.LEFT};
int[] moveUD={Graphics.BOTTOM, Graphics.VCENTER, Graphics.TOP};
String[] anchorLR={"Graphics.RIGHT", "Graphics.HCENTER", "Graphics. LEFT"};
String[] anchorUD={"Graphics.BOTTOM", "Graphics.VCENTER", "Graphics. TOP"};
Image pictureImage;
public canvasAnchor()
{
try{
pictureImage=Image.createImage("/picture.png");
}catch(Exception ex){}
}
public void keyPressed(int keyCode)
{
gameaction=getGameAction(keyCode);
switch(gameaction)
{
case LEFT:
moveX--;
if(moveX<0) moveX=0;
repaint();
break;
case RIGHT:
moveX ;
if(moveX>2) moveX=2;
repaint();
break;
case UP:
moveY--;
if(moveY<0) moveY=0;
repaint();
break;
case DOWN:
moveY ;
if(moveY>2) moveY=2;
repaint();
break;
}
}
public void paint(Graphics g)
{
g.setColor(0xFFFFFF);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0);
Graphics.TOP);
g.drawString("状态:" anchorLR[moveX] " 左右数值:" String.va lueOf
(moveLR[moveX]),
0, 22, Graphics.LEFT | Graphics.TOP);
g.drawString("状态:" anchorUD[moveY] " 上下数值:" String.valueOf
(moveUD[moveY]),
0, 42, Graphics.LEFT | Graphics.TOP);
g.drawImage(pictureImage, getWidth()/2, 117, moveLR[moveX] |
moveUD[moveY]);
g.drawLine(0,117,getWidth(),117);
g.drawLine(getWidth()/2,57,getWidth()/2,177);
}
}
}
运行结果如图6-8所示。
图6-8
下载本文示例代码