home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-04-04 | 4.2 KB | 183 lines |
- /*
- * @(#)Ripple.java 1.0 1999/03/04
- *
- * 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.
- *
- */
-
- /**
- * Ripple is an applet calss that takes an image and applies virtual
- * and horizontal sine tranlsations to the pixels.
- *
- * @version 1.0 1999/03/04
- * @author David Griffiths
- */
-
- import java.awt.*;
- import java.applet.Applet;
- import java.awt.image.ImageObserver;
- import java.io.PrintStream;
-
- public class Ripple extends Applet implements Runnable {
- static boolean dispCopy;
- Image origImg;
- Image backImg;
- Image finImg;
- int w1;
- int h1;
- Graphics backG;
- Graphics finG;
- int phase;
- Thread thrRipple;
- int frameNo;
- int frames;
- int period;
- boolean stopIt;
- boolean borderGap;
- boolean imLoaded;
-
- public void init() {
- String param;
-
- if (!dispCopy) {
- System.out.println("Ripple v 1.0 for Java");
- dispCopy = true;
- }
- setBackground(Color.white);
- origImg = getImage(getDocumentBase(), getParameter("image"));
- borderGap = "on".equals(getParameter("bordergap"));
- w1 = origImg.getWidth(this);
- h1 = origImg.getHeight(this);
- if (h1 > 0 && w1 > 0)
- stopIt = false;
- param = getParameter("period");
- if (param != null)
- period = Integer.parseInt(param);
- else
- period = 24;
-
- param = getParameter("frames");
- if (param != null)
- frames = Integer.parseInt(param);
- else
- frames = 12;
- }
-
- public void start() {
- if (thrRipple == null) {
- thrRipple = new Thread(this);
- thrRipple.start();
- }
- }
-
- public void stop() {
- if (thrRipple != null) {
- thrRipple.stop();
- thrRipple = null;
- }
- }
-
- public boolean imageUpdate(Image image, int i1, int j1, int k, int i2, int j2) {
- boolean flag = true;
- if ((i1 & 2) > 0)
- h1 = j2;
- if ((i1 & 1) > 0)
- w1 = i2;
- if ((i1 & 32) > 0)
- imLoaded = true;
- if (imLoaded && w1 > 0 && h1 > 0) {
- stopIt = false;
- flag = false;
- }
- return flag;
- }
-
- public void createIt() {
- backImg = createImage(w1, h1);
- backG = backImg.getGraphics();
- finImg = createImage(w1, frames * h1);
- finG = finImg.getGraphics();
- backG.setColor(Color.white);
- for (phase = 0; phase < frames; phase++) {
- backG.drawImage(origImg, 0, 0, this);
- transformImage(backG);
- finG.drawImage(backImg, 0, phase * h1, this);
- }
- }
-
- public void transformImage(Graphics g) {
- translateX(g);
- translateY(g);
- }
-
- public void translateX(Graphics g) {
- for (int i = 0; i < h1; i++) {
- double d = (double)(period >> 1) * Math.sin((double)i / period + 6.283185307179586 * phase / frames);
- g.copyArea(0, i, w1, 1, (int)d, 0);
- if (borderGap) {
- g.drawLine((int)d, i, 0, i);
- g.drawLine((int)d + w1, i, w1, i);
- }
- }
- }
-
- public void translateY(Graphics g) {
- for (int i = 0; i < w1; i++) {
- double d = (double)(period >> 1)
- * Math.sin((double)i / period
- + 6.283185307179586 * phase / frames);
- g.copyArea(i, 0, 1, h1, 0, (int)d);
- if (borderGap) {
- g.drawLine(i, (int)d, i, 0);
- g.drawLine(i, (int)d + h1, i, h1);
- }
- }
- }
-
- public void run() {
- while (thrRipple != null) {
- try {
- Thread.sleep(50);
- }
- catch (InterruptedException e) {
- }
- frameNo = (frameNo + 1) % frames;
- repaint();
- }
- }
-
- public void update(Graphics g) {
- paint(g);
- }
-
- public void paint(Graphics g) {
- if (!stopIt) {
- stopIt = true;
- createIt();
- }
- if (finImg != null) {
- g.drawImage(finImg, 0, -frameNo * h1, this);
- g.drawImage(finImg, 0, (frames - frameNo) * h1, this);
- return;
- }
- g.drawImage(origImg, 0, 0, this);
- }
-
- public Ripple() {
- stopIt = true;
- borderGap = true;
- imLoaded = false;
- }
- }
-