您的位置:首页 > 其它

J2ME手机游戏设计技术:Image图片显示

2008-03-28 10:39 330 查看
Image类用于处理图形界面,Image类中包含将置入手机屏幕的方法,使用Graphics类的相关方法绘制屏幕如示例6-5所示,调用方法如下:
public void drawImage (Image img,
(int x,
int y,
int anchor)
在anchor点绘制图片。
public int getHeight ()
取得图片高度。
public int getWidth ()
取得图片宽度。
public boolean isMutable ()
图片是否可以修改。

图片显示时,以anchor点作为显示基准点,使用时有一定的规则。它直接调用Graphics类中的锚点位置常量:
public static final int HCENTER
图片或文本与锚点水平中央对齐。
public static final int VCENTER
图片或文本与锚点垂直中央对齐。
public static final int LEFT
图片或文本与锚点最左边对齐。
public static final int RIGHT
图片或文本与锚点最右边对齐。
public static final int TOP
图片或文本与锚点最上方对齐。
public static final int BOTTOM
图片或文本与锚点最下方对齐。
public static final int BASELINE
图片或文字靠基准线对齐。
如图6-7所示。


图6-7 图片显示锚点示意图

Graphics类还有针对文字字型Font对象的应用,也是以这种对齐方式来显示。基本调用如下:
public void drawChar (char character,
int x,
int y,
int anchor)

使用当前设置的颜色和Font,绘制出一个字符(character)。(x,y)为显示起点,anchor为锚点。
public void drawChars(char[] ,
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

第一个安装的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);
g.drawString("图片的锚点", getWidth()/2, 2, Graphics.HCENTER |
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

【说明】从程序中可看到,随着anchor点的变化,图片显示点的坐标位置也会不同。所以游戏时,一定要注意anchor锚点不要随意变动,最好保持锚点不变。如果需要图片变更位置,以变换(X,Y)轴坐标位置来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐