publicstaticint trace2(int n) {
int z = 0;
while (n > 0) {
++z;
n /= 2; // same as n = n / 2
}
return z;
}
publicstaticvoid main(String[] args) {
System.out.println(trace2(32));
}
publicstaticString trace3(String s, int a, int b) {
String t = "";
for (int i = a; i < b; ++i) {
t += s.charAt(i);
}
return t;
}
publicstaticvoid main(String[] args) {
System.out.println(trace3("Abraham", 4, 7));
}
publicstaticint trace4(int n) {
int z = 0;
while (n > 0) {
z = z * 10 + n % 10;
n = n / 10;
}
return z;
}
publicstaticvoid 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…
random noise
a line between two points
a checkerboard pattern
a gradient
Before we meet again, please do the following:
Read sections 2.3, 2.4, 5.1, and 5.2. On a quarter sheet, write down 2-3 questions or observations from your reading.
See you next class!
Sincerely,
P.S. Here’s the code we wrote together…
Noise.java
packagelecture1031;
importjava.awt.Color;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.Random;
importjavax.imageio.ImageIO;
publicclassNoise {
publicstaticvoid main(String[] args) throwsIOException {
BufferedImage image = newBufferedImage(512, 256, BufferedImage.TYPE_BYTE_GRAY);
Random generator = newRandom();
for (int r = 0; r < image.getHeight(); ++r) {
for (int c = 0; c < image.getWidth(); ++c) {
int gray = generator.nextInt(256);
Color randomColor = newColor(gray, gray, gray);
image.setRGB(c, r, randomColor.getRGB());
}
}
ImageIO.write(image, "png", newFile("/Users/johnch/Desktop/noise.png"));
}
}
Checkerboard.java
packagelecture1031;
importjava.awt.Color;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.Random;
importjavax.imageio.ImageIO;
publicclassCheckerboard {
publicstaticvoid main(String[] args) throwsIOException {
BufferedImage image = newBufferedImage(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", newFile("/Users/johnch/Desktop/check.png"));
}
}
Gradient.java
packagelecture1031;
importjava.awt.Color;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.util.Random;
importjavax.imageio.ImageIO;
publicclassGradient {
publicstaticvoid main(String[] args) throwsIOException {
BufferedImage image = newBufferedImage(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, newColor(gray, gray, gray).getRGB());
}
}
ImageIO.write(image, "png", newFile("/Users/johnch/Desktop/gradient.png"));
}
}