|
|
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class One extends Applet implements MouseListener,
MouseMotionListener {
Image[] frames = new Image[10];
int currentFrame = 0;
int locX = 60, locY = 60;
public void init() {
Component component = this;
MediaTracker tracker = new MediaTracker(component);
for (int i = 0; i < 10; i++) {
String imgName = "images/T" + (i + 1) + ".gif";
frames[i] = this.getImage(this.getCodeBase(), imgName);
tracker.addImage(frames[i], 0);
}
System.out.println("Images are being loaded...");
try {
tracker.waitForAll();
} catch (Exception e) {
System.out.println("Something went wrong...");
}
if (tracker.isErrorAny()) {
System.out.println("*** isErrorAny() reports an error... ");
} else if (tracker.checkAll()) {
System.out.println("All's well.");
}
MouseListener mouseListener = this;
this.addMouseListener(mouseListener);
MouseMotionListener mouseMotionListener = this;
this.addMouseMotionListener(mouseMotionListener);
}
public void paint(Graphics g) {
ImageObserver imgObserver = this;
g.drawImage(frames[currentFrame], locX, locY, imgObserver);
// g.drawString("(" + locX + ", " + locY + ")", locX, locY);
}
public void mouseMoved(MouseEvent e) {
currentFrame = (currentFrame + 1) % 10;
locX = e.getX();
locY = e.getY();
repaint();
}
public void mouseDragged(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
}
Don't forget that you need to get the images first. The code above does give you animation, of a very primitive type.