日期:2014-05-20 浏览次数:20825 次
//动画的精灵同时也在运动,把一个连续的图片分成几个等分部分, 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 MIDlet4 extends MIDlet { private MyGameCanvas mgc=new MyGameCanvas(); private Display dis; public MIDlet4() { } protected void startApp() throws MIDletStateChangeException { dis=Display.getDisplay(this); dis.setCurrent(mgc); } protected void pauseApp() { } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } class MyGameCanvas extends GameCanvas implements Runnable{ private Image img; private Sprite sp; private Graphics gra; private boolean RUN=true; public MyGameCanvas(){ super(true); gra=this.getGraphics(); try{ img=Image.createImage("/donghua.png");//放入要切割的图片 sp=new Sprite(img,img.getWidth()/4,img.getHeight()); }catch(Exception ex){ ex.printStackTrace(); } new Thread(this).start(); } public void run(){ while(RUN){ try{ //清屏 gra.setColor(255,255,255); gra.fillRect(0, 0, this.getWidth(), this.getHeight()); sp.paint(gra); this.flushGraphics();//缓冲区画 sp.move(1, 1);//移动 sp.nextFrame();//动画效果 Thread.currentThread().sleep(100); }catch(Exception ex){ ex.printStackTrace(); } } } } }
img = Image.createImage("/donghua.png"); // 放入要切割的图片 sp = new Sprite(img, img.getWidth() / 4, img.getHeight());