// Copyright 1996 BDM International, Inc.  This applet and associated
// documentation and files may be distributed in its entirety for
// non-commercial purposes only, provided this copyright notice is included 
// in every copy made.  All other rights reserved by the author. 

// This applet and associated documentation and files are provided "as is" 
// with no warranty of any kind, either expressed or implied, including, but
// not lmited to the implied warranties of merchantability, fitness for a
// particular purpose, and non-infringement.

// Programmer: Kelly Jo Brown

import java.awt.*;
import java.applet.*;
import java.lang.*;

public class orbit1 extends java.applet.Applet implements Runnable{
   Graphics img;
   Image screen,earth,satelite,explosion[],bar,slider,title,earea;
   int starsx[];
   int starsy[];
   int explode=0,emode=0;
   int pause=10;
   int caught=0;
   int px,py,tempx,tempy,slide=50;
   double angle=0.0,e=.5,time=0,delta_time=.1;
   Thread kicker = null;
   MediaTracker tracker;
   AudioClip crash;

   public void run() 
   {
      double rad,delta_angle,radius;
    
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
      while(kicker != null) 
      {
         if(explode>0)
         {
            explode++;
            emode++;
            if(explode==4) emode=1;
            if(explode==10)
            {
               explode=0;
               time=0;
               e=0;
               slide=0;
               pause=10;
            }
         }
         if(explode==0)
         {
            // Convert angle to radians
            rad=angle*Math.PI/180;
            // Calculate Radius
            radius=(1-e*e)/(1-e*Math.cos(rad));
            // Calculate angle delta
            delta_angle=2*Math.PI*Math.sqrt(1-e*e)/(radius*radius)*delta_time;
            // Add deltas
            time=time+delta_time;
            angle=angle+delta_angle;
            rad=angle*Math.PI/180;
            // Calculate Coordinates
            px=(int) (90*radius*Math.cos(rad));
            py=(int) (90*radius*Math.sin(rad));
            if(Math.sqrt(px*px+py*py)<15)
            {
               crash.play();
               explode=1;
               emode=0;
               pause=200;
            }
         }
         repaint();
         try {Thread.sleep(pause);} catch (InterruptedException e){};
      }
   }
	
   public void init()
   {
      int i;

      starsx=new int[100];
      starsy=new int[100];
      for(i=0;i<40;i++)
      {
         starsx[i]=(int)Math.round(Math.random()*300);
         starsy[i]=(int)Math.round(Math.random()*300);
      }
      // Allocate space for explosions
      explosion = new Image[3];
      // Load images
      earth=getImage(getDocumentBase(),"Orbit1.jav/earth.gif");
      satelite=getImage(getDocumentBase(),"Orbit1.jav/sat.gif");
      bar=getImage(getDocumentBase(),"Orbit1.jav/eslide.gif");
      title=getImage(getDocumentBase(),"Orbit1.jav/kepler1.gif");
      slider=getImage(getDocumentBase(),"Orbit1.jav/slide.gif");
      earea=getImage(getDocumentBase(),"Orbit1.jav/eccent.gif");
      explosion[0]=getImage(getDocumentBase(),"Orbit1.jav/explo0.gif");
      explosion[1]=getImage(getDocumentBase(),"Orbit1.jav/explo1.gif");
      explosion[2]=getImage(getDocumentBase(),"Orbit1.jav/explo2.gif");
      // Add tracker to see if images loaded
      tracker = new MediaTracker(this);
      tracker.addImage(earth,0);
      tracker.addImage(satelite,0);
      tracker.addImage(bar,0);
      tracker.addImage(slider,0);
      tracker.addImage(title,0);
      tracker.addImage(earea,0);
      tracker.addImage(explosion[0],0);
      tracker.addImage(explosion[1],0);
      tracker.addImage(explosion[2],0);
      // Load crash sound
      //crash=getAudioClip(getCodeBase(), "graphics/explode.au");
   }

   public void start() 
   {
      if(kicker == null) 
      {
         kicker = new Thread(this);
	kicker.start();
      }
   }

   public void stop() 
   {
      kicker = null;
   }

   public boolean mouseDown(Event evt, int x, int y)
   {
      if(x<300)
      {
         if(kicker==null)
            start();
         else
            kicker=null;
      }
      // Clicked on slidebar, slide it
      if(x>345 && x<405 && y>100 && y<201)
      {
         slide=200-y;
         e=(double)(slide/100.0);
      }
      return true;
   }

   public boolean mouseDrag(Event evt, int x, int y)
   {
      // Clicked on slidebar, slide it
      if(x>345 && x<405 && y>100 && y<201)
      {
         slide=200-y;
         e=(double)(slide/100.0);
      }
      return true;
   }

   public final synchronized void update(Graphics g) 
   {
      String str;
      int i;

      if(screen == null) 
      {
         screen = createImage(500,300);
         img = screen.getGraphics();
      }
      // Fill black background
      img.setColor(Color.black);
      img.fillRect(0,0,300,299);
      // Fill white background
      img.setColor(Color.white);
      img.fillRect(300,0,499,299);
      if(tracker.statusID(0,true)!=MediaTracker.COMPLETE)
      {
         img.drawString("Please Wait Loading Graphics",20,160);
      }
      else
      {
         // Draw title
         img.drawImage(title,302,0,this);
         // Print 
         img.setColor(Color.black);
         // Draw eccentricity name
         img.drawImage(earea,305,265,this);
         img.drawString(" "+e,415,285);
         // Draw star field
         img.setColor(Color.white);
         for(i=0;i<40;i++)
         {
            img.drawLine(starsx[i],starsy[i],starsx[i],starsy[i]);
         }
         // Draw Earth
         img.drawImage(earth,100,140,this);
         if(explode==0)
         {
            // Draw satelite
            img.drawImage(satelite,px+105,py+145,this);
         }
         else
         {
            if(explode<5)
               img.drawImage(explosion[emode],px+105,py+145,this);         
         }
         // Draw Slide Bar
         img.drawImage(bar,340,91,this);
         img.drawImage(slider,345,198-slide,this);
      }
      paint(img);
      g.drawImage(screen,0,0,null);
   }
}







