home *** CD-ROM | disk | FTP | other *** search
/ Kompuutteri Kaikille K-CD 2002 #14 / K-CD_2002-14.iso / Savers / rainvalley-48712.exe / support / Rain.java < prev    next >
Text File  |  2001-04-18  |  3KB  |  154 lines

  1. //###################################################################
  2. //
  3. // Confetti.java      (C)2000 amIbleeding.com
  4. //
  5. //###################################################################
  6.  
  7. import java.applet.*;
  8. import java.awt.*;
  9. public class Rain extends Applet implements Runnable
  10. {
  11.     private Thread     m_snow = null;
  12.     private Image offscreenImage;
  13.     private String m_back = "none";
  14.     private Image backdrop;
  15.     private Graphics offscreenGraphics;
  16.     private Image frame;
  17.     private int xSize;
  18.     private int ySize;
  19.     private int[] colors;
  20.     private int amount;
  21.     private Color[] cl={Color.white, Color.lightGray, Color.gray, Color.darkGray,
  22.         Color.black};
  23.     private String[] cNames={"white","lightGray","gray","darkGray",
  24.         "black"};
  25.     private int delay;
  26.     private int angle;
  27.  
  28.     public String getAppletInfo()
  29.     {
  30.         return "Name: Confetti\r\n" +
  31.                "Author: amIbleeding.com\r\n" +
  32.                "Created with Microsoft Visual J++ Version 1.1";
  33.     }
  34.     public void init()
  35.     {
  36.         String param;
  37.         colors = new int[5];
  38.         amount=0;
  39.         for (int t=0;t<5;t++) {
  40.             param = getParameter(cNames[t]);
  41.             if (param != null)
  42.                 colors[t] = Integer.parseInt(param);
  43.         }
  44.         for (int t=0;t<5;t++) {
  45.             int tmp=colors[t];
  46.             colors[t]+=amount;
  47.             amount+=tmp;
  48.         }
  49.         param = getParameter("back");
  50.         if (param != null)
  51.             m_back = param;
  52.         param = getParameter("frame");
  53.         if (param != null)
  54.             frame = getImage(getCodeBase(),param);
  55.         else
  56.             frame=null;
  57.         param = getParameter("delay");
  58.         if (param != null)
  59.             delay= Integer.parseInt(param);
  60.         else
  61.             delay=100;
  62.         param = getParameter("angle");
  63.         if (param != null)
  64.             angle= Integer.parseInt(param);
  65.         else
  66.             angle=0;
  67.         xSize=size().width;
  68.         ySize=size().height;
  69.         backdrop=getImage(getCodeBase(),m_back);
  70.         offscreenImage = createImage(xSize,ySize);
  71.         offscreenGraphics =offscreenImage.getGraphics();
  72.         System.out.println("Confetti (C)2000 amIbleeding.com");
  73.             //{{INIT_CONTROLS
  74.         //}}
  75. }
  76.     public void newimage()
  77.     {    offscreenGraphics.drawImage(backdrop,0,0,xSize,ySize,null);
  78.         offscreenGraphics.setColor(Color.white);
  79.         int onCl=0;
  80.         int x1,y1;
  81.         for(int i=0;i<amount;i++)
  82.         {
  83.             while (i>=colors[onCl]) {
  84.                 onCl++;
  85.                 offscreenGraphics.setColor(cl[onCl]);
  86.             }
  87.             x1=(int)(Math.random()*xSize);
  88.             y1=(int)(Math.random()*ySize);
  89.             offscreenGraphics.drawLine(x1,y1,x1+2*angle,y1+4);
  90.         }
  91.         if (frame != null) {
  92.             offscreenGraphics.drawImage(frame,0,0,xSize,ySize,null);
  93.         }
  94.     }
  95.     public void destroy()
  96.     {
  97.         offscreenGraphics.dispose();
  98.     }
  99.     public void paint(Graphics g)
  100.     {
  101.         g.drawImage(offscreenImage,0,0,this);
  102.     }
  103.     public void update(Graphics g)
  104.     {
  105.         paint(g);
  106.     }
  107.     public void start()
  108.     {
  109.         if (m_snow == null)
  110.         {
  111.             m_snow = new Thread(this);
  112.             m_snow.start();
  113.         }
  114.     }
  115.     public void stop()
  116.     {
  117.         if (m_snow != null)
  118.         {
  119.             m_snow.stop();
  120.             m_snow = null;
  121.         }
  122.     }
  123.     public void run()
  124.     {
  125.         MediaTracker mt = new MediaTracker(this);
  126.         mt.addImage(backdrop,0);
  127.         if (frame!=null) {
  128.             mt.addImage(frame,0);
  129.         }
  130.         try {
  131.             mt.waitForAll();
  132.         } catch (Exception e) {}
  133.         while (true)
  134.         {
  135.             try
  136.             {
  137.                 long nw=System.currentTimeMillis();
  138.                 newimage();
  139.                 repaint();
  140.                 long tm=delay-(System.currentTimeMillis()-nw);
  141.                 if (tm<10) {tm=10;}
  142.                 Thread.sleep(tm);
  143.                 newimage();
  144.             }
  145.             catch (InterruptedException e)
  146.             {
  147.                 stop();
  148.             }
  149.         }
  150.     }
  151.     //{{DECLARE_CONTROLS
  152.     //}}
  153. }
  154.