/
ZSpiral.java
70 lines (53 loc) · 2.11 KB
/
ZSpiral.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// This program generates the zigzag spiral pattern.
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class ZSpiral {
public static void main(String[] args) throws Exception {
System.setProperty("java.awt.headless", "true");
int size = args.length == 0 ? 202 : (Integer.parseInt(args[0]) * 10 + 2);
System.out.println("Generating zigzag spiral image " + size + " * " + size);
int iw = size, ih = size;
BufferedImage image = new BufferedImage(iw, ih, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = image.createGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, iw, ih);
g.setColor(Color.white);
int x = 1, y = 1, len = size - 2;
boolean first = true;
for(;;) {
if (len < 5) break;
if (first) {
first = false;
} else {
g.drawLine(x+0-2 , y+2, x+0-2, y );
g.drawLine(x+0-2 , y , x+0+0, y );
g.drawLine(x+0+0 , y , x+0+0, y+2);
}
for(int i=0;i<len-4;i+=4) {
g.drawLine(x+i , y+2, x+i+2, y+2);
g.drawLine(x+i+2, y+2, x+i+2, y );
g.drawLine(x+i+2, y , x+i+4, y );
g.drawLine(x+i+4, y , x+i+4, y+2);
g.drawLine(size - 2 - (y+2), x+i , size - 2 - (y+2), x+i+2);
g.drawLine(size - 2 - (y+2), x+i+2, size - 2 - (y ), x+i+2);
g.drawLine(size - 2 - (y ), x+i+2, size - 2 - (y ), x+i+4);
g.drawLine(size - 2 - (y ), x+i+4, size - 2 - (y+2), x+i+4);
g.drawLine(size - 2 - (x+i ), size - 2 - (y+2), size - 2 - (x+i+2), size - 2 - (y+2));
g.drawLine(size - 2 - (x+i+2), size - 2 - (y+2), size - 2 - (x+i+2), size - 2 - (y ));
g.drawLine(size - 2 - (x+i+2), size - 2 - (y ), size - 2 - (x+i+4), size - 2 - (y ));
g.drawLine(size - 2 - (x+i+4), size - 2 - (y ), size - 2 - (x+i+4), size - 2 - (y+2));
if (i >= len-8) break;
g.drawLine((y+2), size - 2- (x+i ), (y+2), size - 2- (x+i+2));
g.drawLine((y+2), size - 2- (x+i+2), (y ), size - 2- (x+i+2));
g.drawLine((y ), size - 2- (x+i+2), (y ), size - 2- (x+i+4));
g.drawLine((y ), size - 2- (x+i+4), (y+2), size - 2- (x+i+4));
}
len -= 8;
x += 4;
y += 4;
}
ImageIO.write(image, "png", new File("zspiral" + size + ".png"));
}
}