Needing JAVA help in animation

2019-09-16 15:40发布

I'm new in Java (still learning) all the other works have gone well but only this animation gives my headache and the coffee won't even help=(! I should make an animation of Javaman (10 gif pictures named as T1, T2,...T10) I should use Thread, MediaTracker-class and addImage-method. Then I should specify the speed of the animation by using sleep-method (I used join-method if that's right??).

(MY JAVA CODE GOES LIKE THIS)

import java.applet.Applet;
import java.awt.*;

public class Animaatio extends Applet implements Runnable {
    Image[] images = null;
    MediaTracker tracker = null;
    int current = 0;
    Thread animThread;

    @Override
    public void init() {
        // Creating a new media tracker, to track loading images
        tracker = new MediaTracker(this);
        // Creating an array of ten images
        images = new Image[10];
        // Downloading the images
        for (int i = 0; i < 10; i++) {
            // Loading the images
            images[i] = getImage(getCodeBase(), (i + 1) + "T.gif");
            tracker.addImage(images[i], 0);
        }
        try {

            tracker.waitForAll();
        } catch (InterruptedException e) {
        }
    }

    @Override
    public void start() {
        if (animThread == null) {
            animThread = new Thread(this);
            animThread.start();
        }
        try {

            animThread.join();
        } catch (InterruptedException e) {
        }
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(images[current++], 0, 0, this);
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }
    }
}

The problem is that I don't see any animation just an empty applet viewer which just keeps running. Problem might be caused if the images are storaged in the wrong place? If someone could wreally help me, I would be really thankful for my knight=).

0条回答
登录 后发表回答