Tile.java


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

/*************************************************************************
 *  Compilation:  javac Tile.java
 *  Execution:    java Tile filename M N
 * 
 *  Reads in an image from a file, and create an M-by-N tile of the image.
 *
 *  % java Tile image.jpg 3 3
 *
 *************************************************************************/

import java.awt.Color;

public class Tile {

    public static void main(String args[]) {
        Picture input = new Picture(args[0]);
        int height = input.height();
        int width  = input.width();
        int M = Integer.parseInt(args[1]);
        int N = Integer.parseInt(args[2]);

        Picture output = new Picture(N*input.width(), M*input.height());

        // convert to black and white
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                Color c = input.getColor(x, y);
                for (int i = 0; i < M; i++) {
                    for (int j = 0; j < N; j++) {
                        output.setColor(width*j + x, height*i + y, c);
                    }
                }
            }
        }
        output.show();
        output.save("temp.png");
    }
 

   
}


Last updated: Thu Sep 30 11:54:59 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.