Remove drawLine. Better overlay for BasicDrawing.

This commit is contained in:
Holt59 2018-02-25 18:33:46 +01:00
parent 467253f9bb
commit ae460ff454
3 changed files with 35 additions and 93 deletions

View File

@ -40,16 +40,12 @@ public class BasicDrawing extends JPanel implements Drawing {
*/ */
private static final long serialVersionUID = 96779785877771827L; private static final long serialVersionUID = 96779785877771827L;
public class BasicOverlayTracker implements OverlayTracker { public abstract class BasicOverlay implements OverlayTracker {
// Image of the marker
protected BufferedImage image;
// Visible? // Visible?
protected boolean visible; protected boolean visible;
public BasicOverlayTracker(BufferedImage image) { public BasicOverlay() {
this.image = image;
this.visible = true; this.visible = true;
} }
@ -61,20 +57,29 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public void delete() { public void delete() {
BasicDrawing.this.markers.remove(this); BasicDrawing.this.overlays.remove(this);
BasicDrawing.this.repaint(); 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. // Point of the marker.
private Point point; private Point point;
public BasicMarkerTracker(Point point, BufferedImage image) { // Color of the marker.
super(image); private Color color;
public BasicMarker(Point point, Color color) {
super();
this.point = point; this.point = point;
this.color = color;
} }
@Override @Override
@ -88,6 +93,20 @@ public class BasicDrawing extends JPanel implements Drawing {
BasicDrawing.this.repaint(); 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. // Default path color.
@ -116,7 +135,7 @@ public class BasicDrawing extends JPanel implements Drawing {
private Graphics2D overlayGraphics; private Graphics2D overlayGraphics;
// List of image for markers // List of image for markers
private List<BasicMarkerTracker> markers = new ArrayList<>(); private List<BasicOverlay> overlays = new ArrayList<>();
// Mapping DrawingClickListener -> MouseEventListener // Mapping DrawingClickListener -> MouseEventListener
private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>(); private Map<DrawingClickListener, MouseListener> listenerMapping = new IdentityHashMap<>();
@ -170,13 +189,8 @@ public class BasicDrawing extends JPanel implements Drawing {
g.drawImage(overlayImage, 0, 0, this); g.drawImage(overlayImage, 0, 0, this);
// Draw markers // Draw markers
for (BasicMarkerTracker mtracker: markers) { for (BasicOverlay overlay: overlays) {
if (mtracker.visible) { overlay.draw(g);
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);
}
} }
} }
@ -268,10 +282,9 @@ public class BasicDrawing extends JPanel implements Drawing {
public void clear() { public void clear() {
this.graphGraphics.clearRect(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.overlayGraphics.clearRect(0, 0, this.width, this.height);
this.markers.clear(); this.overlays.clear();
} }
@Override
public void drawLine(Point from, Point to) { public void drawLine(Point from, Point to) {
int x1 = this.projx(from.getLongitude()); int x1 = this.projx(from.getLongitude());
int x2 = this.projx(to.getLongitude()); int x2 = this.projx(to.getLongitude());
@ -282,19 +295,6 @@ public class BasicDrawing extends JPanel implements Drawing {
this.repaint(); 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 @Override
public MarkerTracker drawMarker(Point point) { public MarkerTracker drawMarker(Point point) {
return drawMarker(point, this.overlayGraphics.getColor()); return drawMarker(point, this.overlayGraphics.getColor());
@ -302,18 +302,8 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public MarkerTracker drawMarker(Point point, Color color) { public MarkerTracker drawMarker(Point point, Color color) {
/* BasicMarker marker = new BasicMarker(point, color);
* BufferedImage img = new BufferedImage(DEFAULT_MARKER_WIDTH, this.overlays.add(marker);
* 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);
this.repaint(); this.repaint();
return marker; return marker;
} }

View File

@ -28,35 +28,6 @@ public interface Drawing {
*/ */
public void clear(); 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. * Draw a marker at the given point with the default color.
* *

View File

@ -34,7 +34,6 @@ import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.cache.TileCache; import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.hills.HillsRenderConfig; import org.mapsforge.map.layer.hills.HillsRenderConfig;
import org.mapsforge.map.layer.overlay.Marker; 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.layer.renderer.TileRendererLayer;
import org.mapsforge.map.model.DisplayModel; import org.mapsforge.map.model.DisplayModel;
import org.mapsforge.map.model.MapViewPosition; import org.mapsforge.map.model.MapViewPosition;
@ -190,24 +189,6 @@ public class MapViewDrawing extends MapView implements Drawing {
repaint(); 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 @Override
public MarkerTracker drawMarker(Point point) { public MarkerTracker drawMarker(Point point) {
return drawMarker(point, Color.GREEN); return drawMarker(point, Color.GREEN);