From 825108d10e011777bce5c9b49a2cad6142c8558c Mon Sep 17 00:00:00 2001 From: Holt59 Date: Mon, 5 Mar 2018 22:59:47 +0100 Subject: [PATCH] Improve 'clear' performance (only clear overlays if possible). --- src/main/org/insa/graphics/MainWindow.java | 34 +++++++++--- .../org/insa/graphics/drawing/Drawing.java | 5 ++ .../drawing/components/BasicDrawing.java | 52 +++++++++++++++---- .../drawing/components/MapViewDrawing.java | 44 ++++++++++++---- 4 files changed, 108 insertions(+), 27 deletions(-) diff --git a/src/main/org/insa/graphics/MainWindow.java b/src/main/org/insa/graphics/MainWindow.java index 23c94d6..51a7ec8 100644 --- a/src/main/org/insa/graphics/MainWindow.java +++ b/src/main/org/insa/graphics/MainWindow.java @@ -104,6 +104,9 @@ public class MainWindow extends JFrame { private final MapViewDrawing mapViewDrawing; private final BasicDrawing basicDrawing; + private final GraphPalette basicPalette, blackAndWhitePalette; + private GraphPalette currentPalette; + // Main panel. private final JSplitPane mainPanel; @@ -150,9 +153,13 @@ public class MainWindow extends JFrame { // Create drawing and action listeners... this.basicDrawing = new BasicDrawing(); this.mapViewDrawing = new MapViewDrawing(); - this.drawing = this.basicDrawing; + // Createa palettes + this.basicPalette = new BasicGraphPalette(); + this.blackAndWhitePalette = new BlackAndWhiteGraphPalette(); + this.currentPalette = this.basicPalette; + wccPanel = new AlgorithmPanel(this, WeaklyConnectedComponentsAlgorithm.class, "Weakly-Connected Components", new String[] {}, false, false); wccPanel.addStartActionListener(new ActionListener() { @@ -452,8 +459,13 @@ public class MainWindow extends JFrame { } // 2. We draw the graph. - drawing.clear(); - ((MapViewDrawing) drawing).drawGraph(mfile); + if (isNewGraph) { + drawing.clear(); + ((MapViewDrawing) drawing).drawGraph(mfile); + } + else { + drawing.clearOverlays(); + } notifyRedrawRequest(); } @@ -465,8 +477,14 @@ public class MainWindow extends JFrame { mainPanel.setDividerLocation(oldLocation); notifyDrawingLoaded(mapViewDrawing, basicDrawing); } - drawing.clear(); - drawing.drawGraph(graph, palette); + if (isNewGraph || palette != this.currentPalette) { + this.currentPalette = palette; + drawing.clear(); + drawing.drawGraph(graph, palette); + } + else { + drawing.clearOverlays(); + } notifyRedrawRequest(); } @@ -483,7 +501,7 @@ public class MainWindow extends JFrame { * */ private void drawGraph() { - drawGraph(null, new BasicGraphPalette()); + drawGraph(null, this.currentPalette); } private void loadGraph(GraphReader reader) { @@ -662,7 +680,7 @@ public class MainWindow extends JFrame { launchThread(new Runnable() { @Override public void run() { - drawGraph(BasicDrawing.class); + drawGraph(BasicDrawing.class, basicPalette); } }); } @@ -676,7 +694,7 @@ public class MainWindow extends JFrame { launchThread(new Runnable() { @Override public void run() { - drawGraph(BasicDrawing.class, new BlackAndWhiteGraphPalette()); + drawGraph(BasicDrawing.class, blackAndWhitePalette); } }); } diff --git a/src/main/org/insa/graphics/drawing/Drawing.java b/src/main/org/insa/graphics/drawing/Drawing.java index 237a0d2..4a456d8 100644 --- a/src/main/org/insa/graphics/drawing/Drawing.java +++ b/src/main/org/insa/graphics/drawing/Drawing.java @@ -30,6 +30,11 @@ public interface Drawing { */ public void clear(); + /** + * Remove overlays from the drawing. + */ + public void clearOverlays(); + /** * Draw the given point with the given color. * diff --git a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java index 39d25cc..7a46f28 100644 --- a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java @@ -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 * @return @@ -433,26 +462,29 @@ public class BasicDrawing extends JPanel implements Drawing { return new Point((float) lon, (float) lat); } + /* + * (non-Javadoc) + * + * @see + * org.insa.graphics.drawing.Drawing#addDrawingClickListener(org.insa.graphics. + * drawing.DrawingClickListener) + */ @Override public void addDrawingClickListener(DrawingClickListener listener) { this.drawingClickListeners.add(listener); } + /* + * (non-Javadoc) + * + * @see org.insa.graphics.drawing.Drawing#removeDrawingClickListener(org.insa. + * graphics.drawing.DrawingClickListener) + */ @Override public void removeDrawingClickListener(DrawingClickListener 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) { return new BasicMarkerOverlay(point, color); } diff --git a/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java b/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java index 28d3f2f..f4f4d13 100644 --- a/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java +++ b/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java @@ -120,7 +120,7 @@ public class MapViewDrawing extends MapView implements Drawing { private class MapViewMarkerOverlay extends MapViewOverlay implements MarkerOverlay { public MapViewMarkerOverlay(Marker marker, Color color) { - super(new Layer[]{ marker }, color); + super(new Layer[] { marker }, color); } @Override @@ -156,11 +156,11 @@ public class MapViewDrawing extends MapView implements Drawing { public MapViewPathOverlay(PolylineAutoScaling path, MarkerAutoScaling origin, MarkerAutoScaling destination) { - super(new Layer[]{ path, origin, destination }, path.getColor()); + super(new Layer[] { path, origin, destination }, path.getColor()); } public MapViewPathOverlay(PolylineAutoScaling path) { - super(new Layer[]{ path }, path.getColor()); + super(new Layer[] { path }, path.getColor()); } @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 public void paint(Graphics 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) { return new LatLong(point.getLatitude(), point.getLongitude()); } @@ -319,12 +351,6 @@ public class MapViewDrawing extends MapView implements Drawing { this.drawingClickListeners.remove(listener); } - @Override - public void clear() { - getLayerManager().getLayers().clear(); - repaint(); - } - protected MarkerAutoScaling createMarker(Point point, Color color) { Image image = MarkerUtils.getMarkerForColor(color); return new MarkerAutoScaling(convertPoint(point), image);