CS 145 Lecture 22 – Loops IV
Agenda
- what ?s
- program this
- invertingĀ a
BufferedImage
- blackboxes
TODO
- No Weekly Outside Problem this week.
- Mercy Fortnight expires before Friday.
Blackboxes
Program This
Code
GetThird.java
package lecture1022;
import java.util.Scanner;
public class GetThird {
public static String getThird(String csv) {
Scanner in = new Scanner(csv);
int target = 3;
String field = null;
for (int i = 0; i < target; ++i) {
if (in.hasNext()) {
field = in.next();
} else {
return null;
}
}
return field;
}
}
Image.java
package lecture1022;
import java.util.Random;
public class Image {
public static void main(String[] args) {
int width = 512;
int height = 256;
System.out.println("P2");
System.out.println(width + " " + height);
System.out.println(255);
makeHorizontalGradient(width, height);
}
public static void makeHorizontalGradient(int width,
int height) {
double jump = 255.0 / width;
for (int r = 0; r < height; ++r) {
double brightness = 0;
for (int c = 0; c < width; ++c) {
if (r < height / 2) {
brightness += jump;
System.out.println((int) brightness);
} else {
System.out.println((int) (255.0 * c / (double) width));
}
}
}
}
public static void makeGradient(int width,
int height) {
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
System.out.println((int) (c / (double) width * 255.0));
}
}
}
public static void makeXor(int width,
int height) {
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
System.out.println((c ^ r) % 256);
}
}
}
public static void makeChecks(int width,
int height) {
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
if (r / 10 % 2 != c / 10 % 2) {
System.out.println(255);
} else {
System.out.println(0);
}
}
}
}
public static void makeStatic(int width,
int height) {
Random g = new Random();
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
System.out.println(g.nextInt(256));
}
}
}
public static void makeStripes(int width,
int height) {
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
if (c % 10 <= 3) {
System.out.println(0);
} else {
System.out.println(255);
}
}
}
}
public static void makeDiagonals(int width,
int height) {
for (int r = 0; r < height; ++r) {
for (int c = 0; c < width; ++c) {
// if (r % 10 == c % 10) {
if ((r + width - c) % 20 < 10) {
System.out.println(0);
} else {
System.out.println(255);
}
}
}
}
public static void makePacman(int width,
int height) {
int rCenter = height / 2;
int cCenter = width / 2;
for (int r = 0; r < height; ++r) {
int diffR = r - rCenter;
for (int c = 0; c < width; ++c) {
int diffC = c - cCenter;
double distance = Math.hypot(diffR, diffC);
if (distance < 14) {
System.out.println(0);
} else {
System.out.println(255);
}
}
}
}
}
Inverter.java
package lecture1022;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Inverter {
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("/Users/johnch/Desktop/rainbow.png"));
BufferedImage inverted = invert(image);
ImageIO.write(inverted, "png", new File("/Users/johnch/Desktop/inverted.png"));
}
public static BufferedImage invert(BufferedImage src) {
BufferedImage dst = new BufferedImage(src.getWidth(),
src.getHeight(),
src.getType());
// traverse image
// for each pixel
// find its complement
for (int r = 0; r < src.getHeight(); ++r) {
for (int c = 0; c < src.getWidth(); ++c) {
int packedColor = src.getRGB(c, r);
Color color = new Color(packedColor);
int redComplement = 255 - color.getRed();
int greenComplement = 255 - color.getGreen();
int blueComplement = 255 - color.getBlue();
Color complementColor = new Color(redComplement, greenComplement, blueComplement);
int packedComplementColor = complementColor.getRGB();
dst.setRGB(c, r, packedComplementColor);
}
}
return dst;
}
}
Haiku
on the dimensionality of games:
Which is easier?
1-D, 2-D, or 3-D?
Just try Linesweeper
Which is easier?
1-D, 2-D, or 3-D?
Just try Linesweeper