Wave.java


Below is the syntax highlighted version of Wave.java from §3.1 Using Data Types.


/******************************************************************************
 *  Compilation:  javac Wave.java
 *  Execution:    java Wave filename
 *
 *  Produce a wave effect.
 *
 *  % java Wave baboon.jpg
 *
 *
 ******************************************************************************/

public class Wave {

    public static void main(String[] args) {
        Picture pic1 = new Picture(args[0]);
        pic1.show();
        int width  = pic1.width();
        int height = pic1.height();

        Picture pic2 = new Picture(width, height);

        // wave filter
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int xx = x;
                int yy = (int) (y + 20 * Math.sin(x * 2 * Math.PI / 64));
                if (yy >= 0 && yy < height) {
                    pic2.set(x, y, pic1.get(xx, yy));
                }
            }
        }

        pic2.show();
    }


}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:22:01 EDT 2022.