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)轴坐标位置来。
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)轴坐标位置来。
相关文章推荐
- unity中Image显示android手机SD卡中的图片
- Dreamweaver中用css载入bg-image图片在设计栏不出现但可在网页中正常显示
- 利用Java Swing技术设计一个鼠标点击速度比赛游戏程序。程序显示一个按钮和一个文本框,用户点击按钮,文本框显示鼠标点击次数。两个人同时运行本程序,即可进行比赛
- J2ME手机游戏对于图片的要求
- J2ME手机游戏开发引擎的分析与设计
- android 部分手机拍照后在imageView中显示预览图片被旋转的问题
- J2ME游戏设计基础练习1_用图片数字代替文本数字绘制
- 利用Java Swing技术设计一个鼠标点击速度比赛游戏程序。程序显示一个按钮和一个文本框,用户点击按钮,文本框显示鼠标点击次数。
- J2ME 用来处理手机游戏中长字符串显示问题,自己写的,仅作参考
- PHP后台技术-后台登录安全问题和显示多条图片数据库设计
- Android开发 利用imageview显示选取的手机内存的图片
- J2ME手机游戏对于图片的要求
- 安卓开发-利用smart-image-view来实现网络中的图片在手机上的显示
- Android手机开发:ImageView使用和从内存读取图片显示
- J2ME下基于MIDP2.0的手机游戏设计
- android网络获取图片并保存在本地和获取手机SD卡中的图片显示到ImageView上及利用代码删除图片
- 搜应用网是中国最丰富的手机资源下载站,为你提供精彩的手机图片.手机应用软件.手机游戏,定制服务手机用户的个性化需求,是广大手机用户.手机开发者.手机软件界面设计人员的广阔乐园.
- J2ME技术前瞻——3D手机游戏能发展起来吗?
- Android手机开发:图片的放大和缩小显示ImageView
- 《游戏脚本的设计与开发》-1.4 图片的读取显示