Easiest way to animate an image to appear that it's walking?
<p>I am recreating my own version of Mario. I would like for him to change between two images when the right or left key is held down to appear like he is walking.</p>

<p>This I would like to be done after certain intervals. What is the easiest way to do this? I have tried a few things but cannot get it to work. Ignore the animate with the switch statement as I know that is wrong. Here is my code. </p>

<pre><code>package sidescroller;

import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class Mario extends Sprite implements Commons {

    String mario = "tinyMarioRightStill.png";
    static int dx;
    static int dy; 
    int animate = 1;
    static boolean onGround = false;
    static boolean jumping;

    public Mario() {

        ImageIcon ii = new ImageIcon(this.getClass().getResource(mario));
        image = ii.getImage();

        width = image.getWidth(null);
        height = image.getHeight(null);

        resetState();

    }

    public void move() {
        x += dx;
        y += dy;

    }

    public void animate() {

        ImageIcon ii = new ImageIcon(this.getClass().getResource(mario));
        image = ii.getImage();

    }

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = -2;
            if (System.currentTimeMillis() % 2 == 0) {
                mario = "tinyMarioLeftStill.png";
                System.out.println(mario);
            }
            if ((System.currentTimeMillis() + 1) % 2 == 0) {
                mario = "tinyMarioLeftMove.png";
                System.out.println(mario);
            }
            ImageIcon ii = new ImageIcon(this.getClass().getResource(mario));
            image = ii.getImage();
        }

        if (key == KeyEvent.VK_RIGHT) {


            if (animate == 1) {
                animate = 2;
            }if (animate == 2) {
                animate = 1;
            }


            switch (animate) {
                case 1:
                    mario = "tinyMarioRightStill.png";

                case 2:
                    mario = "tinyMarioRightMove.png";

            }
            dx = 2;
        }




        if (key == KeyEvent.VK_SPACE) {
            int i = 0;
            jumping = true;
            while (i &lt;= 50) {
                dy = -1;
                i += 1;
                System.out.println("Jumping");
            }
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        ImageIcon ii = new ImageIcon(this.getClass().getResource(mario));

        if (key == KeyEvent.VK_LEFT) {
            mario = "tinyMarioLeftStill.png";
            System.out.println(mario);
        }

        image = ii.getImage();
        dx = 0;


        if (key == KeyEvent.VK_RIGHT) {
            mario = "tinyMarioRightStill.png";
            System.out.println(mario);
            image = ii.getImage();
            dx = 0;
        }
        if (key == KeyEvent.VK_SPACE &amp;&amp; !jumping) {
            dy = 0;
        }

    }

    public void resetState() {
        x = 200;
        y = 360;
    }
}
</code></pre>
