Skip to content

Commit

Permalink
fixed ConcurrentModificationException
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 22, 2020
1 parent 0d98639 commit 23b657f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
8 changes: 4 additions & 4 deletions parameters.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"width": 25,
"height": 25,
"jungleRatio": 0.05,
"moveEnergy": 0.00001,
"width": 15,
"height": 15,
"jungleRatio": 0.1,
"moveEnergy": 1.0,
"initialEnergy": 100.0,
"plantEnergy": 25.0,
"initialAnimals": 10
Expand Down
13 changes: 0 additions & 13 deletions src/sample/Main.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package sample;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import simulation.Simulation;
import simulation.SimulationErrorException;
import simulation.StatsWatcher;

public class Main extends Application {

Expand Down
2 changes: 0 additions & 2 deletions src/sample/ParametersLoader.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package sample;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import simulation.InvalidStartingParametersException;
import simulation.Simulation;

import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;

Expand Down
40 changes: 26 additions & 14 deletions src/sample/SimulationThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,65 @@
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import simulation.Simulation;
import simulation.SimulationErrorException;
import simulation.StatsWatcher;

import java.util.concurrent.atomic.AtomicBoolean;

public class SimulationThread extends Thread{
private final Stage stage = new Stage();
private final Simulation simulation;
private final Scene simulationScene;
private final Group root = new Group();
private final StatsWatcher statsWatcher;
private final SimulationCanvas canvas;
private final Label statsLabel = new Label();
private final AtomicBoolean paused = new AtomicBoolean(false);

public void run() {
Platform.runLater(this::displaySimulation);
}
public SimulationThread(Simulation simulation){
this.simulation = simulation;
this.simulationScene = new Scene(root, simulation.getWidth()*10+400, Math.max(simulation.getHeight()*10,400), Color.BLACK);
this.statsWatcher = new StatsWatcher(simulation);
this.canvas = new SimulationCanvas(simulation);
}
public void displaySimulation() {
final Group root = new Group();
final double width = simulation.getWidth()*10+400;
final double height = Math.max(simulation.getHeight()*10,400);
final Scene simulationScene = new Scene(root, width, height, Color.BLACK);

final StatsWatcher statsWatcher = new StatsWatcher(simulation);
final SimulationCanvas canvas = new SimulationCanvas(simulation);
canvas.setLayoutX(400);
final Label statsLabel = new Label();
statsLabel.setTextFill(Color.web("#FFFFFF"));
statsLabel.setPadding(new Insets(5.0));

root.getChildren().add(statsLabel);
root.getChildren().add(canvas);
stage.setScene(simulationScene);
stage.setTitle("Evolution Simulator");
stage.setResizable(false);

simulation.start();
canvas.drawBackground();
canvas.update();
stage.show();
SimulationThread simulationThread = this;
Task<Integer> task = new Task<>() {
@Override protected Integer call() throws Exception {
int iterations;
for (iterations = 0; true; iterations++) {
if (isCancelled()) {
break;
}
simulation.simulateOneDay();
System.out.println("CANVAS UPDATE start");

Platform.runLater(canvas::update);
Platform.runLater(()->{statsLabel.setText(statsWatcher.getSummary());});

Thread.sleep(100);
if(simulationThread.paused.get()){
continue;
}

Platform.runLater(()->{
try {
simulation.simulateOneDay();
} catch (SimulationErrorException e) {
e.printStackTrace();
}
canvas.update(); statsLabel.setText(statsWatcher.getSummary());});
}
return iterations;
}
Expand Down

0 comments on commit 23b657f

Please sign in to comment.