问题描述
问一下大家,就是我想实现点一下开始这个命令,然后屏幕上那幅图像就一直旋转,就好像动画那样,帮忙看看这个代码哪里错了哈,谢谢。可以运行,但是屏幕上还是那幅图片,没有效果哈protectedvoidpaint(Graphicsg){/*g.drawImage(img1,x,y,Graphics.LEFT|Graphics.TOP);*/g.setColor(255,255,255);g.fillRect(0,0,width,height);g.drawRegion(img1,0,0,img1.getWidth(),img1.getHeight(),dir,x,y,Graphics.LEFT|Graphics.TOP);//g.drawRegion(img2,0,0,img2.getWidth(),img2.getHeight(),Sprite.TRANS_NONE,x+30,y,Graphics.LEFT|Graphics.TOP);}publicvoidcommandAction(Commandarg0,Displayablearg1){if(arg0==start){while(true){switch(dir){caseSprite.TRANS_NONE:dir=Sprite.TRANS_MIRROR_ROT90;break;caseSprite.TRANS_MIRROR_ROT90:dir=Sprite.TRANS_MIRROR_ROT180;break;caseSprite.TRANS_MIRROR_ROT180:dir=Sprite.TRANS_MIRROR_ROT270;break;caseSprite.TRANS_MIRROR_ROT270:dir=Sprite.TRANS_NONE;break;}repaint();try{Thread.sleep(4000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}System.out.println("执行了");}}}
解决方案
解决方案二:
在commandAction开个线程处理循环示例代码如下:importjava.io.IOException;importjavax.microedition.lcdui.Canvas;importjavax.microedition.lcdui.Command;importjavax.microedition.lcdui.CommandListener;importjavax.microedition.lcdui.Displayable;importjavax.microedition.lcdui.Graphics;importjavax.microedition.lcdui.Image;importjavax.microedition.lcdui.game.Sprite;publicclassCartoonCanvasextendsCanvasimplementsCommandListener{privateintwidth;privateintheight;privateImageimg1;privateintdir;privateintx;privateinty;Commandstart=newCommand("启动",Command.OK,1);privateCartoonThreadthread;publicCartoonCanvas(){width=getWidth();height=getHeight();try{img1=Image.createImage("/img.png");}catch(IOExceptione){//TODO自动生成catch块e.printStackTrace();}addCommand(start);setCommandListener(this);}protectedvoidpaint(Graphicsg){/*g.drawImage(img1,x,y,Graphics.LEFT|Graphics.TOP);*/g.setColor(255,255,255);g.fillRect(0,0,width,height);g.drawRegion(img1,0,0,img1.getWidth(),img1.getHeight(),dir,x,y,Graphics.LEFT|Graphics.TOP);//g.drawRegion(img2,0,0,//img2.getWidth(),img2.getHeight(),Sprite.TRANS_NONE,x+30,//y,Graphics.LEFT|Graphics.TOP);}publicvoidcommandAction(Commandarg0,Displayablearg1){if(arg0==start){if(thread==null){thread=newCartoonThread();thread.start();}}}publicclassCartoonThreadextendsThread{publicvoidrun(){while(true){switch(dir){caseSprite.TRANS_NONE:dir=Sprite.TRANS_MIRROR_ROT90;break;caseSprite.TRANS_MIRROR_ROT90:dir=Sprite.TRANS_MIRROR_ROT180;break;caseSprite.TRANS_MIRROR_ROT180:dir=Sprite.TRANS_MIRROR_ROT270;break;caseSprite.TRANS_MIRROR_ROT270:dir=Sprite.TRANS_NONE;break;}repaint();try{Thread.sleep(4000);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}System.out.println("执行了");}}}}
解决方案三:
mark