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

  1. /*
  2.  * @(#)Boil.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 Boil extends java.applet.Applet implements Runnable {
  26.     Image origImg, backImg;
  27.     int w1, h1;
  28.     Thread thrBoil;
  29.  
  30.     public void start () {
  31.         if (thrBoil == null) {
  32.             thrBoil = new Thread (this);
  33.             thrBoil.start ();
  34.         }
  35.     }
  36.  
  37.     public void stop () {
  38.         if (thrBoil != null) {
  39.             thrBoil.stop ();
  40.             thrBoil = null;
  41.         }
  42.     }
  43.  
  44.     public void run () {
  45.         loadImage ();
  46.         int a, b, temp;
  47.         backImg = createImage (w1, h1);
  48.         Graphics backG = backImg.getGraphics();
  49.         backG.setColor(Color.white);
  50.         backG.drawImage(origImg, 0, 0, this);
  51.         while (thrBoil != null) {
  52.             a = (int)(Math.random() * (double)w1);
  53.             b = (int)(Math.random() * (double)h1);
  54.             smudge (backG, a, b);
  55.             try {
  56.                  Thread.sleep (50);
  57.             }
  58.             catch (InterruptedException e) {}
  59.             repaint ();
  60.         }
  61.     }
  62.  
  63.     public void loadImage () {
  64.         MediaTracker tracker = new MediaTracker(this);
  65.         String nextImage = getParameter ("image");
  66.         origImg = getImage (getDocumentBase(), nextImage);
  67.  
  68.         tracker.addImage(origImg, 0);
  69.  
  70.         try {
  71.             tracker.waitForID(0);
  72.         }
  73.         catch(InterruptedException e) {}
  74.  
  75.         w1 = origImg.getWidth (this);
  76.         h1 = origImg.getHeight (this);
  77.     }
  78.  
  79.     private final static int SMUDGE_WIDTH = 40;
  80.     private final static int SMUDGE_HEIGHT = 40;
  81.  
  82.     public void smudge(Graphics g, int a, int b) {
  83.         int ditherX = (int)(Math.random() * (double)SMUDGE_WIDTH / 10.0) - (SMUDGE_WIDTH / 20);
  84.         int ditherY = (int)(Math.random() * (double)SMUDGE_HEIGHT / 10.0) - (SMUDGE_HEIGHT / 20);
  85.         g.copyArea (a, b, SMUDGE_WIDTH, SMUDGE_HEIGHT, ditherX, ditherY);
  86.     }
  87.  
  88.     public void update (Graphics g) {
  89.         paint (g);
  90.     }
  91.  
  92.     public void paint (Graphics g) {
  93.         if (backImg != null)
  94.             g.drawImage (backImg, 0, 0, this); 
  95.     }
  96. }
  97.