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

  1. /*
  2.  * @(#)Ripple.java    1.0 1999/03/04
  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. /**
  20.  * Ripple is an applet calss that takes an image and applies virtual
  21.  * and horizontal sine tranlsations to the pixels.
  22.  * 
  23.  * @version    1.0 1999/03/04
  24.  * @author     David Griffiths
  25.  */
  26.  
  27. import java.awt.*;
  28. import java.applet.Applet;
  29. import java.awt.image.ImageObserver;
  30. import java.io.PrintStream;
  31.  
  32. public class Ripple extends Applet implements Runnable {
  33.     static boolean dispCopy;
  34.     Image origImg;
  35.     Image backImg;
  36.     Image finImg;
  37.     int w1;
  38.     int h1;
  39.     Graphics backG;
  40.     Graphics finG;
  41.     int phase;
  42.     Thread thrRipple;
  43.     int frameNo;
  44.     int frames;
  45.     int period;
  46.     boolean stopIt;
  47.     boolean borderGap;
  48.     boolean imLoaded;
  49.  
  50.     public void init() {
  51.         String param;
  52.  
  53.         if (!dispCopy) {
  54.             System.out.println("Ripple v 1.0 for Java");
  55.             dispCopy = true;
  56.         }
  57.         setBackground(Color.white);
  58.         origImg = getImage(getDocumentBase(), getParameter("image"));
  59.         borderGap = "on".equals(getParameter("bordergap"));
  60.         w1 = origImg.getWidth(this);
  61.         h1 = origImg.getHeight(this);
  62.         if (h1 > 0 && w1 > 0)
  63.             stopIt = false;
  64.         param = getParameter("period");
  65.         if (param != null)
  66.             period = Integer.parseInt(param);
  67.         else
  68.             period = 24;
  69.  
  70.         param = getParameter("frames");
  71.         if (param != null)
  72.             frames = Integer.parseInt(param);
  73.         else
  74.             frames = 12;
  75.     }
  76.  
  77.     public void start() {
  78.         if (thrRipple == null) {
  79.             thrRipple = new Thread(this);
  80.             thrRipple.start();
  81.         }
  82.     }
  83.  
  84.     public void stop() {
  85.         if (thrRipple != null) {
  86.             thrRipple.stop();
  87.             thrRipple = null;
  88.         }
  89.     }
  90.  
  91.     public boolean imageUpdate(Image image, int i1, int j1, int k, int i2, int j2) {
  92.         boolean flag = true;
  93.         if ((i1 & 2) > 0)
  94.             h1 = j2;
  95.         if ((i1 & 1) > 0)
  96.             w1 = i2;
  97.         if ((i1 & 32) > 0)
  98.             imLoaded = true;
  99.         if (imLoaded && w1 > 0 && h1 > 0) {
  100.             stopIt = false;
  101.             flag = false;
  102.         }
  103.         return flag;
  104.     }
  105.  
  106.     public void createIt() {
  107.         backImg = createImage(w1, h1);
  108.         backG = backImg.getGraphics();
  109.         finImg = createImage(w1, frames * h1);
  110.         finG = finImg.getGraphics();
  111.         backG.setColor(Color.white);
  112.         for (phase = 0; phase < frames; phase++) {
  113.             backG.drawImage(origImg, 0, 0, this);
  114.             transformImage(backG);
  115.             finG.drawImage(backImg, 0, phase * h1, this);
  116.         }
  117.     }
  118.  
  119.     public void transformImage(Graphics g) {
  120.         translateX(g);
  121.         translateY(g);
  122.     }
  123.  
  124.     public void translateX(Graphics g) {
  125.         for (int i = 0; i < h1; i++) {
  126.             double d = (double)(period >> 1) * Math.sin((double)i / period + 6.283185307179586 * phase / frames);
  127.             g.copyArea(0, i, w1, 1, (int)d, 0);
  128.             if (borderGap) {
  129.                 g.drawLine((int)d, i, 0, i);
  130.                 g.drawLine((int)d + w1, i, w1, i);
  131.             }
  132.         }
  133.     }
  134.  
  135.     public void translateY(Graphics g) {
  136.         for (int i = 0; i < w1; i++) {
  137.             double d = (double)(period >> 1)
  138.                 * Math.sin((double)i / period
  139.                     + 6.283185307179586 * phase / frames);
  140.             g.copyArea(i, 0, 1, h1, 0, (int)d);
  141.             if (borderGap) {
  142.                 g.drawLine(i, (int)d, i, 0);
  143.                 g.drawLine(i, (int)d + h1, i, h1);
  144.             }
  145.         }
  146.     }
  147.  
  148.     public void run() {
  149.         while (thrRipple != null) {
  150.             try {
  151.                 Thread.sleep(50);
  152.             }
  153.             catch (InterruptedException e) {
  154.             }
  155.             frameNo = (frameNo + 1) % frames;
  156.             repaint();
  157.         }
  158.     }
  159.  
  160.     public void update(Graphics g) {
  161.         paint(g);
  162.     }
  163.  
  164.     public void paint(Graphics g) {
  165.         if (!stopIt) {
  166.             stopIt = true;
  167.             createIt();
  168.         }
  169.         if (finImg != null) {
  170.             g.drawImage(finImg, 0, -frameNo * h1, this);
  171.             g.drawImage(finImg, 0, (frames - frameNo) * h1, this);
  172.             return;
  173.         }
  174.         g.drawImage(origImg, 0, 0, this);
  175.     }
  176.  
  177.     public Ripple() {
  178.         stopIt = true;
  179.         borderGap = true;
  180.         imLoaded = false;
  181.     }
  182. }
  183.