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