-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTilePanel.java
417 lines (374 loc) · 15.2 KB
/
TilePanel.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* This program, if distributed by its author to the public as source code,
* can be used if credit is given to its author and any project or program
* released with the source code is released under the same stipulations.
*/
package life;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* @author Julian
*/
class TilePanel extends JPanel {
private int leftMostTile = 0;
private int upMostTile = 0;
private int endTileRight;
private int endTileDown;
public static final int CELL_SIZE_X_SMALL = 3;
public static final int CELL_SIZE_SMALL = 5;
public static final int CELL_SIZE_MEDIUM = 7;
public static final int CELL_SIZE_LARGE = 9;
public static final int CELL_SIZE_X_LARGE = 11;
public static Color BROWN = Color.decode("#8B4513");
public static Color PURPLE = Color.decode("#9932CC");
private int sizeOfCell = 7;
private static final int numColumns = 1000; // X -- max on my res is about 250
private static final int numRows = 1000; // Y -- max on my res is like about 140
private static final int sizeOfGameX = 1024;
private static final int sizeOfGameY = 768;
private int currentRuleset = Rulesets.RULESET_STANDARD; // Standard ruleset
private Color liveColor;
private int simulationStep;
private boolean cell[][] = new boolean[numColumns][numRows];
private int age[][] = new int[numColumns][numRows];
private boolean paused;
private boolean updateOutsideOfScreen;
//private ActionListener al = null; // will be sent from main class
public TilePanel() {
simulationStep = 0;
liveColor = Color.GREEN;
paused = true; // start game paused
updateOutsideOfScreen = false; // Turned off initially for performance
this.setPreferredSize(new Dimension(sizeOfGameX, sizeOfGameY)); // set PREFERRED size so that pack() works
this.setFocusable(true); // set focusable so our KeyAdapter (KeyListener) works
//this.requestFocusInWindow();
///*
// Add mouse motion listener (drag operations,
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getX() / sizeOfCell;
int y = e.getY() / sizeOfCell;
int button = e.getButton();
// Determine what action is to be performed
boolean action;
if(SwingUtilities.isLeftMouseButton(e)) action = true;
else action = false;
// Check array bounds, because dragging offscreen will cause AOBE
if (x >= 0 && x < cell.length && y >= 0 && y < cell[0].length) {
cell[x][y] = action;
age[x][y] = 0;
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {
// Do nothing
}
});
//*/
// Add MouseListener (via MouseAdapter)
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//System.out.println("Mouse clicked x: " + e.getX() + " y: " + e.getY());
}
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX() / sizeOfCell;
int y = e.getY() / sizeOfCell;
// Determine what action is to be performed
boolean action;
if(SwingUtilities.isLeftMouseButton(e)) action = true;
else action = false;
cell[x][y] = action;
age[x][y] = 0;
repaint();
}
// Not working / todo
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
System.out.println("Something happened here...\n");
int notches = e.getWheelRotation();
if(notches < 0) {
System.out.println("Mouse wheel moved up " + -notches + " notches \n");
} else {
System.out.println("Mouse wheel moved down " + notches + " notches \n");
}
}
@Override
public void mouseReleased(MouseEvent e) {
//System.out.println("Mouse released x: " + e.getX() + " y: " + e.getY());
}
@Override
public void mouseEntered(MouseEvent e) {
//System.out.println("Mouse entered x: " + e.getX() + " y: " + e.getY());
}
@Override
public void mouseExited(MouseEvent e) {
//System.out.println("Mouse exited x: " + e.getX() + " y: " + e.getY());
}
});
// Add KeyListener (via KeyAdapter)
this.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
//System.out.println("Key typed: " + e.getKeyChar() + "(" + e.getKeyCode() + ")");
}
@Override
public void keyPressed(KeyEvent e) {
//System.out.println("Key pressed: " + e.getKeyChar() + "(" + e.getKeyCode() + ")");
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
Life.kill();
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (paused) paused = false;
else paused = true;
} else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_PLUS) {
System.out.println("Game speed set to " + ++Life.TICKS_PER_SECOND + " ticks per second.");
} else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_MINUS) {
if(Life.TICKS_PER_SECOND > 1)
System.out.println("Game speed set to " + --Life.TICKS_PER_SECOND + " ticks per second.");
else
System.out.println("Can't set game speed to 0 ticks per second... division by 0, duh!");
}
}
@Override
public void keyReleased(KeyEvent e) {
//System.out.println("Key released: " + e.getKeyChar() + "(" + e.getKeyCode() + ")");
}
});
}
public void setCellSize(int cellSize) {
sizeOfCell = cellSize;
repaint();
}
public int getCellSize() {
return sizeOfCell;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int liveColorMult = 0x400;
if (liveColor == Color.GREEN) {
liveColorMult = 0x400;
} else if (liveColor == BROWN){
liveColorMult = 0x02; // Why can't I do this correctly? Color doesn't update properly with the new brown
} else if (liveColor == PURPLE) {
liveColorMult = 0x20;
}
int width = this.getSize().width;
int height = this.getSize().height;
endTileRight = (width / sizeOfCell) + ((width % sizeOfCell > 0) ? 1 : 0) + leftMostTile; //
endTileDown = (height / sizeOfCell) + ((height % sizeOfCell > 0) ? 1 : 0) + upMostTile; //
for(int col = leftMostTile; col < endTileRight; col++) {
for(int row = upMostTile; row < endTileDown; row++) {
if(cell[col][row]) { // if true i.e. it's a LIVE cell
g.setColor(new Color(liveColor.getRGB() - (age[col][row]*liveColorMult) )); // This color thing isn't final...
} else {
g.setColor(Color.BLACK);
}
g.fillRect(col*sizeOfCell, row*sizeOfCell, sizeOfCell, sizeOfCell);
g.setColor(Color.DARK_GRAY);
g.drawRect(col*sizeOfCell, row*sizeOfCell, sizeOfCell, sizeOfCell);
}
}
}
// GAME LOOP
protected void run() {
while(Life.RUNNING) {
while(paused) {
try {
Thread.sleep(10); // just sleep for 10ms bc why wouldn't you
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long startTime = System.currentTimeMillis();
// Perform simulation logic
tick();
updateSimStep();
// Repaint the screen to
repaint();
long timeDiff = System.currentTimeMillis() - startTime;
// Make sure we only process TICKS_PER_SECOND steps in a second
if(timeDiff < (1000L/Life.TICKS_PER_SECOND)) {
try {
Thread.sleep((1000L/Life.TICKS_PER_SECOND) - timeDiff);
} catch (InterruptedException e) {
// do nothing because I really don't give a fudge
}
}
}
}
private void tick() {
int l, r, u, d;
if(!updateOutsideOfScreen) {
l = leftMostTile;
r = endTileRight;
u = upMostTile;
d = endTileDown;
} else {
l = 0;
r = cell[0].length;
u = 0;
d = cell.length;
}
switch(currentRuleset) {
case Rulesets.RULESET_STANDARD:
Rulesets.standardRulesetTick(cell, age, l, r, u, d);
break;
case Rulesets.RULESET_CUSTOM_1:
Rulesets.customRulesetTick1(cell, age, l, r, u, d);
break;
case Rulesets.RULESET_CUSTOM_2:
Rulesets.customRulesetTick2(cell, age, l, r, u, d);
break;
case Rulesets.RULESET_AGE:
age = Rulesets.ageRuleset(cell, age, l, r, u, d);
break;
case Rulesets.RULESET_AGE_2:
age = Rulesets.ageRuleset2(cell, age, l, r, u, d);
break;
case Rulesets.RULESET_AGE_CRAZY:
age = Rulesets.ageRulesetCrazy(cell, age, l, r, u, d);
break;
default:
Rulesets.standardRulesetTick(cell, age, l, r, u, d);
break;
}
}
public void setCurrentRuleset(int ruleset) {
currentRuleset = ruleset;
}
protected void clear() { // Called by JMenuItem File>Clear
paused = true; // Pause sim
simulationStep = 0; // Set sim step to 0
Life.jl.setText(" | Current simulation step: " + simulationStep);
simulationStep = 0;
for(int col = 0; col < cell.length; col++) {
for(int row = 0; row < cell[col].length; row++) {
cell[col][row] = false;
age[col][row] = 0;
}
}
repaint();
}
protected void setColor(Color color) {
liveColor = color;
repaint();
}
protected void randomBoard(int percentChance) {
clear(); // Call clear first for age
assert percentChance <= 100; // 101+% chance of an event is not a real thing - needs -ea
int colStart, colEnd, rowStart, rowEnd;
if(!updateOutsideOfScreen) {
colStart = leftMostTile;
rowStart = upMostTile;
colEnd = endTileRight;
rowEnd = endTileDown;
} else {
colStart = rowStart = 0;
colEnd = cell.length;
rowEnd = cell[0].length;
}
Random rand = new Random();
for(int col = colStart; col < colEnd; col++) {
for(int row = rowStart; row < rowEnd; row++) {
boolean alive = false;
int roll = rand.nextInt(100);
if(roll < percentChance) { // 0 through 24 == 25
alive = true;
age[col][row] = 1;
}
cell[col][row] = alive;
}
}
repaint();
}
protected void setUpdateOutsideOfScreen(boolean bool) {
updateOutsideOfScreen = bool;
}
protected boolean getUpdateOutsideOfScreen() {
return updateOutsideOfScreen;
}
protected void save(File file) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
int streamSize = (numRows * numColumns / 8);
byte[] stream = new byte[streamSize];
int byteCount = 0;
byte currentByte = 0x00;
int currentByteInt = 0;
int numBits = 0;
paused = true; // MAKE SURE we're paused
for(int col = 0; col < cell.length; col++) {
for(int row = 0; row < cell[col].length; row++) {
if(cell[col][row]) {
currentByteInt += 1 << numBits; // If alive, set LSB to 1
}
if(++numBits == 8) { // If we have a full byte after incrementing the count
currentByte = (byte)currentByteInt;
currentByteInt = 0;
stream[byteCount++] = currentByte; // Add byte to stream
numBits = 0; // Reset numBits
}
}
}
if(numBits > 0) { // In the middle of writing the byte, but not done?
while(numBits < 8) {
currentByteInt *= 2; // Bitshift to left
numBits++;
}
currentByte = (byte)currentByteInt;
stream[byteCount] = currentByte;
}
fos.write(stream);
fos.close();
}
protected void load(File file) throws FileNotFoundException, IOException {
clear();
FileInputStream fileIn = new FileInputStream(file);
char currentByte = 0x00;
int numBits = 0;
boolean[] cells = new boolean[8];
paused = true; // MAKE SURE we're paused
for(int col = 0; col < cell.length; col++) {
for(int row = 0; row < cell[col].length; row++) {
if(numBits == 0) {
currentByte = (char)fileIn.read(); // Grab new byte
for(int i = 0; i < 8; i++) {
if(currentByte % 2 == 1) {
cells[7-i] = true;
currentByte-=1;
} else {
cells[7-i] = false;
}
currentByte /= 2;
}
}
cell[col][row] = cells[7-numBits];
numBits++;
if(numBits == 8) numBits = 0; // If at 8 bits, reset to 0
}
}
repaint();
}
private void updateSimStep() {
simulationStep++; // simulationStep
Life.jl.setText(" | Current simulation step: " + simulationStep);
}
}