home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue153 / java / copyjava.exe / Rain.java < prev    next >
Encoding:
Java Source  |  1999-04-04  |  2.6 KB  |  102 lines

  1. /*
  2.  * @(#)Rain.java    1.0 1999/04/03
  3.  * 
  4.  * Copyright (c) 1999, David Griffiths. All Rights Reserved.
  5.  * 
  6.  * This software is the proprietary information of David Griffiths.
  7.  * This source code may not be published or redistributed without the 
  8.  * express permission of the author. 
  9.  * 
  10.  * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
  11.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  12.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  13.  * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
  14.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  15.  * THIS SOFTWARE OR ITS DERIVATIVES.
  16.  * 
  17.  */
  18.  
  19. import java.awt.Graphics;
  20. import java.awt.Image;
  21. import java.awt.Color;
  22. import java.lang.Math;
  23. import java.awt.*;
  24.  
  25. public class Rain extends java.applet.Applet implements Runnable {
  26.     Image origImg, backImg;
  27.     int w1, h1;
  28.     Thread thrRain;
  29.  
  30.     public void start () {
  31.         if (thrRain == null) {
  32.             thrRain = new Thread (this);
  33.             thrRain.start ();
  34.         }
  35.     }
  36.  
  37.     public void stop () {
  38.         if (thrRain != null) {
  39.             thrRain.stop ();
  40.             thrRain = null;
  41.         }
  42.     }
  43.  
  44.     public void run () {
  45.         loadImage ();
  46.         backImg = createImage (w1, h1);
  47.         Graphics backG = backImg.getGraphics();
  48.         backG.setColor(Color.white);
  49.         backG.drawImage(origImg, 0, 0, this);
  50.         int a = (int)(Math.random() * (double)w1);
  51.         int b = 0;
  52.         while (thrRain != null) {
  53.             smudge (backG, a, b);
  54.             a += (int) (Math.random() * 7.0) - 3;
  55.             b += SMUDGE_HEIGHT;
  56.             if (b > h1) {
  57.                 a = (int)(Math.random() * (double)w1);
  58.                 b = 0;
  59.             }
  60.             try {
  61.                  Thread.sleep (50);
  62.             }
  63.             catch (InterruptedException e) {}
  64.             repaint ();
  65.         }
  66.     }
  67.  
  68.     public void loadImage () {
  69.         MediaTracker tracker = new MediaTracker(this);
  70.         String nextImage = getParameter ("image");
  71.         origImg = getImage (getDocumentBase(), nextImage);
  72.  
  73.         tracker.addImage(origImg, 0);
  74.  
  75.         try {
  76.             tracker.waitForID(0);
  77.         }
  78.         catch(InterruptedException e) {}
  79.  
  80.         w1 = origImg.getWidth (this);
  81.         h1 = origImg.getHeight (this);
  82.     }
  83.  
  84.     private final static int SMUDGE_WIDTH = 15;
  85.     private final static int SMUDGE_HEIGHT = 15;
  86.  
  87.     public void smudge(Graphics g, int a, int b) {
  88.         int ditherX = (int)(Math.random() * (double)SMUDGE_WIDTH / 4.0) - (SMUDGE_WIDTH / 8);
  89.         int ditherY = (int)(Math.random() * (double)SMUDGE_HEIGHT / 4.0) - (SMUDGE_HEIGHT / 8);
  90.         g.copyArea (a, b, SMUDGE_WIDTH, SMUDGE_HEIGHT, ditherX, ditherY);
  91.     }
  92.  
  93.     public void update (Graphics g) {
  94.         paint (g);
  95.     }
  96.  
  97.     public void paint (Graphics g) {
  98.         if (backImg != null)
  99.             g.drawImage (backImg, 0, 0, this); 
  100.     }
  101. }
  102.