Permalink
Browse files

Various locks to avoid ConcurrentModificationExceptions. Should've

started with Scala. Have been told that due to immutable objects there
aren't any ConcurrentModificationExceptions in Scala. Next time ...
  • Loading branch information...
stevenschwenke committed Mar 24, 2013
1 parent 3ffefcc commit 4af9dd85ebd714fd3f6ee248411689543225bdd2
Showing with 40 additions and 31 deletions.
  1. +8 −5 src/business/worldObjects/World.java
  2. +32 −26 src/ui/javaFX/JavaFxApplication.java
@@ -132,12 +132,15 @@ public void interact(Interaction interaction, WorldObject... objects) {
* @return all {@link Resource}s on the map
*/
public Set<Resource> getAllExistingResources() {
Set<Resource> resources = new HashSet<Resource>();
for (WorldObject worldObject : objects) {
if (worldObject instanceof Resource)
resources.add((Resource) worldObject);
synchronized (objects) {
Set<Resource> resources = new HashSet<Resource>();
for (WorldObject worldObject : objects) {
if (worldObject instanceof Resource)
resources.add((Resource) worldObject);
}
return resources;
}
return resources;
}
/**
@@ -124,7 +124,9 @@ private void addMessageLabel() {
messageLabel.setLayoutX(world.getWidth() / 2);
messageLabel.setLayoutY(10);
rootGroup.getChildren().add(messageLabel);
synchronized (rootGroup) {
rootGroup.getChildren().add(messageLabel);
}
postToMessageLabel("Initialized");
}
@@ -191,16 +193,18 @@ public void notifyCreation(final WorldObject newWorldObject) {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (newWorldObject instanceof ResourceSpawner) {
ResourceSpawnerFX newSpawnerFX = new ResourceSpawnerFX(
(ResourceSpawner) newWorldObject);
rootGroup.getChildren().add(newSpawnerFX);
} else if (newWorldObject instanceof Resource) {
rootGroup.getChildren().add(
new ResourceFX((Resource) newWorldObject));
} else if (newWorldObject instanceof Colony) {
rootGroup.getChildren().add(
new ColonyFX((Colony) newWorldObject));
synchronized (rootGroup) {
if (newWorldObject instanceof ResourceSpawner) {
ResourceSpawnerFX newSpawnerFX = new ResourceSpawnerFX(
(ResourceSpawner) newWorldObject);
rootGroup.getChildren().add(newSpawnerFX);
} else if (newWorldObject instanceof Resource) {
rootGroup.getChildren().add(
new ResourceFX((Resource) newWorldObject));
} else if (newWorldObject instanceof Colony) {
rootGroup.getChildren().add(
new ColonyFX((Colony) newWorldObject));
}
}
}
});
@@ -245,21 +249,23 @@ public void run() {
line.setStrokeWidth(2);
rootGroup.getChildren().add(line);
FadeTransition ft = new FadeTransition(Duration
.millis(1300), line);
ft.setFromValue(1.0);
ft.setToValue(0);
ft.play();
// remove the line after the animation
ft.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
rootGroup.getChildren().remove(line);
}
});
synchronized (rootGroup) {
rootGroup.getChildren().add(line);
FadeTransition ft = new FadeTransition(Duration
.millis(1300), line);
ft.setFromValue(1.0);
ft.setToValue(0);
ft.play();
// remove the line after the animation
ft.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
rootGroup.getChildren().remove(line);
}
});
}
}
}
};

0 comments on commit 4af9dd8

Please sign in to comment.