FlipX.java


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


/******************************************************************************
 *  Compilation:  javac FlipX.java
 *  Execution:    java FlipX filename
 *
 *  Reads in an image from a file, and flips it horizontally.
 *
 *  % java FlipX image.jpg
 *
 ******************************************************************************/

import java.awt.Color;

public class FlipX {

    public static void main(String[] args) {
        Picture pic = new Picture(args[0]);
        int width  = pic.width();
        int height = pic.height();
        pic.show();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width / 2; x++) {
                Color c1 = pic.get(x, y);
                Color c2 = pic.get(width - x - 1, y);
                pic.set(x, y, c2);
                pic.set(width - x - 1, y, c1);
            }
        }
        pic.show();
    }



}


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