//Brad Culberson 04.23.98
//this is printing images, in an animation with scrolling text.

import java.awt.*;
import java.applet.Applet;

public class MyApplet14 extends Applet implements Runnable
   {int frameNumber = -1;                
    int delay;                          
    Thread animatorThread;
    boolean frozen = false;
    Font waitfont=new Font("TimesRoman",Font.BOLD,14);
    FontMetrics waitfontm = getFontMetrics(waitfont);
    String waitstr1, waitstr2 = "";
    Font shipfont=new Font("TimesRoman",Font.PLAIN,10);
    FontMetrics shipfontm = getFontMetrics(shipfont);
    String shipstr = "";
    String shipstrpad = "                                                    ";
    Font titlefont=new Font("TimesRoman",Font.BOLD,24);
    FontMetrics titlefontm = getFontMetrics(titlefont);
    String titlestr = "";
    Color starblue = new Color(0,100,255);
    Dimension preImageDim;            
    Image preImage;                 
    Graphics preImageGraphics;         

    Image backgroundstarImage[];                    
    Image AndromedaImage;
    Image MilkywayHydrogenImage;
    Image PleiadesClusterImage;
    Image borgcubeImage;              
    Image rocketshipImage;
    Image earthImage;
    MediaTracker tracker;                
    int backNumber = 0;
    int shipstrIndex = 0;
    int xspeedUp = 0;
    int yspeedUp = 0;



    public void init()
       {setBackground(Color.black);
	String str;
        int fps = 10;                   
        str = getParameter("fps");     
        try                                
         {if (str != null)
           {fps = Integer.parseInt(str);}  
          }                                
        catch (Exception e) {}           
        delay = (fps > 0) ? (1000/fps): 100; 
                                             	     
	backgroundstarImage = new Image[4];
        tracker = new MediaTracker(this);  
	AndromedaImage = getImage(getCodeBase(),"images/am5hst.jpg");
	tracker.addImage(AndromedaImage,0);
	MilkywayHydrogenImage = getImage(getCodeBase(),"images/nHI_alt_skyview.jpg");
	tracker.addImage(MilkywayHydrogenImage,0);
	PleiadesClusterImage = getImage(getCodeBase(),"images/pleiades2.gif");
	tracker.addImage(PleiadesClusterImage,0);
	earthImage = getImage(getCodeBase(),"images/earth_a17.gif");
	tracker.addImage(earthImage,0);
	
	backgroundstarImage[0] = AndromedaImage;
	tracker.addImage(backgroundstarImage[0],0);
	backgroundstarImage[1] = MilkywayHydrogenImage;
	tracker.addImage(backgroundstarImage[1],0);
	backgroundstarImage[2] = PleiadesClusterImage;
	tracker.addImage(backgroundstarImage[2],0);
	backgroundstarImage[3] = earthImage;
	tracker.addImage(backgroundstarImage[3],0);
	rocketshipImage = getImage(getCodeBase(),"images/rocketship.gif");
	tracker.addImage(rocketshipImage,0);
	}  

    public boolean mouseDown(Event e, int x, int y)
       {if (frozen)
         {frozen = false;
          start();}
        else
         {frozen = true;
          animatorThread = null;        
	  }                              
        return true;                      
        }                              

    public void start()
       {if (frozen) { }                   
        else                            
         {if (animatorThread == null)     
           {animatorThread = new Thread(this);} 
          animatorThread.start();               
          }                                    
        }

    public void run()
       {try                                            
	 {tracker.waitForAll();}                        
        catch (InterruptedException e) {}              
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        long startTime = System.currentTimeMillis();            
        while (Thread.currentThread() == animatorThread)      
           {frameNumber++;                              
            repaint();                                   
            try                                        
             {startTime += delay;
              Thread.sleep(Math.max(0,
                                    startTime-System.currentTimeMillis()));
              }
            catch (InterruptedException e) {break;}     
            }                                           
        }                                             



    
    public void paint(Graphics g)      
     {if (!frozen) update(g);}         
					
    public void update(Graphics g)        
     {Dimension d = size();            
      if (!tracker.checkAll())                       
       {g.clearRect(0, 0, d.width, d.height);       
	g.setColor(starblue);
	g.setFont(waitfont);  
	waitstr1 = "Loading images, please wait...  Depending upon your computer ";
	waitstr2 = "and/or your connection speed, this may take several minutes...";
	g.drawString(waitstr1,                                    
	             (d.width - waitfontm.stringWidth(waitstr1))/2, 
		      d.height/2);                                
        g.drawString(waitstr2,                                    
	             (d.width - waitfontm.stringWidth(waitstr2))/2, 
		      (d.height/2) + waitfontm.getHeight());                                
	}
      else	       	               
       {if ((preImageGraphics == null) ||             
	    (d.width != preImageDim.width) ||          
	    (d.height != preImageDim.height))         
	     {preImageDim = d;                         
	      preImage = createImage(d.width,d.height); 
	      preImageGraphics = preImage.getGraphics();
	      }                                     
	preImageGraphics.setColor(getBackground());    
	preImageGraphics.fillRect(0,0,d.width,d.height); 
							
	int rocketshipWidth = rocketshipImage.getWidth(this);   
	int rocketshipHeight = rocketshipImage.getHeight(this); 
	int xscrollDistance = d.width + rocketshipWidth;                         
	int yscrollDistance = d.height + rocketshipHeight;  
	int xscrollPosition = 0;
	int yscrollPosition = 0;
    
	preImageGraphics.drawImage(                          
	   backgroundstarImage[backNumber],                 
	   0,0,d.width,d.height,Color.black,this);           
							 
	preImageGraphics.setFont(titlefont);                  
	preImageGraphics.setColor(starblue);
		
	   
	if (backNumber == 0)                                  
	  {preImageGraphics.drawString(
	   "Lost in Space ?  ",
	   d.width - titlefontm.stringWidth("Lost in Space ?  "),
	   titlefontm.getAscent());	
	   yscrollPosition = (frameNumber % yscrollDistance) - rocketshipHeight;
	   if (yscrollPosition >= d.height/2) xspeedUp++;     
	   xscrollPosition = 
	      (frameNumber % xscrollDistance) - rocketshipWidth + xspeedUp*50;
	   preImageGraphics.drawImage(
	      rocketshipImage,
	      xscrollPosition,
	      yscrollPosition,this);	
	   preImageGraphics.setFont(shipfont);
	   shipstr = "          Hmmm..., looks like Andromeda... Let's warp in and check it out !";
	   shipstr += shipstrpad + shipstrpad;                  
	   if (yscrollPosition > 0)
	    {preImageGraphics.drawString(
	     shipstr.substring(shipstrIndex,shipstrIndex + 11), 
	     xscrollPosition + 27,                              
	     yscrollPosition + rocketshipWidth/2);	
	     if (frameNumber % 2 == 0) shipstrIndex++;          
	     if (shipstrIndex + 11 == shipstr.length()) shipstrIndex = 0; 
	     }	                                                          
	   if (xscrollPosition >= d.width)                     
	    {frameNumber = -1; 
	     backNumber = 1;
	     shipstrIndex = 0;
	     xspeedUp = 0;
	     }
	   }
	else if (backNumber == 1)                              
	 {preImageGraphics.drawString(
	   "Lost in Space ?  ",
	   d.width - titlefontm.stringWidth("Lost in Space ?  "),
	   titlefontm.getAscent());	
	   yscrollPosition = 3*(d.height/4) - (frameNumber % yscrollDistance);
	  if (yscrollPosition <= .1*d.height) xspeedUp++;
	  xscrollPosition = 
	        (frameNumber % xscrollDistance) - rocketshipWidth + xspeedUp*50;
	  preImageGraphics.drawImage(
	      rocketshipImage,
	      xscrollPosition,
	      yscrollPosition,this);	
	  preImageGraphics.setFont(shipfont);
	  shipstr = "          Where am I ?? This looks like a Hydrogen-only view of the Milky Way... Bye !";
	  shipstr += shipstrpad + shipstrpad;
	  if (yscrollPosition < .62*d.height)
	   {preImageGraphics.drawString(
	    shipstr.substring(shipstrIndex,shipstrIndex + 11),
	    xscrollPosition + 27,
	    yscrollPosition + rocketshipWidth/2);	
	    if (frameNumber % 2 == 0) shipstrIndex++;
	    if (shipstrIndex + 11 == shipstr.length()) shipstrIndex = 0; 
	    }	
	  if (xscrollPosition >= d.width)                     
	   {frameNumber = -1; 
	    backNumber = 2;
	    shipstrIndex = 0;
	    xspeedUp = 0;
	    } 
	  } 
	else if (backNumber == 2)                             
	 {preImageGraphics.drawString(
	   "Lost in Space ?  ",
	   d.width - titlefontm.stringWidth("Lost in Space ?  "),
	   titlefontm.getAscent());	
	   xscrollPosition = 
	        (frameNumber % xscrollDistance) - rocketshipWidth + xspeedUp*50;
	  yscrollPosition = d.height/2 + yspeedUp*30;
	  preImageGraphics.drawImage(
	      rocketshipImage,
	      xscrollPosition,
	      yscrollPosition,this);	
	  preImageGraphics.setFont(shipfont);
	  shipstr = "          The Pleiades Cluster ?? With HyperDrive you never know... Uh Oh !";
	  shipstr += shipstrpad + shipstrpad;
	  if (xscrollPosition > 0)
	   {preImageGraphics.drawString(
	    shipstr.substring(shipstrIndex,shipstrIndex + 11),
	    xscrollPosition + 27,
	    yscrollPosition + rocketshipWidth/2);	
	    if (frameNumber % 2 == 0) shipstrIndex++;
	    if (shipstrIndex + 11 == shipstr.length()) shipstrIndex = 0; 
	    }	
	  if (xscrollPosition >= d.width/3) 
	   {xspeedUp++; 
	    yspeedUp++; 
	    }
	  if (xscrollPosition >= d.width)                      
	   {frameNumber = -1; 
	    backNumber = 3;
	    shipstrIndex = 0;
	    xspeedUp = 0;
	    yspeedUp = 0;
	    }
	  } 
	  else if (backNumber == 3)                              
	 {preImageGraphics.drawString(
	   "We're not Lost !  ",
	   d.width - titlefontm.stringWidth("We're not Lost !  "),
	   titlefontm.getAscent());	
      yscrollPosition = 3*(d.height/4) - (frameNumber % yscrollDistance);
	  if (yscrollPosition <= .1*d.height) xspeedUp++;
	  xscrollPosition = 
        (frameNumber % xscrollDistance) - rocketshipWidth + xspeedUp*50;

	 preImageGraphics.drawImage(
	      rocketshipImage,
	      xscrollPosition,
	      yscrollPosition,this);	
	  preImageGraphics.setFont(shipfont);
	  shipstr = "      Warp kicks !!! Let's go find that hot chick 7 of 9 ...";
	  shipstr += shipstrpad + shipstrpad;
	  if (xscrollPosition > 0)
	   {preImageGraphics.drawString(
	    shipstr.substring(shipstrIndex,shipstrIndex + 11),
	    xscrollPosition + 27,
	    yscrollPosition + rocketshipWidth/2);	
	    if (frameNumber % 2 == 0) shipstrIndex++;
	    if (shipstrIndex + 11 == shipstr.length()) shipstrIndex = 0; 
	    }	
	  if (xscrollPosition >= d.width/2) 
	   {xspeedUp++; 
	    yspeedUp = yspeedUp-2; 
	    }
	  if (xscrollPosition >= d.width)                 
	   {frameNumber = -1; 
	    backNumber = 0;
	    shipstrIndex = 0;
	    xspeedUp = 0;
	    yspeedUp = 0;
	    }
	  } 					                                                  
							
	g.drawImage(preImage,0,0,this);	                
	} 
      } 

    public void stop()
      {animatorThread = null;   
       preImageGraphics = null;  
       preImage = null;
       }	
    }