From 9acdc998b3638a8f8f3dd31f75564cc9f98149fe Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sun, 25 Feb 2018 12:44:15 +0100 Subject: [PATCH] Improve overlay management for BasicDrawing. --- .../insa/graphics/drawing/BasicDrawing.java | 93 ++++++++++++++----- .../insa/graphics/drawing/MapViewDrawing.java | 3 +- .../graphics/drawing/utils/MarkerUtils.java | 7 +- 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/src/main/org/insa/graphics/drawing/BasicDrawing.java b/src/main/org/insa/graphics/drawing/BasicDrawing.java index 89574fa..7a376e3 100644 --- a/src/main/org/insa/graphics/drawing/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/BasicDrawing.java @@ -11,6 +11,7 @@ import java.awt.event.MouseListener; import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; import java.awt.image.BufferedImage; +import java.util.ArrayList; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; @@ -23,6 +24,7 @@ import org.insa.graph.Graph; import org.insa.graph.Node; import org.insa.graph.Path; import org.insa.graph.Point; +import org.insa.graphics.drawing.utils.MarkerUtils; /** * Cette implementation de la classe Dessin produit vraiment un affichage (au @@ -41,12 +43,12 @@ public class BasicDrawing extends JPanel implements Drawing { // Point of the marker. private Point point; - // Color of the "marker". - private Color color; + // Image of the marker + protected BufferedImage image; - public BasicMarkerTracker(Point point, Color color) { + public BasicMarkerTracker(Point point, BufferedImage image) { this.point = point; - this.color = color; + this.image = image; } @Override @@ -56,11 +58,14 @@ public class BasicDrawing extends JPanel implements Drawing { @Override public void moveTo(Point point) { - BasicDrawing.this.drawMarker(point, color); + this.point = point; + BasicDrawing.this.repaint(); } @Override public void delete() { + BasicDrawing.this.markers.remove(this); + BasicDrawing.this.repaint(); } }; @@ -74,18 +79,24 @@ public class BasicDrawing extends JPanel implements Drawing { // Default marker width private static final int DEFAULT_MARKER_WIDTH = 10; - // - private final Graphics2D gr; - private double long1, long2, lat1, lat2; // Width and height of the image private final int width, height; - // - private Image image; private ZoomAndPanListener zoomAndPanListener; + // + private Image graphImage; + private final Graphics2D graphGraphics; + + // Image for path / points + private Image overlayImage; + private Graphics2D overlayGraphics; + + // List of image for markers + private List markers = new ArrayList<>(); + // Mapping DrawingClickListener -> MouseEventListener private Map listenerMapping = new IdentityHashMap<>(); @@ -104,11 +115,16 @@ public class BasicDrawing extends JPanel implements Drawing { this.height = 1600; BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR); + this.graphImage = img; + this.graphGraphics = img.createGraphics(); + this.graphGraphics.setBackground(Color.WHITE); - this.image = img; - this.gr = img.createGraphics(); + img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_4BYTE_ABGR); + this.overlayImage = img; + this.overlayGraphics = img.createGraphics(); + this.overlayGraphics.setBackground(new Color(0, 0, 0, 0)); - this.zoomAndPanListener.setCoordTransform(this.gr.getTransform()); + this.zoomAndPanListener.setCoordTransform(this.graphGraphics.getTransform()); this.long1 = -180; this.long2 = 180; @@ -125,7 +141,20 @@ public class BasicDrawing extends JPanel implements Drawing { Graphics2D g = (Graphics2D) g1; g.clearRect(0, 0, getWidth(), getHeight()); g.setTransform(zoomAndPanListener.getCoordTransform()); - g.drawImage(image, 0, 0, this); + + // Draw graph + g.drawImage(graphImage, 0, 0, this); + + // Draw overlays (path, etc.) + g.drawImage(overlayImage, 0, 0, this); + + // Draw markers + for (BasicMarkerTracker mtracker: markers) { + BufferedImage img = mtracker.image; + int px = this.projx(mtracker.getPoint().getLongitude()); + int py = this.projy(mtracker.getPoint().getLatitude()); + g.drawImage(img, px - img.getWidth() / 2, py - img.getHeight(), this); + } } protected void setBB(double long1, double long2, double lat1, double lat2) { @@ -206,17 +235,18 @@ public class BasicDrawing extends JPanel implements Drawing { } protected void setWidth(int width) { - this.gr.setStroke(new BasicStroke(width)); + this.overlayGraphics.setStroke(new BasicStroke(width)); } protected void setColor(Color col) { - this.gr.setColor(col); + this.overlayGraphics.setColor(col); } @Override public void clear() { - this.gr.setColor(Color.WHITE); - this.gr.fillRect(0, 0, this.width, this.height); + this.graphGraphics.clearRect(0, 0, this.width, this.height); + this.overlayGraphics.clearRect(0, 0, this.width, this.height); + this.markers.clear(); } @Override @@ -226,7 +256,7 @@ public class BasicDrawing extends JPanel implements Drawing { int y1 = this.projy(from.getLatitude()); int y2 = this.projy(to.getLatitude()); - gr.drawLine(x1, y1, x2, y2); + overlayGraphics.drawLine(x1, y1, x2, y2); this.repaint(); } @@ -245,14 +275,24 @@ public class BasicDrawing extends JPanel implements Drawing { @Override public MarkerTracker drawMarker(Point point) { - drawPoint(point, DEFAULT_MARKER_WIDTH, this.gr.getColor()); - return new BasicMarkerTracker(point, this.gr.getColor()); + return drawMarker(point, this.overlayGraphics.getColor()); } @Override public MarkerTracker drawMarker(Point point, Color color) { - setColor(color); - return drawMarker(point); + /* + * BufferedImage img = new BufferedImage(DEFAULT_MARKER_WIDTH, + * DEFAULT_MARKER_WIDTH, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D gr = + * img.createGraphics(); gr.setColor(color); gr.fillOval(0, 0, + * DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH); + */ + BufferedImage img = MarkerUtils.getMarkerForColor(color); + Graphics2D gr = img.createGraphics(); + gr.scale(0.3, 0.3); + BasicMarkerTracker marker = new BasicMarkerTracker(point, img); + this.markers.add(marker); + this.repaint(); + return marker; } @Override @@ -261,7 +301,7 @@ public class BasicDrawing extends JPanel implements Drawing { setColor(color); int x = this.projx(point.getLongitude()) - DEFAULT_MARKER_WIDTH / 2; int y = this.projy(point.getLatitude()) - DEFAULT_MARKER_WIDTH / 2; - gr.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH); + overlayGraphics.fillOval(x, y, DEFAULT_MARKER_WIDTH, DEFAULT_MARKER_WIDTH); this.repaint(); } @@ -321,12 +361,15 @@ public class BasicDrawing extends JPanel implements Drawing { @Override public void drawGraph(Graph graph, GraphPalette palette) { clear(); + Graphics2D oldOverlayGraphics = this.overlayGraphics; + this.overlayGraphics = this.graphGraphics; initialize(graph); for (Node node: graph.getNodes()) { for (Arc arc: node.getSuccessors()) { drawArc(arc, palette); } } + this.overlayGraphics = oldOverlayGraphics; } @Override @@ -366,7 +409,7 @@ public class BasicDrawing extends JPanel implements Drawing { private void putText(Point point, String txt) { int x = this.projx(point.getLongitude()); int y = this.projy(point.getLatitude()); - gr.drawString(txt, x, y); + graphGraphics.drawString(txt, x, y); this.repaint(); } diff --git a/src/main/org/insa/graphics/drawing/MapViewDrawing.java b/src/main/org/insa/graphics/drawing/MapViewDrawing.java index 5f7ca1f..60be9a7 100644 --- a/src/main/org/insa/graphics/drawing/MapViewDrawing.java +++ b/src/main/org/insa/graphics/drawing/MapViewDrawing.java @@ -22,6 +22,7 @@ import org.mapsforge.core.model.BoundingBox; import org.mapsforge.core.model.LatLong; import org.mapsforge.core.model.MapPosition; import org.mapsforge.core.util.LatLongUtils; +import org.mapsforge.map.awt.graphics.AwtBitmap; import org.mapsforge.map.awt.graphics.AwtGraphicFactory; import org.mapsforge.map.awt.util.AwtUtil; import org.mapsforge.map.awt.view.MapView; @@ -196,7 +197,7 @@ public class MapViewDrawing extends MapView implements Drawing { @Override public MarkerTracker drawMarker(Point point, Color color) { - Bitmap bitmap = MarkerUtils.getMarkerForColor(color); + Bitmap bitmap = new AwtBitmap(MarkerUtils.getMarkerForColor(color)); Marker marker = new Marker(convertPoint(point), bitmap, 0, -bitmap.getHeight() / 2); getLayerManager().getLayers().add(marker); return new MapViewMarkerTracker(marker); diff --git a/src/main/org/insa/graphics/drawing/utils/MarkerUtils.java b/src/main/org/insa/graphics/drawing/utils/MarkerUtils.java index e76125d..e9d5da9 100644 --- a/src/main/org/insa/graphics/drawing/utils/MarkerUtils.java +++ b/src/main/org/insa/graphics/drawing/utils/MarkerUtils.java @@ -3,9 +3,6 @@ package org.insa.graphics.drawing.utils; import java.awt.Color; import java.awt.image.BufferedImage; -import org.mapsforge.core.graphics.Bitmap; -import org.mapsforge.map.awt.graphics.AwtBitmap; - public class MarkerUtils { /** @@ -24,7 +21,7 @@ public class MarkerUtils { * @param color * @return */ - public static Bitmap getMarkerForColor(Color color) { + public static BufferedImage getMarkerForColor(Color color) { // create image BufferedImage image = new BufferedImage(MARKER_MASK[0].length, MARKER_MASK.length, BufferedImage.TYPE_INT_ARGB); @@ -36,7 +33,7 @@ public class MarkerUtils { } // Create Bitmap and return it. - return new AwtBitmap(image); + return image; } // Mask