/** Use JWS to modify MyApplet15.java (previously Neko.java 
 ** on pp. 236-238 of the text) so that Neko has a more unusual 
 ** (weird?) behavior on a more interesting background image or
 ** color chosen by you. Note that since the Neko images aren't
 ** nicely cropped, they carry a white square with them. So, one
 ** possibility would be to create a maze with a colored background
 ** amd white paths. Also, edit MyApplet15.html in the project 
 ** directory, both to run the MyApplet15.class file you create,
 ** and so that everything looks nice and works properly.        **/

import java.awt.*;
import java.awt.Image;
import java.awt.Color;

public class MyApplet15 extends java.applet.Applet
    implements Runnable {

   Image currentimg;
    Thread runner;
    int xpos;
    int ypos = 50;
    Dimension preImageDim;               // Define preImage variables needed to
    Image preImage;                      // create the offscreen copy of the image
    Graphics preImageGraphics; 

    public void init() {
       setBackground(Color.white);
        String nekosrc[] = { "right1.gif", "right2.gif",
            "stop.gif", "yawn.gif", "scratch1.gif",
            "scratch2.gif","sleep1.gif", "sleep2.gif",
            "awake.gif", "stars.gif" };

        for (int i=0; i < nekopics.length; i++) {
            nekopics[i] = getImage(getCodeBase(),
            "images/" + nekosrc[i]);
        }
    }

    public void start() {
        if (runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }
    
    public void stop() {
        if (runner != null) {
            runner.stop();
            runner = null;
        }
    }

    public void run() {
       

        // run from one side of the screen to the middle
        nekorun(0, size().width / 2);

        // stop and pause
        currentimg = nekopics[2];
        repaint();
        pause(1000);

        // yawn
        currentimg = nekopics[3];
        repaint();
        pause(1000);

        // scratch four times
        nekoscratch(4);

        // sleep for 5 "turns"
        nekosleep(5);

        // wake up and run off
        currentimg = nekopics[8];
        repaint();
        pause(500);
        nekorun(xpos, size().width + 10);
    }

    void nekorun(int start, int end) {
        for (int i = start; i < end; i += 10) {
            xpos = i;
            // swap images
            if (currentimg == nekopics[0])
                currentimg = nekopics[1];
            else currentimg = nekopics[0];
                repaint();
            pause(150);
        }
    }


    void nekoscratch(int numtimes) {
        for (int i = numtimes; i > 0; i--) {
            currentimg = nekopics[4];
            repaint();
            pause(150);
            currentimg = nekopics[5];
            repaint();
            pause(150);
        }
    }

    void nekosleep(int numtimes) {
        for (int i = numtimes; i > 0; i--) {
            currentimg = nekopics[6];
            repaint();
            pause(250);
            currentimg = nekopics[7];
            repaint();
            pause(250);
        }
    }

    void pause(int time) {
        try { Thread.sleep(time); }
        catch (InterruptedException e) { }
    }
 public void paint(Graphics g)         // This technique of painting by calling
     {update(g);}            // update, prevents the default (and hidden)  
					  // behavior of update, and instead allows
    public void update(Graphics g)   
 
	 {Dimension d = size();
	 if ((preImageGraphics == null) ||               // When all images are loaded, 
	    (d.width != preImageDim.width) ||           // compare the current applet
	    (d.height != preImageDim.height))           // viewing size to our preImage,
	     {preImageDim = d;                          // and if different, resize our 
	      preImage = createImage(d.width,d.height); // next preImage.
	      preImageGraphics = preImage.getGraphics();
	      }                                     
	preImageGraphics.setColor(getBackground());      // Clear the previous preImage
	preImageGraphics.fillRect(0,0,d.width,d.height); // by filling with t
	 int starWidth = nekopics[9].getWidth(this);   // Get rocketship image 
	int starHeight = nekopics[9].getHeight(this); 
        if (currentimg != null)
	 {for (int i = 0; i <= 15; i++)  
	  //g.drawImage(nekopics[9], 0, 0, this);
	    {preImageGraphics.drawImage(nekopics[9], i*starWidth, 0, this);
	     preImageGraphics.drawImage(nekopics[9], i*starWidth, 2*starHeight,this);
	     preImageGraphics.drawImage(nekopics[9], i*starWidth, 3*starHeight,this);
	     preImageGraphics.drawImage(nekopics[9], i*starWidth, 4*starHeight,this);
	     preImageGraphics.drawImage(nekopics[9], i*starWidth, 5*starHeight,this);
	     preImageGraphics.drawImage(nekopics[9], i*starWidth, 6*starHeight,this);
	     preImageGraphics.drawImage(nekopics[9], i*starWidth, 7*starHeight,this);
	     preImageGraphics.drawImage(nekopics[9], i*starWidth, 8*starHeight,this);
	     preImageGraphics.drawImage(nekopics[9], i*starWidth, 9*starHeight,this);}
           preImageGraphics.drawImage(currentimg, xpos, ypos, this);
	    g.drawImage(preImage,0,0,this);
    }}
}
