-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMosaicImage.java
More file actions
52 lines (43 loc) · 1.96 KB
/
Copy pathMosaicImage.java
File metadata and controls
52 lines (43 loc) · 1.96 KB
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
public class MosaicImage {
public static void main(String args[]) {
double canvasH = 720;
int totalHorizontalSquare = 204;
int totalVerticalSquare = 264;
int lengthW = 21;
int lengthH = 21;
int totalHorizontalBig_sq = 10;
double canvasW = totalHorizontalSquare * canvasH / totalVerticalSquare;
StdDraw.setCanvasSize((int) canvasW, (int) canvasH);
StdDraw.setXscale(0, canvasW);
StdDraw.setYscale(0, canvasH);
double sq_W = canvasH / totalVerticalSquare;
double x0 = sq_W / 2, y0 = canvasH - sq_W / 2;
int c = -1;
while (!StdIn.isEmpty()) {
String s = StdIn.readString();
c++;
String[] strings = new String[lengthH];
for (int i = 0; i < lengthH; i++) {
strings[i] = StdIn.readString();
}
double x = x0 + lengthW * (c % totalHorizontalBig_sq) * sq_W;
int cc = c / totalHorizontalBig_sq;
double y = y0 - lengthH * cc * sq_W;
draw(strings, x, y, sq_W);
}
}
public static void draw(String[] strings, double x, double y, double sq_W) {
for (int i = 0; i < strings.length; i++) {
for (int j = 0; j < strings[i].length(); j++) {
if (strings[i].charAt(j) == 'W') StdDraw.setPenColor(StdDraw.WHITE);
else if (strings[i].charAt(j) == 'B') StdDraw.setPenColor(StdDraw.BLACK);
else if (strings[i].charAt(j) == 'O') StdDraw.setPenColor(StdDraw.ORANGE);
else if (strings[i].charAt(j) == 'R') StdDraw.setPenColor(StdDraw.RED);
else if (strings[i].charAt(j) == 'G') StdDraw.setPenColor(StdDraw.GREEN);
else if (strings[i].charAt(j) == 'Y') StdDraw.setPenColor(StdDraw.YELLOW);
if (strings[i].charAt(j) != 'W')
StdDraw.filledSquare(x + j * sq_W, y - i * sq_W, sq_W / 2);
}
}
}
}