-
Notifications
You must be signed in to change notification settings - Fork 13
/
HitomezashiMain.java
59 lines (54 loc) · 2.13 KB
/
HitomezashiMain.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
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.BorderFactory;
public class HitomezashiMain {
private static final Color[] BORDERS = {
new Color(200, 200, 200), new Color(100, 100, 150)
};
private static class ImagePanel extends JPanel {
private final Image img;
public ImagePanel(Image img, String toolTip) {
this.setToolTipText(toolTip);
this.setBorder(BorderFactory.createEtchedBorder(BORDERS[0], BORDERS[1]));
this.img = img;
this.setPreferredSize(new Dimension(img.getWidth(this), img.getHeight(this)));
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, this);
}
}
public static void main(String[] args) {
Random rng = new Random(4242);
final int W = 21, H = 20, SQUARE = 20, ROWS = 2, COLS = 3;
final int[] PROB = { 30, 40, 50, 60, 70, 80 };
JFrame f = new JFrame("Hitomezashi Demo");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLayout(new GridLayout(ROWS, COLS));
int imageCount = 0;
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < COLS; j++) {
boolean[] horizontal = new boolean[W];
boolean[] vertical = new boolean[H];
horizontal[0] = vertical[0] = rng.nextBoolean();
for(int x = 1; x < W; x++) {
horizontal[x] = (rng.nextInt(100) < PROB[imageCount]) == horizontal[x - 1];
}
for(int y = 1; y < H; y++) {
vertical[y] = (rng.nextInt(100) < PROB[imageCount]) == vertical[y - 1];
}
BufferedImage pattern = Hitomezashi.createPattern(W, H, SQUARE, horizontal, vertical);
f.add(new ImagePanel(pattern, PROB[imageCount] + "%"));
imageCount++;
}
}
f.pack();
f.setVisible(true);
}
}