84 lines
3.0 KiB
Java
84 lines
3.0 KiB
Java
import java.awt.*;
|
|
|
|
public class Client {
|
|
public static void main(String[] args) {
|
|
// 1. Create a new picture sized 400 x 400 pixels
|
|
// 2. Call divide divide canvas
|
|
// 3. Save the image to display it
|
|
Picture pic = new Picture(500,500);
|
|
divideCanvas(pic, 4);
|
|
pic.save("boner.jpg");
|
|
}
|
|
|
|
public static void divideCanvas(Picture p, int n) {
|
|
Color[][] pixels = p.getPixels();
|
|
divideCanvas(p, pixels, n, new Point(0, 0), new Point(p.width() - 1, p.height() - 1));
|
|
p.setPixels(pixels);
|
|
}
|
|
|
|
private static void divideCanvas(Picture p, Color[][] pixels, int n, Point one, Point two) {
|
|
// extract coordinates from points
|
|
int x1 = one.x;
|
|
int x2 = two.x;
|
|
int y1 = one.y;
|
|
int y2 = two.y;
|
|
|
|
// make useful boundary points
|
|
Point topLeft = new Point(x1, y1);
|
|
Point topMiddle = new Point((x1 + x2) / 2, y1);
|
|
// Point topRight = new Point(x2,y1);
|
|
Point middleLeft = new Point(x1, (y1 + y2) / 2);
|
|
Point middleMiddle = new Point(topMiddle.x, middleLeft.y);
|
|
Point middleRight = new Point(x2, middleLeft.y);
|
|
// Point bottomLeft = new Point(x1,y2);
|
|
Point bottomMiddle = new Point(topMiddle.x, y2);
|
|
Point bottomRight = new Point(x2, y2);
|
|
|
|
// if we are actually dividing, do this
|
|
if (n != 0) {
|
|
// System.out.println(topLeft);
|
|
// System.out.println(topMiddle);
|
|
// System.out.println(middleLeft);
|
|
// System.out.println(middleMiddle);
|
|
// System.out.println(middleRight);
|
|
// System.out.println(bottomMiddle);
|
|
// System.out.println(bottomRight);
|
|
|
|
fill(pixels, topLeft.x, middleMiddle.x, topLeft.y, middleMiddle.y);
|
|
fill(pixels, topMiddle.x, middleRight.x, topMiddle.y, middleRight.y);
|
|
fill(pixels, middleLeft.x, bottomMiddle.x, middleLeft.y, bottomMiddle.y);
|
|
fill(pixels, middleMiddle.x, bottomRight.x, middleMiddle.y, bottomRight.y);
|
|
|
|
divideCanvas(p, pixels, n - 1, topLeft, middleMiddle);
|
|
divideCanvas(p, pixels, n - 1, topMiddle, middleRight);
|
|
divideCanvas(p, pixels, n - 1, middleLeft, bottomMiddle);
|
|
divideCanvas(p, pixels, n - 1, middleMiddle, bottomRight);
|
|
}
|
|
|
|
// otherwise, just draw the border
|
|
else {
|
|
fill(pixels,topLeft.x,bottomRight.x,topLeft.y,bottomRight.y);
|
|
}
|
|
}
|
|
|
|
public static void fill(Color[][] pixels, int x1, int x2, int y1, int y2) {
|
|
for (int i = y1 + 1; i <= y2 - 1; i++) {
|
|
for (int n = x1 + 1; n <= x2-1; n++) {
|
|
pixels[i][n] = Color.WHITE;
|
|
}
|
|
}
|
|
|
|
// outer borders
|
|
for (int i = y1; i <= y2; i++) {
|
|
pixels[i][x1] = Color.BLACK;
|
|
pixels[i][x2] = Color.BLACK;
|
|
}
|
|
|
|
for (int i = x1; i <= x2; i++) {
|
|
pixels[y1][i] = Color.BLACK;
|
|
pixels[y2][i] = Color.BLACK;
|
|
}
|
|
}
|
|
}
|
|
|