-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathWindow.java
More file actions
62 lines (50 loc) · 1.87 KB
/
Window.java
File metadata and controls
62 lines (50 loc) · 1.87 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
53
54
55
56
57
58
59
60
61
62
package javacodes.lightdraw;
import java.util.Map;
import java.util.TreeMap;
public class Window {
/**
* Key is row number Value is another map, whose key is column number, and
* value is the character
*/
private Map<Integer, Map<Integer, String>> screen = new TreeMap<Integer, Map<Integer, String>>();
private static final int WINDOW_SIZE = 20;
/**
* Draw an empty screen, with pounds as chars
*/
public void init() {
for (int i = 0; i < WINDOW_SIZE; i++) {
Map<Integer, String> row = new TreeMap<Integer, String>();
for (int j = 0; j < WINDOW_SIZE; j++) {
row.put(j, " ");
}
screen.put(i, row);
}
}
public void render() {
System.out.println("[Screen");
System.out.println(" ------------------------------------------");
for (int rowNumber : screen.keySet()) {
Map<Integer, String> row = screen.get(rowNumber);
System.out.print(" | ");
for (int columnNumber : row.keySet()) {
String pixel = row.get(columnNumber);
System.out.print(pixel + " ");
}
System.out.print(" | ");
System.out.println("");
}
System.out.println(" ------------------------------------------");
}
public void draw(int row, int col, String character) {
if (row < 0 || row > WINDOW_SIZE - 1 || col < 0 || col > WINDOW_SIZE - 1) {
throw new IllegalArgumentException("OUT OF BOUNDS KA BOY!");
}
this.screen.get(row).put(col, character);
}
public String getPixel(int row, int col) {
if (row < 0 || row > WINDOW_SIZE - 1 || col < 0 || col > WINDOW_SIZE - 1) {
throw new IllegalArgumentException("OUT OF BOUNDS KA BOY!");
}
return this.screen.get(row).get(col);
}
}