Improve 'clear' performance (only clear overlays if possible).

This commit is contained in:
Holt59 2018-03-05 22:59:47 +01:00
parent bd01aadeb1
commit 825108d10e
4 changed files with 108 additions and 27 deletions

View File

@ -104,6 +104,9 @@ public class MainWindow extends JFrame {
private final MapViewDrawing mapViewDrawing; private final MapViewDrawing mapViewDrawing;
private final BasicDrawing basicDrawing; private final BasicDrawing basicDrawing;
private final GraphPalette basicPalette, blackAndWhitePalette;
private GraphPalette currentPalette;
// Main panel. // Main panel.
private final JSplitPane mainPanel; private final JSplitPane mainPanel;
@ -150,9 +153,13 @@ public class MainWindow extends JFrame {
// Create drawing and action listeners... // Create drawing and action listeners...
this.basicDrawing = new BasicDrawing(); this.basicDrawing = new BasicDrawing();
this.mapViewDrawing = new MapViewDrawing(); this.mapViewDrawing = new MapViewDrawing();
this.drawing = this.basicDrawing; this.drawing = this.basicDrawing;
// Createa palettes
this.basicPalette = new BasicGraphPalette();
this.blackAndWhitePalette = new BlackAndWhiteGraphPalette();
this.currentPalette = this.basicPalette;
wccPanel = new AlgorithmPanel(this, WeaklyConnectedComponentsAlgorithm.class, wccPanel = new AlgorithmPanel(this, WeaklyConnectedComponentsAlgorithm.class,
"Weakly-Connected Components", new String[] {}, false, false); "Weakly-Connected Components", new String[] {}, false, false);
wccPanel.addStartActionListener(new ActionListener() { wccPanel.addStartActionListener(new ActionListener() {
@ -452,8 +459,13 @@ public class MainWindow extends JFrame {
} }
// 2. We draw the graph. // 2. We draw the graph.
if (isNewGraph) {
drawing.clear(); drawing.clear();
((MapViewDrawing) drawing).drawGraph(mfile); ((MapViewDrawing) drawing).drawGraph(mfile);
}
else {
drawing.clearOverlays();
}
notifyRedrawRequest(); notifyRedrawRequest();
} }
@ -465,8 +477,14 @@ public class MainWindow extends JFrame {
mainPanel.setDividerLocation(oldLocation); mainPanel.setDividerLocation(oldLocation);
notifyDrawingLoaded(mapViewDrawing, basicDrawing); notifyDrawingLoaded(mapViewDrawing, basicDrawing);
} }
if (isNewGraph || palette != this.currentPalette) {
this.currentPalette = palette;
drawing.clear(); drawing.clear();
drawing.drawGraph(graph, palette); drawing.drawGraph(graph, palette);
}
else {
drawing.clearOverlays();
}
notifyRedrawRequest(); notifyRedrawRequest();
} }
@ -483,7 +501,7 @@ public class MainWindow extends JFrame {
* *
*/ */
private void drawGraph() { private void drawGraph() {
drawGraph(null, new BasicGraphPalette()); drawGraph(null, this.currentPalette);
} }
private void loadGraph(GraphReader reader) { private void loadGraph(GraphReader reader) {
@ -662,7 +680,7 @@ public class MainWindow extends JFrame {
launchThread(new Runnable() { launchThread(new Runnable() {
@Override @Override
public void run() { public void run() {
drawGraph(BasicDrawing.class); drawGraph(BasicDrawing.class, basicPalette);
} }
}); });
} }
@ -676,7 +694,7 @@ public class MainWindow extends JFrame {
launchThread(new Runnable() { launchThread(new Runnable() {
@Override @Override
public void run() { public void run() {
drawGraph(BasicDrawing.class, new BlackAndWhiteGraphPalette()); drawGraph(BasicDrawing.class, blackAndWhitePalette);
} }
}); });
} }

View File

@ -30,6 +30,11 @@ public interface Drawing {
*/ */
public void clear(); public void clear();
/**
* Remove overlays from the drawing.
*/
public void clearOverlays();
/** /**
* Draw the given point with the given color. * Draw the given point with the given color.
* *

View File

@ -392,6 +392,35 @@ public class BasicDrawing extends JPanel implements Drawing {
} }
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#clear()
*/
@Override
public void clear() {
if (this.graphGraphics != null) {
this.graphGraphics.clearRect(0, 0, this.width, this.height);
}
synchronized (overlays) {
this.overlays.clear();
}
this.repaint();
}
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#clearOverlays()
*/
@Override
public void clearOverlays() {
synchronized (overlays) {
this.overlays.clear();
}
this.repaint();
}
/** /**
* @param lon * @param lon
* @return * @return
@ -433,26 +462,29 @@ public class BasicDrawing extends JPanel implements Drawing {
return new Point((float) lon, (float) lat); return new Point((float) lon, (float) lat);
} }
/*
* (non-Javadoc)
*
* @see
* org.insa.graphics.drawing.Drawing#addDrawingClickListener(org.insa.graphics.
* drawing.DrawingClickListener)
*/
@Override @Override
public void addDrawingClickListener(DrawingClickListener listener) { public void addDrawingClickListener(DrawingClickListener listener) {
this.drawingClickListeners.add(listener); this.drawingClickListeners.add(listener);
} }
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#removeDrawingClickListener(org.insa.
* graphics.drawing.DrawingClickListener)
*/
@Override @Override
public void removeDrawingClickListener(DrawingClickListener listener) { public void removeDrawingClickListener(DrawingClickListener listener) {
this.drawingClickListeners.remove(listener); this.drawingClickListeners.remove(listener);
} }
@Override
public void clear() {
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) { public BasicMarkerOverlay createMarker(Point point, Color color) {
return new BasicMarkerOverlay(point, color); return new BasicMarkerOverlay(point, color);
} }

View File

@ -120,7 +120,7 @@ public class MapViewDrawing extends MapView implements Drawing {
private class MapViewMarkerOverlay extends MapViewOverlay implements MarkerOverlay { private class MapViewMarkerOverlay extends MapViewOverlay implements MarkerOverlay {
public MapViewMarkerOverlay(Marker marker, Color color) { public MapViewMarkerOverlay(Marker marker, Color color) {
super(new Layer[]{ marker }, color); super(new Layer[] { marker }, color);
} }
@Override @Override
@ -156,11 +156,11 @@ public class MapViewDrawing extends MapView implements Drawing {
public MapViewPathOverlay(PolylineAutoScaling path, MarkerAutoScaling origin, public MapViewPathOverlay(PolylineAutoScaling path, MarkerAutoScaling origin,
MarkerAutoScaling destination) { MarkerAutoScaling destination) {
super(new Layer[]{ path, origin, destination }, path.getColor()); super(new Layer[] { path, origin, destination }, path.getColor());
} }
public MapViewPathOverlay(PolylineAutoScaling path) { public MapViewPathOverlay(PolylineAutoScaling path) {
super(new Layer[]{ path }, path.getColor()); super(new Layer[] { path }, path.getColor());
} }
@Override @Override
@ -270,6 +270,11 @@ public class MapViewDrawing extends MapView implements Drawing {
} }
} }
/*
* (non-Javadoc)
*
* @see org.mapsforge.map.awt.view.MapView#paint(java.awt.Graphics)
*/
@Override @Override
public void paint(Graphics graphics) { public void paint(Graphics graphics) {
super.paint(graphics); super.paint(graphics);
@ -282,6 +287,33 @@ public class MapViewDrawing extends MapView implements Drawing {
} }
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#clear()
*/
@Override
public void clear() {
getLayerManager().getLayers().clear();
repaint();
}
/*
* (non-Javadoc)
*
* @see org.insa.graphics.drawing.Drawing#clearOverlays()
*/
@Override
public void clearOverlays() {
Layers layers = getLayerManager().getLayers();
for (Layer layer: layers) {
if (layer instanceof PolylineAutoScaling || layer instanceof MarkerAutoScaling) {
getLayerManager().getLayers().remove(layer, false);
}
}
repaint();
}
protected LatLong convertPoint(Point point) { protected LatLong convertPoint(Point point) {
return new LatLong(point.getLatitude(), point.getLongitude()); return new LatLong(point.getLatitude(), point.getLongitude());
} }
@ -319,12 +351,6 @@ public class MapViewDrawing extends MapView implements Drawing {
this.drawingClickListeners.remove(listener); this.drawingClickListeners.remove(listener);
} }
@Override
public void clear() {
getLayerManager().getLayers().clear();
repaint();
}
protected MarkerAutoScaling createMarker(Point point, Color color) { protected MarkerAutoScaling createMarker(Point point, Color color) {
Image image = MarkerUtils.getMarkerForColor(color); Image image = MarkerUtils.getMarkerForColor(color);
return new MarkerAutoScaling(convertPoint(point), image); return new MarkerAutoScaling(convertPoint(point), image);