From 92af449178fd1a81fd2c2d5364a85e9035786aa8 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sat, 3 Mar 2018 23:55:53 +0100 Subject: [PATCH] Create new class to have better scaling for markers with MapView. --- .../drawing/components/BasicDrawing.java | 7 ++- .../drawing/components/MapViewDrawing.java | 14 +++--- .../drawing/overlays/MarkerAutoScaling.java | 45 +++++++++++++++++++ .../drawing/overlays/MarkerUtils.java | 11 +---- 4 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 src/main/org/insa/graphics/drawing/overlays/MarkerAutoScaling.java diff --git a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java index ac79f0e..81783da 100644 --- a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java @@ -91,6 +91,9 @@ public class BasicDrawing extends JPanel implements Drawing { private class BasicMarkerOverlay extends BasicOverlay implements MarkerOverlay { + // Marker width and height + public static final int MARKER_WIDTH = 30, MARKER_HEIGHT = 60; + // Point of the marker. private Point point; @@ -98,7 +101,7 @@ public class BasicDrawing extends JPanel implements Drawing { private final Color color; // Image to draw - private final BufferedImage image; + private final Image image; public BasicMarkerOverlay(Point point, Color color) { super(); @@ -129,7 +132,7 @@ public class BasicDrawing extends JPanel implements Drawing { int px = BasicDrawing.this.projx(getPoint().getLongitude()); int py = BasicDrawing.this.projy(getPoint().getLatitude()); - graphics.drawImage(this.image, px - this.image.getWidth() / 2, py - this.image.getHeight(), + graphics.drawImage(this.image, px - MARKER_WIDTH / 2, py - MARKER_HEIGHT, MARKER_WIDTH, MARKER_HEIGHT, BasicDrawing.this); } diff --git a/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java b/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java index d5c1ea3..7fee4be 100644 --- a/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java +++ b/src/main/org/insa/graphics/drawing/components/MapViewDrawing.java @@ -3,6 +3,7 @@ package org.insa.graphics.drawing.components; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; @@ -17,20 +18,19 @@ import org.insa.graph.Point; import org.insa.graphics.drawing.Drawing; import org.insa.graphics.drawing.DrawingClickListener; import org.insa.graphics.drawing.GraphPalette; +import org.insa.graphics.drawing.overlays.MarkerAutoScaling; import org.insa.graphics.drawing.overlays.MarkerOverlay; import org.insa.graphics.drawing.overlays.MarkerUtils; import org.insa.graphics.drawing.overlays.Overlay; import org.insa.graphics.drawing.overlays.PathOverlay; import org.insa.graphics.drawing.overlays.PointSetOverlay; import org.insa.graphics.drawing.overlays.PolylineAutoScaling; -import org.mapsforge.core.graphics.Bitmap; import org.mapsforge.core.graphics.GraphicFactory; 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.core.util.Parameters; -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; @@ -122,10 +122,9 @@ public class MapViewDrawing extends MapView implements Drawing { @Override public void moveTo(Point point) { - Marker marker = (Marker) this.layers[0]; + MarkerAutoScaling marker = (MarkerAutoScaling) this.layers[0]; this.delete(); - marker = new Marker(convertPoint(point), marker.getBitmap(), marker.getHorizontalOffset(), - marker.getVerticalOffset()); + marker = new MarkerAutoScaling(convertPoint(point), marker.getImage()); this.layers[0] = marker; MapViewDrawing.this.getLayerManager().getLayers().add(marker); } @@ -302,8 +301,9 @@ public class MapViewDrawing extends MapView implements Drawing { } protected Marker createMarker(Point point, Color color) { - Bitmap bitmap = new AwtBitmap(MarkerUtils.getMarkerForColor(color)); - return new Marker(convertPoint(point), bitmap, 0, -bitmap.getHeight() / 2); + Image image = MarkerUtils.getMarkerForColor(color); + // Bitmap bitmap = new AwtBitmap(MarkerUtils.getMarkerForColor(color)); + return new MarkerAutoScaling(convertPoint(point), image); } @Override diff --git a/src/main/org/insa/graphics/drawing/overlays/MarkerAutoScaling.java b/src/main/org/insa/graphics/drawing/overlays/MarkerAutoScaling.java new file mode 100644 index 0000000..89e2f90 --- /dev/null +++ b/src/main/org/insa/graphics/drawing/overlays/MarkerAutoScaling.java @@ -0,0 +1,45 @@ +package org.insa.graphics.drawing.overlays; + +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.image.BufferedImage; + +import org.mapsforge.core.graphics.Canvas; +import org.mapsforge.core.model.BoundingBox; +import org.mapsforge.core.model.LatLong; +import org.mapsforge.core.model.Point; +import org.mapsforge.map.awt.graphics.AwtBitmap; +import org.mapsforge.map.layer.overlay.Marker; + +public class MarkerAutoScaling extends Marker { + + // Original image. + private final Image originalImage; + + public MarkerAutoScaling(LatLong latLong, Image image) { + super(latLong, null, 0, 0); + this.originalImage = image; + } + + /** + * @return + */ + public Image getImage() { + return originalImage; + } + + @Override + public synchronized void draw(BoundingBox boundingBox, byte zoomLevel, Canvas canvas, Point topLeftPoint) { + int width = (int) PaintUtils.getStrokeWidth(12, zoomLevel), + height = (int) PaintUtils.getStrokeWidth(24, zoomLevel); + BufferedImage bfd = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); + Graphics2D g = bfd.createGraphics(); + g.drawImage(this.originalImage.getScaledInstance(bfd.getWidth(), bfd.getHeight(), Image.SCALE_SMOOTH), 0, 0, + null); + setBitmap(new AwtBitmap(bfd)); + + setVerticalOffset(-height / 2); + super.draw(boundingBox, zoomLevel, canvas, topLeftPoint); + g.dispose(); + } +} diff --git a/src/main/org/insa/graphics/drawing/overlays/MarkerUtils.java b/src/main/org/insa/graphics/drawing/overlays/MarkerUtils.java index 42e8a88..c4d045f 100644 --- a/src/main/org/insa/graphics/drawing/overlays/MarkerUtils.java +++ b/src/main/org/insa/graphics/drawing/overlays/MarkerUtils.java @@ -1,7 +1,6 @@ package org.insa.graphics.drawing.overlays; import java.awt.Color; -import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.DataInputStream; @@ -17,7 +16,7 @@ public class MarkerUtils { * @param color * @return */ - public static BufferedImage getMarkerForColor(Color color) { + public static Image getMarkerForColor(Color color) { // create image int[][] mask = readMarkerMask(); BufferedImage image = new BufferedImage(mask[0].length, mask.length, BufferedImage.TYPE_4BYTE_ABGR); @@ -31,13 +30,7 @@ public class MarkerUtils { } } - BufferedImage scaleImage = new BufferedImage(28, 48, BufferedImage.TYPE_4BYTE_ABGR); - Graphics2D graphics = scaleImage.createGraphics(); - graphics.drawImage(image.getScaledInstance(28, 48, Image.SCALE_SMOOTH), 0, 0, null); - graphics.dispose(); - - // Create Bitmap and return it. - return scaleImage; + return image; } // Mask cache