Thank you Rahul,
It sounds pretty much right to me, what you have said. I have posted my code as requested by Nate. I think this will just verify what Rahul has explained.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
* Insert the type's description here.
* Creation date: (09/03/01 16:11:33)
* @author:
*/
public class Neko extends Applet implements MouseListener, MouseMotionListener, Runnable {
Thread thread;
public java.awt.Image[] catPics = new java.awt.Image[9];
private java.lang.String[] cat = {"awake.gif","right1.gif","right2.gif","scratch1.gif","scratch2.gif","sleep1.gif","sleep2.gif","stop.gif","yawn.gif"};
public java.awt.Image currentImage;
private int x = 0;
public int endX;
public int currentX = 0;
private int y = 20;
public int currentY = 20;
/**
* Initializes the applet.
*/
public void init() {
addMouseListener(this);
addMouseMotionListener(this);
for (int i=0; i<catPics.length;i++)
catPics[i] = getImage(getDocumentBase(),"images/"+cat[i]);
currentImage=catPics[0];
}
public void mouseMoved(MouseEvent e) {
endX=e.getX();
System.out.println("Am at horizontal position "+endX+" and current position is "+currentX);
if (endX>currentX){
System.out.println("Am about to run to the right");
runRight(endX);
}
else{
System.out.println("Am about to run to the left");
runLeft(endX);
}
}
public void paint(Graphics g) {
System.out.println("Am in the paint method now");
if (currentImage!=null){
System.out.println(currentX+"\t"+currentY);
g.drawImage(currentImage,currentX,currentY,this);
}
// insert code to paint the applet here
}
/**
* Insert the method's description here.
* Creation date: (12/03/01 09:17:10)
* @param time long
*/
public void pause(long time) {
try{
Thread.sleep(time);
}
catch (InterruptedException e){}
}
/**
* Insert the method's description here.
* Creation date: (12/03/01 14:02:20)
* @param numZs int
*/
public void rest(int numZs) {
currentImage=catPics[8];
repaint();
pause(400);
for (int i=0; i<numZs; i++){
if (currentImage!=catPics[5])
currentImage=catPics[5];
else
currentImage=catPics[6];
repaint();
pause(800);
}
currentImage=catPics[0];
repaint();
pause(600);
}
/**
* Contains the thread execution loop.
*/
public void run() {
repaint();
double x=getSize().width;
runRight((int)x/2);
rest(4);
scratch(6);
runLeft(0);
}
/**
* Insert the method's description here.
* Creation date: (12/03/01 12:43:00)
* @param endPos int
*/
public void runLeft(int endPos) {
System.out.println("Am about to run from "+currentX+" to "+endPos);
while (currentX>endPos){
if (currentImage!=catPics[2]){
currentImage=catPics[2];
repaint();}
else{
currentImage=catPics[1];
repaint();}
currentX-=5;
pause(200);
}
currentX=endPos;
currentImage=catPics[7];
repaint();
pause(400);
}
/**
* Insert the method's description here.
* Creation date: (09/03/01 16:54:33)
*/
public void runRight(int endPos) {
System.out.println("Am about to run from "+currentX+" to "+endPos);
while (currentX<endPos){
if (currentImage!=catPics[2])
currentImage=catPics[2];
else
currentImage=catPics[1];
currentX+=5;
System.out.println("currentX = "+currentX);
if (currentX>=endPos) System.out.println("last iteration");
repaint();
pause(250);
}
currentX=endPos;
currentImage=catPics[7];
repaint();
pause(700);
}
/**
* Insert the method's description here.
* Creation date: (12/03/01 14:08:03)
* @param numScratch int
*/
public void scratch(int numScratch) {
for (int i=0; i<numScratch; ++i){
if (currentImage!=catPics[3])
currentImage=catPics[3];
else
currentImage=catPics[4];
repaint();
pause(350);
}
currentImage=catPics[7];
repaint();
pause(600);
}
/**
* Starts up the thread.
*/
public void start() {
if (thread == null){
thread = new Thread(this);
thread.start();
}
}
/**
* Terminates the thread and leaves it for garbage collection.
*/
public void stop() {
if (thread != null){
thread.stop();
thread = null;
}
}
}
Best Regards
Matt Gaunt