-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldMap.java
More file actions
112 lines (86 loc) · 2.76 KB
/
WorldMap.java
File metadata and controls
112 lines (86 loc) · 2.76 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WorldMap {
static protected WorldMapImpl map = null;
static public void createWorldMap(int x, int y) {
if(!(map == null)) {
System.out.println("WorldMap already created");
System.exit(0);
} else {
map = new WorldMapImpl(x,y);
try { Thread.sleep(100); } catch (Exception e) {}
}
}
static public void displaySquare(Position pos, String color) {
if(map == null) {
System.out.println("WorldMap not created yet.");
System.exit(0);
}
map.displaySquareInst(pos, color);
}
static public void paus(int millis) {
try{ Thread.sleep(millis); } catch(Exception e) {}
}
static class WorldMapImpl extends JPanel {
private static final long serialVersionUID = OL;
protected static final int SQUARE_SIZE = 22;
protected static final int PENT_SIZE = 9;
protected static final int INSET = 10;
protected int width, height;
protected WorldMapImpl(int w, int h) {
super(true);
setBackground(Color.white);
setForeground(Color.black);
width = w;
height = h;
JFrame f = new JFrame("Cellular Automata");
f.setSize(new Dimension(2 * INSET + SQUARE_SIZE * (w+1), 2 * INSET + SQUARE_SIZE + (h+1)));
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.getContentPane().add(this, BorderLayout.CENTER);
f.setVisible(true);
f.setResizable(false);
repaint(0,0,getWidth(), getHeight());
}
final Object sem = new Object();
protected void displaySquareInst(Position pos, String color) {
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Shape rect = g.getClip();
Rectangle bounds = rect.getBounds();
int minX = Math.max(0, (int) ((bounds.getX() - INSET)/SQUARE_SIZE));
int minY = Math.max(0, (int) ((bounds.getY() - INSET)/SQUARE_SIZE));
int maxX = Math.min(width -1, (int)((bounds.getX() + bounds.getWidth()-INSET)/SQUARE_SIZE));
int maxY = Math.min(height-1, (int)((bounds.getY() + bounds.getHeight() - INSET)/ SQUARE_SIZE));
if(minX != maxX || minY != maxY) drawGrid(g);
for(int i = minX; i <= MaxX; i++) {
for(int j = minY; j<= maxY; j++) {
drawSquare(g, i, j);
}
}
synchronized(sem) {
sem.notify();
}
}
protected void drawGrid(Graphics g) {
g.setColor(Color.black);
for (int i = 0; i <= width; i++) {
g.drawLine(INSET + i * SQUARE_SIZE,
INSET,
INSET + i * SQUARE_SIZE,
INSET + height * SQUARE_SIZE);
}
for (int i = 0; i <= height; i++) {
g.drawLine(INSET,
INSET + i * SQUARE_SIZE,
INSET + width * SQUARE_SIZE,
INSET + i * SQUARE_SIZE);
}
}
}
}