-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulatorMain.java
More file actions
118 lines (106 loc) · 4.33 KB
/
SimulatorMain.java
File metadata and controls
118 lines (106 loc) · 4.33 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
113
114
115
116
117
118
package FinalProject;
//these line show all the import used in the program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
//this is where the SimulatorMain class started
public class SimulatorMain extends JPanel {
//these are some of the variable used in the program
public static final int TILE_SIZE = 10;
public static final int TILES_PER_ROW = 100;
public static final int TILES_PER_COL = 100;
private Tiles[][] grid;
private Fire fire;
private Random rand = new Random();
public SimulatorMain(Interface input) {
setPreferredSize(new Dimension(TILE_SIZE * TILES_PER_ROW, TILE_SIZE * TILES_PER_COL));
createGrid(input);
fire = new Fire();
Timer starter = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
fire.startFire(grid);
}
});
starter.setRepeats(false);
starter.start();
Timer timer = new Timer(input.tickSpeedValue(), new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < TILES_PER_COL; row++) {
for (int col = 0; col < TILES_PER_ROW; col++) {
if (grid[row][col] instanceof BurnableTiles bt) {
bt.decreaseBurnTime(200, grid, row, col);
}
}
}
fire.spreadFire(grid);
repaint();
}
});
timer.start();
}
//this method we used to draw graphics onto the GUI of the program
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int row = 0; row < TILES_PER_COL; row++) {
for (int col = 0; col < TILES_PER_ROW; col++) {
int x = col * TILE_SIZE;
int y = row * TILE_SIZE;
grid[row][col].draw(g, x, y, TILE_SIZE);
}
}
}
//this method is used to create individual tiles where each one will be defined as a different object
public void createGrid(Interface input) {
grid = new Tiles[TILES_PER_COL][TILES_PER_ROW];
for (int row = 0; row < TILES_PER_COL; row++) {
for (int col = 0; col < TILES_PER_ROW; col++) {
int temp = rand.nextInt(100);
if (col > 60 && col <= 70){
grid[row][col] = new Water();
}else if(col == 60 || col == 71){
int random = rand.nextInt(2);
if(random==0){
grid[row][col] = new Water();
if(col == 60){
grid[row][59]= new Sand();
}else if(col == 71){
grid[row][72]= new Sand();
}
}else{
grid[row][col] = new Sand();
}
}else if(grid[row][col] == null){
if (temp < input.FieldValue()) {
grid[row][col] = new Tiles();
} else if (temp >= input.FieldValue() && temp <= input.GrassValue()+input.FieldValue()){
grid[row][col] = new Grass();
}else if ( temp > input.GrassValue()+input.FieldValue()&&temp<=input.FieldValue()+input.GrassValue()+input.treeValue()){
grid[row][col] = new Tree();
}else{
grid[row][col] = new Rock();
}
}
}
}
}
//this is where the main method started
public static void main(String[] args) {
Interface input = new Interface();
while (!input.currentStatus()) {
try {
Thread.sleep(100); // Check every 100ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
JFrame frame = new JFrame("Simulator Grid");
SimulatorMain panel = new SimulatorMain(input);
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println(input.GrassValue()+" "+input.FieldValue()+" "+input.RocksValue());
}
}