Walk.java



import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Walk extends MIDlet implements Runnable, CommandListener {
    public Image[] images = new Image[8];
    public Thread thread = null;
    public ZouCanvas canvas;
    public Command start, stop;
    public boolean runFlag = true;

    public void startApp() {
        try {
            for( int i=0; i<8; i++ ) {
                images[i]
                   = Image.createImage( "/walk" + (i+1) + ".png" );
            }
            canvas = new ZouCanvas();
            Display.getDisplay(this).setCurrent( canvas );
            stop = new Command( "Stop", Command.ITEM, 1 );
            start = new Command( "Start", Command.ITEM, 1 );
            canvas.addCommand( stop );
            canvas.addCommand( start );
            canvas.setCommandListener( this );
            canvas.setCommandListener( this );
            thread = new Thread( this );
            thread.start();
        }
        catch( Exception e ){
        }
    }

    public void pauseApp() {
    }

    public void destroyApp( boolean b ) {
    }

    public void run() {
       try {
            while( thread.isAlive() ) {
               if( runFlag ) {
                   canvas.repaint();
               }
               Thread.sleep( 200 );
            }
       }
       catch( Exception e ) {
       }
    }

    public void commandAction( Command c, Displayable d ) {
        if( c == start ) {
            runFlag = true;
        }
        else if( c == stop ) {
            runFlag = false;
        }
    }

    class ZouCanvas extends Canvas {

        public boolean firstFlag = true;
        public int counter = 0;
        public int w=0;
        public int h=0;
        public int iw=0;
        public int ih=0;
        public int x=0;
        public int y=0;
        public int d=8;

        public void paint( Graphics g ) {
            if( firstFlag == true ) {
                w = canvas.getWidth();
                h = canvas.getHeight();
                iw = images[0].getWidth();
                ih = images[0].getHeight();
                y = (h-ih)/2;
                x = (w-iw)/2;
                firstFlag = false;
            }
            else {
                x += d;
                if( x > w )
                  x = -w;
            }
            g.setColor( 255, 255, 255 );
            g.fillRect( x-d, y, d, ih );
            g.drawImage( images[ counter ], x, y, g.TOP | g.LEFT );
            counter = (counter+1)%8;
        }
    }
}