作为一个刚学j2me 的人
能不能帮我看看这代码,是不是错的很离谱,就是显示不了背景图import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Rff extends MIDlet {
private Display display;
private MyGameCanvas mgc=new MyGameCanvas();
private Display dis;
public Rff(){
}
protected void destroyApp(boolean arg0)throws MIDletStateChangeException {}
protected void pauseApp() { }
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
dis=Display.getDisplay(this);
dis.setCurrent(mgc);
}
}
class MyGameCanvas extends GameCanvas implements Runnable{
int w,h;
Image img;
static final int UP=0;
static final int DOWN=1;
static final int LEFT=2;
static final int RIGHT=3;
int aup[]={0,1,2,6};
int adown[]={7,8,9,13};
int aleft[]={14,15,16,20};
int aright[]={21,22,23,27};
int mSpeed=5;
int mDir=0;
private boolean RUN=true;
private Graphics gra;
private Sprite mHero;
Image img1 ;
int mapdata[][] = {
{ 43, 44, 44, 44, 9, 10, 11, 44, 44, 44, 45 },
{ 67, 68, 108, 69, 21, 22, 23, 68, 108, 68, 27 },
{ 46, 53, 53, 53, 33, 34, 35, 53, 53, 53, 39 },
{ 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 72 },
{ 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79 },
{ 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79 },
{ 79, 79, 79, 64, 65, 66, 79, 79, 79, 79, 79 },
{ 79, 79, 79, 76, 77, 78, 79, 79, 79, 79, 79 },
{ 79, 79, 79, 88, 89, 90, 79, 79, 79, 70, 71 },
{ 79, 79, 79, 79, 79, 79, 79, 79, 79, 82, 83 },
{ 79, 79, 79, 79, 79, 79, 79, 79, 79, 94, 95 },
{ 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
int tileWidth=16,tileHeight=16;
int mapRows=13,mapCols=11;
int imgCols=12;
MyGameCanvas(){
super(true);
gra=this.getGraphics();
this.setFullScreenMode(true);
w=this.getWidth();
h=this.getHeight();
try{
img=Image.createImage("/map.png");
img1=Image.createImage("/sp.png");
}catch(Exception ex){ex.printStackTrace();}
mHero=new Sprite(img1,48,48);
mHero.setPosition(20, 50);
new Thread(this).start();
}
public void paint(Graphics g){
for(int i=0;i<mapRows;i++)
{for(int j=0;j<mapCols;j++){
if(mapdata[i][j]!=0){
drawClipImage(g,img,j*tileWidth,i*tileHeight,(mapdata[i][j]-1)%
imgCols*tileWidth,
(mapdata[i][j]-1)/imgCols*tileHeight,tileWidth,tileHeight
);
}
}
}
}
public void drawClipImage(Graphics g ,Image img,int x,int y,int mx,int my,int mw,int mh){
g.setClip(x, y, mw, mh);
g.drawImage(img, x-mx, y-my, 0);
g.setClip(0, 0, w, h);
}
public void moveto(int dir){
switch(dir){case UP:
if(dir!=mDir)mHero.setFrameSequence(aup);
mHero.move(0, -mSpeed);
break;
case DOWN:
if(dir!=mDir)mHero.setFrameSequence(adown);
mHero.move(0, mSpeed);
break;
case LEFT:
if(dir!=mDir)mHero.setFrameSequence(aleft);
mHero.move(-mSpeed, 0);
break;
case RIGHT:
if(dir!=mDir)mHero.setFrameSequence(aright);
mHero.move(mSpeed, 0);
break;
}
mDir=dir;
mHero.nextFrame();
}
public void handleKey(){
int key=getKeyStates();
if((key&UP_PRESSED)!=0){moveto(UP);}
if((key&DOWN_PRESSED)!=0){moveto(DOWN);}
if((key&LEFT_PRESSED)!=0){moveto(LEFT);}
if((key&RIGHT_PRESSED)!=0){moveto(RIGHT);}
}
public void run() {
while(RUN){
try{
gra.setColor(255, 255,255);
gra.fillRect(0, 0, this.getWidth(), this.getHeight());
handleKey();
mHero.paint(gra);
this.flushGraphics();
Thread.sleep(100);
}
catch(Exception ex)
{ex.printStackTrace();}
}
while(true){
try{
Thread.sleep(2000);
}
catch(Exception e)
{e.printStackTrace();}
repaint();
}
}
}
--------------------编程问答-------------------- 看不懂 --------------------编程问答-------------------- 话说,现在还有搞javame的? --------------------编程问答--------------------
据说诺基亚还在弄s40的手机耶 --------------------编程问答-------------------- 学了很久了吧。。。简直就是打击啊 --------------------编程问答-------------------- 路过,顺便参观一下。。。 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 背景是按像素分的 每次重绘都得画进去 你看看是不是坐标不对 像素大小也要和你的map对应
补充:Java , J2ME