home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / FROG / FROG.EXE / frog.java < prev    next >
Encoding:
Java Source  |  1996-05-08  |  5.4 KB  |  223 lines

  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import java.net.*;
  4.  
  5. public final class frog extends java.applet.Applet implements Runnable
  6. {
  7.     int i,j,k,counter=0,flyX[],flyY[],flyOldX[],flyOldY[],flyDx[],flyDy[];
  8.     int nearX,nearY,nearNum,eatDx,eatDy;
  9.     int eatState=0;
  10.     int flyBaseX[],flyBaseY[],tongueX[]={18,22,0,0},tongueY[]={27,28,0,0};
  11.     double tongue;
  12.     boolean flyOn[]={false,false,false,false,false};
  13.     int currHead=0;
  14.     final int headList[]={0,1,2,3,2,1};
  15.     double headcount=0;
  16.     Image collection,offImage,frog,frogheads[],legs,flies[];
  17.     Graphics offGraphics;
  18.     MediaTracker tracker;
  19.     ImageFilter filter;
  20.     Thread updateThread;
  21.     Math m;
  22.  
  23.     public void init()
  24.     {
  25.         offImage=createImage(96,60);
  26.         offGraphics=offImage.getGraphics();
  27.         tracker=new MediaTracker(this);
  28.         collection = getImage(getCodeBase(),"froggies.gif");
  29.         tracker.addImage(collection,0);
  30.         try
  31.         {
  32.             tracker.waitForID(0);
  33.         }
  34.             catch(InterruptedException e) {}
  35.  
  36.         frogheads=new Image[8];
  37.         flies=new Image[3];
  38.         filter=new CropImageFilter(0,0,96,60);
  39.         frog=createImage(new FilteredImageSource(collection.getSource(),filter));
  40.         tracker.addImage(frog,1);
  41.         for (i=0;i<4;i++)
  42.             for (j=0;j<2;j++)
  43.             {
  44.                 filter=new CropImageFilter(j*48,60+i*40,48,40);
  45.                 frogheads[i*2+j]=createImage(new FilteredImageSource(
  46.                     collection.getSource(),filter));
  47.                 tracker.addImage(frogheads[i*2+j],1);
  48.             }
  49.         filter=new CropImageFilter(48,40,48,20);
  50.         legs=createImage(new FilteredImageSource(collection.getSource(),filter));
  51.         tracker.addImage(legs,1);
  52.         for (j=0;j<3;j++)
  53.         {
  54.             filter=new CropImageFilter(j*13,220,13,8);
  55.             flies[j]=createImage(new FilteredImageSource(
  56.                 collection.getSource(),filter));
  57.             tracker.addImage(flies[j],1);
  58.         }
  59.         flyX=new int[5];
  60.         flyY=new int[5];
  61.         flyOldX=new int[5];
  62.         flyOldY=new int[5];
  63.         flyDx=new int[5];
  64.         flyDy=new int[5];
  65.         flyBaseX=new int[5];
  66.         flyBaseY=new int[5];
  67.  
  68.         try
  69.         {
  70.             tracker.waitForID(1);
  71.         }
  72.             catch(InterruptedException e) {}
  73.  
  74.         resize(500,60);
  75.     }
  76.  
  77.     public void run()
  78.     {
  79.         while (updateThread !=null)
  80.         {
  81.             try
  82.             {
  83.                 updateThread.sleep(75);
  84.             } catch (InterruptedException e) {}
  85.             counter=(counter+1)% 30;
  86.             nearX=500;
  87.             nearY=0;
  88.             nearNum=-1;
  89.             for (j=0;j<5;j++)
  90.             {
  91.                 if (flyOn[j]) // Fly active?
  92.                 {
  93.                     flyOldX[j]=flyX[j];
  94.                     flyOldY[j]=flyY[j];
  95.                     if (counter==j)        // Pick new homing target
  96.                         flyBaseX[j]=(int)(100+m.random()*400);
  97.                     if (counter==(j+10))
  98.                         flyBaseY[j]=(int)(16+m.random()*19);
  99.                         
  100.                     if ((flyX[j]<flyBaseX[j])&&(flyDx[j]<11)) // Accelerate
  101.                         flyDx[j]+=2;
  102.                     else if ((flyX[j]>flyBaseX[j])&&(flyDx[j]>-11))
  103.                         flyDx[j]-=2;
  104.                     if ((flyY[j]<flyBaseY[j])&&(flyDy[j]<7))
  105.                         flyDy[j]+=2;
  106.                     else if ((flyY[j]>flyBaseY[j])&&(flyDy[j]>-7))
  107.                         flyDy[j]-=2;
  108.                     flyX[j]+=flyDx[j]; // Update location
  109.                     flyY[j]+=flyDy[j];
  110.                     if (flyX[j]<nearX) // See if this one is nearest to frog
  111.                     {
  112.                         nearX=flyX[j];
  113.                         nearY=flyY[j];
  114.                         nearNum=j;
  115.                     }
  116.                 }
  117.                 else // Fly inactive?
  118.                 {
  119.                     if (m.random()<0.01) // Start new one?
  120.                     {
  121.                         flyX[j]=500;
  122.                         flyY[j]=25;
  123.                         flyBaseY[j]=25;
  124.                         flyDx[j]=0;
  125.                         flyDy[j]=0;
  126.                         flyOn[j]=true;
  127.                     }
  128.                 }
  129.             }
  130.             switch (eatState)
  131.             {
  132.                 case 0: // Normal
  133.                     headcount+=0.003*(700-nearX); // Breathe faster when fly is close
  134.                     if (headcount>12)
  135.                         headcount-=6;
  136.                     currHead=headList[((int)headcount)% 6];
  137.  
  138.                     if ((nearX>96)&&(nearX<130)&&(nearY<40)&&(nearY>6)) // Catch?
  139.                     {
  140.                         eatState=1;
  141.                         eatDx=nearX-59;
  142.                         eatDy=nearY-22;
  143.                         tongue=1;
  144.                         flyOn[nearNum]=false;
  145.                         currHead=4;
  146.                     }
  147.                     break;
  148.                 case 1: // Tongue out
  149.                     tongue=tongue-0.3;
  150.                     if (tongue<0)
  151.                     {
  152.                         eatState=2;
  153.                         headcount=0;
  154.                         currHead=5;
  155.                     }
  156.                     break;
  157.                 case 2: // Swallowing
  158.                     headcount+=0.1;
  159.                     if (headcount<0.6)
  160.                         currHead=5;
  161.                     else if (headcount<3.3)
  162.                         currHead=6+(((int)(headcount*2))&1);
  163.                     else if (headcount<4.4)
  164.                         currHead=5;
  165.                     else
  166.                         eatState=0;
  167.                     break;
  168.                 default:
  169.                     break;
  170.             }
  171.             repaint();
  172.         }
  173.     }
  174.  
  175.     public void start()
  176.     {
  177.         if (updateThread==null)
  178.         {
  179.             updateThread=new Thread(this,"Flies");
  180.             updateThread.start();
  181.         }
  182.     }
  183.  
  184.     public void stop()
  185.     {
  186.         if ((updateThread!=null)&&(updateThread.isAlive()))
  187.         {
  188.             updateThread.stop();
  189.         }
  190.         updateThread=null;
  191.     }
  192.  
  193.     public void paint(Graphics g)
  194.     {
  195.         g.drawImage(frog,0,0,this);
  196.     }
  197.  
  198.     public void update(Graphics g)
  199.     {
  200.             offGraphics.setColor(Color.lightGray);
  201.             offGraphics.fillRect(0,0,96,60);
  202.             offGraphics.drawImage(legs,0,40,this);
  203.             offGraphics.drawImage(frogheads[currHead],0,0,this);
  204.             if (eatState==1) // Draw tongue and fly at the end of it
  205.             {
  206.                 offGraphics.setColor(Color.gray);
  207.                 tongueX[2]=(int)(18+tongue*eatDx);
  208.                 tongueY[2]=(int)(30+tongue*eatDy);
  209.                 tongueX[3]=tongueX[2];
  210.                 tongueY[3]=(int)(28+tongue*eatDy);
  211.                 offGraphics.fillPolygon(tongueX,tongueY,4);
  212.                 offGraphics.drawPolygon(tongueX,tongueY,4);
  213.                 offGraphics.drawImage(flies[2],(int)(11+tongue*eatDx),(int)(25+tongue*eatDy),this);
  214.             }
  215.             g.setColor(Color.lightGray); // Clear rest of background
  216.             g.fillRect(144,0,356,60);
  217.             g.drawImage(offImage,48,0,this);
  218.             for (k=0;k<5;k++) // Draw flies
  219.                 if (flyOn[k])
  220.                     g.drawImage(flies[counter & 1],flyX[k],flyY[k],this);
  221.     }
  222. }
  223.