Create new class to have better scaling for markers with MapView.

This commit is contained in:
Holt59 2018-03-03 23:55:53 +01:00
parent 55dfcbd0fe
commit 92af449178
4 changed files with 59 additions and 18 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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();
}
}

View File

@ -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