home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April A / Pcwk4a98.iso / Lotus / Beanmach / DATA1.CAB / Application_Files / beans / Fireworks.java < prev    next >
Text File  |  1997-11-05  |  5KB  |  233 lines

  1. //------------------------------------------------------------------------------
  2. // BeanMachine IBM copyright 1996, 1997
  3. //
  4. // Class: Fireworks
  5. //
  6. // Description: 
  7. //  A sample class that illustrates how to import Java applets
  8. //  for the BeanMachine Palette.
  9. //
  10. //------------------------------------------------------------------------------
  11.  
  12. import java.applet.*;
  13. import java.awt.*;
  14. import java.lang.*;
  15. import java.util.*;
  16. import java.net.*;
  17.  
  18. public class Fireworks extends Applet implements Runnable
  19. {
  20.   public int speed;
  21.  
  22.   private int BURSTS;
  23.   private int ENERGY;
  24.   private int LINES;
  25.   private int LENGTH;
  26.  
  27.   private int w,h;
  28.   private Thread thread=null;
  29.   private Color color[];
  30.   private boolean finished[];
  31.   private int   energy[];
  32.   private int   lines[];
  33.   private int   length[];
  34.   private int   steps[];
  35.   private Point pt[];
  36.   private Point origin[];
  37.   private int[] vx[];
  38.   private int[] vy[];
  39.  
  40.     private static String parameterInfo[][] = {
  41.                                                                 {"speed",    "1-100",    "How fast the fireworks go"}
  42.                                                             };    
  43.  
  44.   public void init()
  45.   {
  46.     int i;
  47.     String p;
  48.  
  49.     p=getParameter("speed");
  50.     speed=(p==null) ? 10:Integer.valueOf(p).intValue();
  51.  
  52.     w=getSize().width;
  53.     h=getSize().height;
  54.     BURSTS = (int)(Math.random()*3) + 1;
  55.     ENERGY = 400 + (int)(Math.random()*100);
  56.     LINES = (int)(Math.random()*20) + 30;
  57.     LENGTH = w/3;
  58.  
  59.     color = new Color[BURSTS];
  60.     energy = new int[BURSTS];
  61.     lines = new int[BURSTS];
  62.     length = new int[BURSTS];
  63.     finished = new boolean[BURSTS];
  64.     steps = new int[BURSTS];
  65.     pt = new Point[BURSTS];
  66.     origin = new Point[BURSTS];
  67.     vx = new int[LINES][BURSTS];
  68.     vy = new int[LINES][BURSTS];
  69.  
  70.     for(i=0;i<BURSTS;i++)
  71.     {
  72.       finished[i] = false;
  73.     }
  74.  
  75.   }
  76.  
  77.   public void start() 
  78.   {
  79.     if(thread==null)
  80.     {
  81.       thread=new Thread(this);
  82.       thread.start();
  83.     }
  84.    }
  85.  
  86.   public void stop()
  87.   {
  88.     if(thread!=null)
  89.     {
  90.       thread.stop();
  91.       thread=null;
  92.     }
  93.   }
  94.  
  95.   public void run()
  96.   {
  97.     int i;
  98.     Graphics g=getGraphics();
  99.  
  100.     while(true)
  101.     {
  102.       try
  103.       {
  104.         thread.sleep(1000/speed);
  105.       } 
  106.       catch(InterruptedException x)
  107.       {
  108.         // do nothing
  109.       }
  110.  
  111.       for(i=0;i<BURSTS;i++)
  112.       {
  113.         if (!finished[i])
  114.         {
  115.           startBurst(i);
  116.         }
  117.         finishBurst(i, g);
  118.       }
  119.     }
  120.   }
  121.  
  122.   public void paint(Graphics g)
  123.   {
  124.     g.setColor(Color.black);
  125.     g.fillRect(0,0,w,h);
  126.   }
  127.  
  128.   public void startBurst(int i)
  129.   {
  130.     long seed=(long)(Math.random()*10000);
  131.     int j;
  132.     int loss;
  133.  
  134.     energy[i]=(int)(Math.random()*ENERGY);
  135.     lines[i]=(int)(Math.random()*LINES);
  136.     length[i]=(int)(Math.random()*LENGTH);
  137.  
  138.     vx[i]=new int[lines[i]];
  139.     vy[i]=new int[lines[i]];
  140.  
  141.     int red=(int)(Math.random()*128)+127;
  142.     int blue=(int)(Math.random()*128)+127;
  143.     int green=(int)(Math.random()*128)+127;
  144.  
  145.     color[i] = new Color(red, green, blue);
  146.   
  147.     int ox = (int)(Math.random()*w/2)+w/4;
  148.     int oy = (int)(Math.random()*h/2)+h/4;
  149.     origin[i] = new Point(ox, oy);
  150.  
  151.     for(j=0;j<lines[i];j++)
  152.     {
  153.       loss = energy[i] / 4;
  154.       vx[i][j]=(int)(Math.random()*energy[i]) - (loss*2);
  155.       vy[i][j]=(int)(Math.random()*loss*3);
  156.     }
  157.     steps[i]=0;
  158.     finished[i]=true;
  159.   }
  160.  
  161.   public void finishBurst(int i, Graphics g)
  162.   {
  163.     Point p, o;
  164.     if (finished[i])
  165.       if (steps[i]<length[i])
  166.       {
  167.         int j,c;
  168.         double step;
  169.         int red, green, blue;
  170.     
  171.         // make the color closer to white
  172.         red = (int)(Math.random()*64) + color[i].getRed() - 32;
  173.         blue = (int)(Math.random()*64) + color[i].getBlue() - 32;
  174.         green = (int)(Math.random()*64) + color[i].getGreen() - 32;
  175.         if ((red > 0 && red < 255) &&
  176.             (blue > 0 && blue < 255) &&
  177.             (green > 0 && green < 255))
  178.           color[i] = new Color(red, green, blue);
  179.  
  180.         int len = length[i]/2;
  181.         for(j=0;j<lines[i];j++)
  182.         {
  183.           p = getNextPoint(i,j, steps[i]);
  184.           o = origin[i];
  185.           g.setColor(color[i]);
  186.           g.drawLine(o.x+p.x, o.y-p.y, o.x+p.x, o.y-p.y);
  187.  
  188.  
  189.           // At the halfway point, redraw in black to make the line disappear
  190.           if (steps[i]>=len)
  191.           {
  192.               step = ((steps[i]-len)*2);
  193.               p = getNextPoint(i,j,step);
  194.               g.setColor(Color.black);
  195.               g.drawLine(o.x+p.x, o.y-p.y, o.x+p.x, o.y-p.y);
  196.  
  197.               step = step + 1;
  198.               p = getNextPoint(i,j,step);
  199.               g.setColor(Color.black);
  200.               g.drawLine(o.x+p.x, o.y-p.y, o.x+p.x, o.y-p.y);
  201.           }
  202.         }
  203.         steps[i]++;
  204.       }
  205.       else
  206.       {
  207.         finished[i]=false;
  208.       }
  209.   }
  210.  
  211.   private Point getNextPoint(int i, int j, double step)
  212.   {
  213.     double s = step/LENGTH;
  214.     double gravity = LINES * s * s;
  215.     int x =(int)(vx[i][j]*s);
  216.     int y =(int)(vy[i][j]*s-gravity);
  217.     pt[i] = new Point(x, y);
  218.     return pt[i];
  219.   }
  220.  
  221.   public Dimension getPreferredSize() 
  222.   {
  223.     return (new Dimension(100, 100));
  224.   }
  225.  
  226.     public String[][] getParameterInfo()
  227.     {
  228.         return parameterInfo;
  229.     }
  230.  
  231.  
  232. }
  233.