From ae460ff4547bdda9c7fdcfd2971983cd4359bdd3 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sun, 25 Feb 2018 18:33:46 +0100 Subject: [PATCH] Remove drawLine. Better overlay for BasicDrawing. --- .../insa/graphics/drawing/BasicDrawing.java | 80 ++++++++----------- .../org/insa/graphics/drawing/Drawing.java | 29 ------- .../insa/graphics/drawing/MapViewDrawing.java | 19 ----- 3 files changed, 35 insertions(+), 93 deletions(-) diff --git a/src/main/org/insa/graphics/drawing/BasicDrawing.java b/src/main/org/insa/graphics/drawing/BasicDrawing.java index 2ce8844..1e16aab 100644 --- a/src/main/org/insa/graphics/drawing/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/BasicDrawing.java @@ -40,16 +40,12 @@ public class BasicDrawing extends JPanel implements Drawing { */ private static final long serialVersionUID = 96779785877771827L; - public class BasicOverlayTracker implements OverlayTracker { - - // Image of the marker - protected BufferedImage image; + public abstract class BasicOverlay implements OverlayTracker { // Visible? protected boolean visible; - public BasicOverlayTracker(BufferedImage image) { - this.image = image; + public BasicOverlay() { this.visible = true; } @@ -61,20 +57,29 @@ public class BasicDrawing extends JPanel implements Drawing { @Override public void delete() { - BasicDrawing.this.markers.remove(this); + BasicDrawing.this.overlays.remove(this); BasicDrawing.this.repaint(); } + /** + * Draw the given overlay. + */ + public abstract void draw(Graphics2D g); + }; - public class BasicMarkerTracker extends BasicOverlayTracker implements MarkerTracker { + public class BasicMarker extends BasicOverlay implements MarkerTracker { // Point of the marker. private Point point; - public BasicMarkerTracker(Point point, BufferedImage image) { - super(image); + // Color of the marker. + private Color color; + + public BasicMarker(Point point, Color color) { + super(); this.point = point; + this.color = color; } @Override @@ -88,6 +93,20 @@ public class BasicDrawing extends JPanel implements Drawing { BasicDrawing.this.repaint(); } + @Override + public void draw(Graphics2D graphics) { + + int px = BasicDrawing.this.projx(getPoint().getLongitude()); + int py = BasicDrawing.this.projy(getPoint().getLatitude()); + + BufferedImage img = MarkerUtils.getMarkerForColor(color); + Graphics2D gr = img.createGraphics(); + double scale = DEFAULT_MARKER_WIDTH / (double) img.getHeight(); + gr.scale(scale, scale); + + graphics.drawImage(img, px - img.getWidth() / 2, py - img.getHeight(), BasicDrawing.this); + } + }; // Default path color. @@ -116,7 +135,7 @@ public class BasicDrawing extends JPanel implements Drawing { private Graphics2D overlayGraphics; // List of image for markers - private List markers = new ArrayList<>(); + private List overlays = new ArrayList<>(); // Mapping DrawingClickListener -> MouseEventListener private Map listenerMapping = new IdentityHashMap<>(); @@ -170,13 +189,8 @@ public class BasicDrawing extends JPanel implements Drawing { g.drawImage(overlayImage, 0, 0, this); // Draw markers - for (BasicMarkerTracker mtracker: markers) { - if (mtracker.visible) { - 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); - } + for (BasicOverlay overlay: overlays) { + overlay.draw(g); } } @@ -268,10 +282,9 @@ public class BasicDrawing extends JPanel implements Drawing { public void clear() { this.graphGraphics.clearRect(0, 0, this.width, this.height); this.overlayGraphics.clearRect(0, 0, this.width, this.height); - this.markers.clear(); + this.overlays.clear(); } - @Override public void drawLine(Point from, Point to) { int x1 = this.projx(from.getLongitude()); int x2 = this.projx(to.getLongitude()); @@ -282,19 +295,6 @@ public class BasicDrawing extends JPanel implements Drawing { this.repaint(); } - @Override - public void drawLine(Point from, Point to, int width) { - setWidth(width); - drawLine(from, to); - } - - @Override - public void drawLine(Point from, Point to, int width, Color color) { - setWidth(width); - setColor(color); - drawLine(from, to); - } - @Override public MarkerTracker drawMarker(Point point) { return drawMarker(point, this.overlayGraphics.getColor()); @@ -302,18 +302,8 @@ public class BasicDrawing extends JPanel implements Drawing { @Override public MarkerTracker drawMarker(Point point, Color color) { - /* - * 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(); - double scale = DEFAULT_MARKER_WIDTH / (double) img.getHeight(); - gr.scale(scale, scale); - BasicMarkerTracker marker = new BasicMarkerTracker(point, img); - this.markers.add(marker); + BasicMarker marker = new BasicMarker(point, color); + this.overlays.add(marker); this.repaint(); return marker; } diff --git a/src/main/org/insa/graphics/drawing/Drawing.java b/src/main/org/insa/graphics/drawing/Drawing.java index 3d788b2..15c569a 100644 --- a/src/main/org/insa/graphics/drawing/Drawing.java +++ b/src/main/org/insa/graphics/drawing/Drawing.java @@ -28,35 +28,6 @@ public interface Drawing { */ public void clear(); - /** - * Draw a line between the two given points with the default color and width. - * - * @param from - * @param to - */ - public void drawLine(Point from, Point to); - - /** - * Draw a line between the two given points with the default color and the given - * width. - * - * @param from - * @param to - * @param width - */ - public void drawLine(Point from, Point to, int width); - - /** - * Draw a line between the two given points with the given color and the given - * width. - * - * @param from - * @param to - * @param width - * @param color - */ - public void drawLine(Point from, Point to, int width, Color color); - /** * Draw a marker at the given point with the default color. * diff --git a/src/main/org/insa/graphics/drawing/MapViewDrawing.java b/src/main/org/insa/graphics/drawing/MapViewDrawing.java index 3952db4..e3bad41 100644 --- a/src/main/org/insa/graphics/drawing/MapViewDrawing.java +++ b/src/main/org/insa/graphics/drawing/MapViewDrawing.java @@ -34,7 +34,6 @@ import org.mapsforge.map.layer.Layers; import org.mapsforge.map.layer.cache.TileCache; import org.mapsforge.map.layer.hills.HillsRenderConfig; import org.mapsforge.map.layer.overlay.Marker; -import org.mapsforge.map.layer.overlay.Polyline; import org.mapsforge.map.layer.renderer.TileRendererLayer; import org.mapsforge.map.model.DisplayModel; import org.mapsforge.map.model.MapViewPosition; @@ -190,24 +189,6 @@ public class MapViewDrawing extends MapView implements Drawing { repaint(); } - @Override - public void drawLine(Point from, Point to) { - drawLine(from, to, 0, null); - } - - @Override - public void drawLine(Point from, Point to, int width) { - drawLine(from, to, width, null); - } - - @Override - public void drawLine(Point from, Point to, int width, Color color) { - Polyline line = new PolylineAutoScaling(width, color); - line.getLatLongs().add(convertPoint(from)); - line.getLatLongs().add(convertPoint(to)); - getLayerManager().getLayers().add(line); - } - @Override public MarkerTracker drawMarker(Point point) { return drawMarker(point, Color.GREEN);