Synchronize overlays operations in BasicDrawing.

This commit is contained in:
Mikael Capelle 2018-03-01 19:51:14 +01:00
parent 25da6079e6
commit 8bca08f795

View File

@ -13,6 +13,7 @@ import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -65,7 +66,9 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public void delete() { public void delete() {
BasicDrawing.this.overlays.remove(this); synchronized (overlays) {
BasicDrawing.this.overlays.remove(this);
}
BasicDrawing.this.repaint(); BasicDrawing.this.repaint();
} }
@ -276,7 +279,8 @@ public class BasicDrawing extends JPanel implements Drawing {
private Graphics2D graphGraphics = null; private Graphics2D graphGraphics = null;
// List of image for markers // List of image for markers
private List<BasicOverlay> overlays = new ArrayList<>(); private List<BasicOverlay> overlays = Collections
.synchronizedList(new ArrayList<BasicOverlay>());
// Mapping DrawingClickListener -> MouseEventListener // Mapping DrawingClickListener -> MouseEventListener
private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>(); private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>();
@ -308,8 +312,10 @@ public class BasicDrawing extends JPanel implements Drawing {
} }
// Draw markers // Draw markers
for (BasicOverlay overlay: overlays) { synchronized (overlays) {
overlay.draw(g); for (BasicOverlay overlay: overlays) {
overlay.draw(g);
}
} }
} }
@ -381,7 +387,9 @@ public class BasicDrawing extends JPanel implements Drawing {
if (this.graphGraphics != null) { if (this.graphGraphics != null) {
this.graphGraphics.clearRect(0, 0, this.width, this.height); this.graphGraphics.clearRect(0, 0, this.width, this.height);
} }
this.overlays.clear(); synchronized (overlays) {
this.overlays.clear();
}
} }
public BasicMarkerOverlay createMarker(Point point, Color color) { public BasicMarkerOverlay createMarker(Point point, Color color) {
@ -391,7 +399,9 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public MarkerOverlay drawMarker(Point point, Color color) { public MarkerOverlay drawMarker(Point point, Color color) {
BasicMarkerOverlay marker = createMarker(point, color); BasicMarkerOverlay marker = createMarker(point, color);
this.overlays.add(marker); synchronized (overlays) {
this.overlays.add(marker);
}
this.repaint(); this.repaint();
return marker; return marker;
} }
@ -399,7 +409,9 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public PointSetOverlay createPointSetOverlay() { public PointSetOverlay createPointSetOverlay() {
BasicPointSetOverlay ps = new BasicPointSetOverlay(); BasicPointSetOverlay ps = new BasicPointSetOverlay();
this.overlays.add(ps); synchronized (overlays) {
this.overlays.add(ps);
}
return ps; return ps;
} }
@ -552,7 +564,9 @@ public class BasicDrawing extends JPanel implements Drawing {
destination = createMarker(path.getDestination().getPoint(), color); destination = createMarker(path.getDestination().getPoint(), color);
} }
BasicPathOverlay overlay = new BasicPathOverlay(points, color, origin, destination); BasicPathOverlay overlay = new BasicPathOverlay(points, color, origin, destination);
this.overlays.add(overlay); synchronized (overlays) {
this.overlays.add(overlay);
}
this.repaint(); this.repaint();
return overlay; return overlay;
} }