Wave Filter

Wave Filter: The entire source code for this applet is listed below. There are actually two files which you must create and compile. You will be asked to experiment with the amplitude in order to get credit for this assignment, but first you just need to get the basic program up and running.

Here's another effect for you to look at.

import java.net.URL; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.applet.*; public class wave extends Applet{ Image original, wavey; public void init(){ original = getImage(getDocumentBase(), "bear.jpg"); FilteredImageSource fis = new FilteredImageSource( original.getSource(), new WaveFilter()); wavey = createImage(fis); } public void paint(Graphics g){ int iw = original.getWidth(this); g.drawImage(original, 10, 10, this); g.drawImage(wavey, iw+20, 10, this); } } //---------- new file -------------------- //Graphic Java 1.2, GEARY, pages 165 - 167 import java.awt.*; import java.awt.image.*; import java.util.Hashtable; class WaveFilter extends ImageFilter { ColorModel defaultRGB = ColorModel.getRGBdefault(); int amplitude, frequency, width, height; int sineArray[], intPixels[]; public WaveFilter(){ this(10,10); } public WaveFilter(int amplitude, int frequency) { this.amplitude = amplitude; this.frequency = frequency; } public void setDimensions(int width, int height){ int x; this.width = width; this.height = height + (amplitude*2); intPixels = new int[width * this.height]; sineArray = new int[width]; for(x = 0; x < width; ++x) { sineArray[x] = ((int)(amplitude * Math.sin(((double)(x)) / ((double)frequency)))); } consumer.setDimensions(width, this.height); } public void setProperties(Hashtable props) { String akey = "wave amplitude", fkey = "wave frequency"; String aval = Integer.toString(amplitude), fval = Integer.toString(frequency); Object o = props.get(akey); if(o != null && o instanceof String) aval = ((String) o) + ", " + aval; o = props.get(fkey); if(o != null && o instanceof String) fval = ((String) o) + ", " + fval; props.put(akey, aval); props.put(fkey, fval); consumer.setProperties(props); } public void setColorModel(ColorModel model) { consumer.setColorModel(defaultRGB); } public void setHints(int hints) { hints |= TOPDOWNLEFTRIGHT | COMPLETESCANLINES; hints &= ~RANDOMPIXELORDER; consumer.setHints(hints); } public void setPixels(int x, int y, int w, int h, ColorModel cm, int pixels[], int offset, int scansize){ int index, destY, destIndex = 0; for(int row=0; row<h; row++){ for(int col=0; col<w; col++){ destY = y + row + amplitude + sineArray[col]; destIndex = (destY*w) + x + col; index = offset + row*scansize + col; intPixels[destIndex] = cm.getRGB(pixels[index]); } } } public void setPixels(int x, int y, int w, int h, ColorModel cm, byte pixels[], int offset, int scansize){ int index, destY, destIndex = 0; for(int row=0; row<h; row++){ for(int col=0; col<w; col++){ destY = y + row + amplitude + sineArray[col]; destIndex = (destY*w) + x + col; index = offset + row*scansize + col; intPixels[destIndex] = cm.getRGB(pixels[index] & 0x0ff); } } } public void imageComplete(int status) { if(status == IMAGEERROR || status == IMAGEABORTED) { consumer.imageComplete(status); return; } consumer.setPixels(0,0,width,height,defaultRGB,intPixels,0,width); consumer.imageComplete(status); } }