
import java.awt.*;
import java.applet.*;
import Raster;
import MyRaster;

public class Project1 extends Applet implements Runnable {
    MyRaster outputRaster;  // this is the raster that gets displayed
    Image outputImage;

    String bgFile[] = { "dilbert99124880824.gif",      // These all better
                        "dilbert98456240825.gif",      //  be the same
                        "dilbert98030416826.gif",      //  dimensions!
                        "dilbert98082281227.gif",
                        "dilbert98082182498.gif",
                        "dilbert98082915208.gif",
                        "dilbert98405550901.gif",
                        "dilbert98027037902.gif",
                        "dilbert98092027703.gif",
                        "dilbert98090162224.gif",
                        "dilbert98090513518.gif" };
    int bgFilesMax = 11;
    MyRaster currentBgRaster;
    MyRaster bgRaster[] = new MyRaster[bgFilesMax];
    
    // from http://www.dilbert.com/comics/dilbert/images/dzh_mainnav.gif
    String spriteFile[] = { "dogbert2.gif",
                            "boss.gif" };
    double spriteSpeed[] = { 10.0, 5.0 };
    static final int maxSprites = 2;
    Sprite theSprite[] = new Sprite[maxSprites];
    
    Thread Project1Clock;
    long clickNum = 0;   // for debugging

    public void init()
    {
        // set up background and output rasters
        for( int i=0; i < bgFilesMax; i++ ) {
            showStatus( "Loading backgrounds... " + (100*i/bgFilesMax)
                        + "%" );
            repaint();
            bgRaster[i] = new MyRaster( getImage(getDocumentBase(),
                                        bgFile[i] ));
        }
        
        outputRaster = new MyRaster( bgRaster[0].getWidth(),
          bgRaster[0].getHeight());

        // set up theSprites
        for( int i=0; i < maxSprites; i++ ) {
            showStatus( "Loading sprites... " + (100*i/maxSprites) + "%" );
            theSprite[i] = new Sprite( getImage(getDocumentBase(),
                                       spriteFile[i]));
            theSprite[i].SetSpeed( spriteSpeed[i] );
        }
        repaint();
        GetNewBackground( 0 );
        RenderOutputImage();
   	
	}

    public void start() {
        Project1Clock = new Thread( this );
        Project1Clock.start();
    }
    
    public void stop() {
        Project1Clock.stop();
    }
    
    public void run() {
        int bgCounter = 0;
        int a = 0;
        while( currentBgRaster == null ) {      // wait for init() to finish
            try { Project1Clock.sleep(100); }   //  loading everything first
            catch( InterruptedException e ) {}
        }                                            
        while (true) {
            try { Project1Clock.sleep(100); }
            catch( InterruptedException e ) {}
            if( bgCounter == 30 ) {             // how often should
                bgCounter = 0;                  //  background change?
                GetNewBackground( a );
                a = (a==bgFilesMax-1 ? 0 : a+1);
            } else
                bgCounter++;
            RenderOutputImage();
        }
    }

    public void GetNewBackground( int a ) {
        //showStatus( "background number " + a );
        currentBgRaster = bgRaster[a];
    }
    
    public void RenderOutputImage() { 
        // update outputRaster
        outputRaster.copy( currentBgRaster );
        for( int i=0; i < maxSprites; i++ ) {
            theSprite[i].UpdateLocation();      // sprite moves toward its
            theSprite[i].Draw( outputRaster );  //   destination
        }
        outputImage = outputRaster.toImage();
        repaint();
    }
    
    public void paint(Graphics g) {
        g.drawImage(outputImage, 0, 0, this);
    }

    public void update(Graphics g) {
        // called automatically by window manager I think
        paint( g );
    }
    
    public boolean mouseUp(Event e, int x, int y) {
        for( int i=0; i < maxSprites; i++ )
            theSprite[i].SetDestination( x, y );
        
        clickNum++;          // for debugging
        //showStatus( "(x,y)=(" + x + "," + y + ")  clickNum=" + clickNum );
        return true;
    }
		//{{DECLARE_CONTROLS
	//}}
}

