teaching machines

CS 145 Lecture 22 – Images

October 31, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

We start today by revisiting memory tracing, but this time in programs with loops. Here we go!

Memory Tracing #1
public static String trace1(char c, int n) {
  String s = "";
  for (int i = 0; i < n; ++i) {
    s += c;
  }
  return s;
}

public static void main(String[] args) {
  System.out.println(trace1('*', 6)); 
}
Memory Tracing #2
public static int trace2(int n) {
  int z = 0;
  while (n > 0) {
    ++z;
    n /= 2; // same as n = n / 2
  }
  return z;
}

public static void main(String[] args) {
  System.out.println(trace2(32)); 
}
Memory Tracing #3
public static String trace3(String s, int a, int b) {
  String t = "";
  for (int i = a; i < b; ++i) {
    t += s.charAt(i);
  }
  return t;
}

public static void main(String[] args) {
  System.out.println(trace3("Abraham", 4, 7)); 
}
Memory Tracing #4
public static int trace4(int n) {
  int z = 0;
  while (n > 0) {
    z = z * 10 + n % 10;
    n = n / 10;
  }
  return z;
}

public static void main(String[] args) {
  System.out.println(trace4(24589)); 
}

Then we’ll work on some more image generating programs, hitting upon nested loops and BufferedImage. We’ll write programs that draw…

Before we meet again, please do the following:

See you next class!

Sincerely,

P.S. Here’s the code we wrote together…

Noise.java

package lecture1031;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;

public class Noise {
  public static void main(String[] args) throws IOException {
    BufferedImage image = new BufferedImage(512, 256, BufferedImage.TYPE_BYTE_GRAY);
    Random generator = new Random();

    for (int r = 0; r < image.getHeight(); ++r) {
      for (int c = 0; c < image.getWidth(); ++c) {
        int gray = generator.nextInt(256);
        Color randomColor = new Color(gray, gray, gray);
        image.setRGB(c, r, randomColor.getRGB());
      }
    }

    ImageIO.write(image, "png", new File("/Users/johnch/Desktop/noise.png"));
  }
}

Checkerboard.java

package lecture1031;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;

public class Checkerboard {
  public static void main(String[] args) throws IOException {
    BufferedImage image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);

    for (int r = 0; r < image.getHeight(); ++r) {
      for (int c = 0; c < image.getWidth(); ++c) {
//        if (c % 2 == 0 && r % 2 == 0) {
//        if (c % 2 == r % 2) {
          if (c / 32 % 2 == r / 32 % 2) {
          image.setRGB(c, r, Color.ORANGE.darker().darker().darker().darker().brighter().brighter().getRGB());
        } else {
          image.setRGB(c, r, Color.BLACK.getRGB());
        }
      }
    }

    ImageIO.write(image, "png", new File("/Users/johnch/Desktop/check.png"));
  }
}

Gradient.java

package lecture1031;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;

public class Gradient {
  public static void main(String[] args) throws IOException {
    BufferedImage image = new BufferedImage(1024, 1024, BufferedImage.TYPE_BYTE_GRAY);

    for (int r = 0; r < image.getHeight(); ++r) {
      for (int c = 0; c < image.getWidth(); ++c) {
        int gray = (int) (c / (image.getWidth() - 1.0) * 255);
        image.setRGB(c, r, new Color(gray, gray, gray).getRGB());
      }
    }

    ImageIO.write(image, "png", new File("/Users/johnch/Desktop/gradient.png"));
  }
}