home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-04-04 | 2.6 KB | 102 lines |
- /*
- * @(#)Rain.java 1.0 1999/04/03
- *
- * Copyright (c) 1999, David Griffiths. All Rights Reserved.
- *
- * This software is the proprietary information of David Griffiths.
- * This source code may not be published or redistributed without the
- * express permission of the author.
- *
- * THE AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
- * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- */
-
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Color;
- import java.lang.Math;
- import java.awt.*;
-
- public class Rain extends java.applet.Applet implements Runnable {
- Image origImg, backImg;
- int w1, h1;
- Thread thrRain;
-
- public void start () {
- if (thrRain == null) {
- thrRain = new Thread (this);
- thrRain.start ();
- }
- }
-
- public void stop () {
- if (thrRain != null) {
- thrRain.stop ();
- thrRain = null;
- }
- }
-
- public void run () {
- loadImage ();
- backImg = createImage (w1, h1);
- Graphics backG = backImg.getGraphics();
- backG.setColor(Color.white);
- backG.drawImage(origImg, 0, 0, this);
- int a = (int)(Math.random() * (double)w1);
- int b = 0;
- while (thrRain != null) {
- smudge (backG, a, b);
- a += (int) (Math.random() * 7.0) - 3;
- b += SMUDGE_HEIGHT;
- if (b > h1) {
- a = (int)(Math.random() * (double)w1);
- b = 0;
- }
- try {
- Thread.sleep (50);
- }
- catch (InterruptedException e) {}
- repaint ();
- }
- }
-
- public void loadImage () {
- MediaTracker tracker = new MediaTracker(this);
- String nextImage = getParameter ("image");
- origImg = getImage (getDocumentBase(), nextImage);
-
- tracker.addImage(origImg, 0);
-
- try {
- tracker.waitForID(0);
- }
- catch(InterruptedException e) {}
-
- w1 = origImg.getWidth (this);
- h1 = origImg.getHeight (this);
- }
-
- private final static int SMUDGE_WIDTH = 15;
- private final static int SMUDGE_HEIGHT = 15;
-
- public void smudge(Graphics g, int a, int b) {
- int ditherX = (int)(Math.random() * (double)SMUDGE_WIDTH / 4.0) - (SMUDGE_WIDTH / 8);
- int ditherY = (int)(Math.random() * (double)SMUDGE_HEIGHT / 4.0) - (SMUDGE_HEIGHT / 8);
- g.copyArea (a, b, SMUDGE_WIDTH, SMUDGE_HEIGHT, ditherX, ditherY);
- }
-
- public void update (Graphics g) {
- paint (g);
- }
-
- public void paint (Graphics g) {
- if (backImg != null)
- g.drawImage (backImg, 0, 0, this);
- }
- }
-