Allow modification of color for overlays.

This commit is contained in:
Mikael Capelle 2018-03-05 18:35:01 +01:00
parent 98aba8dd06
commit b77a39e01b
8 changed files with 124 additions and 54 deletions

View File

@ -21,8 +21,8 @@ import java.util.List;
import javax.swing.JPanel; import javax.swing.JPanel;
import org.insa.graph.ArcForward;
import org.insa.graph.Arc; import org.insa.graph.Arc;
import org.insa.graph.ArcForward;
import org.insa.graph.Graph; import org.insa.graph.Graph;
import org.insa.graph.Node; import org.insa.graph.Node;
import org.insa.graph.Path; import org.insa.graph.Path;
@ -54,8 +54,22 @@ public class BasicDrawing extends JPanel implements Drawing {
// Visible? // Visible?
protected boolean visible; protected boolean visible;
public BasicOverlay() { // Color
protected Color color;
public BasicOverlay(Color color) {
this.visible = true; this.visible = true;
this.color = color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public Color getColor() {
return this.color;
} }
@Override @Override
@ -98,14 +112,11 @@ public class BasicDrawing extends JPanel implements Drawing {
// Point of the marker. // Point of the marker.
private Point point; private Point point;
// Color of the marker.
private final Color color;
// Image to draw // Image to draw
private final Image image; private Image image;
public BasicMarkerOverlay(Point point, Color color) { public BasicMarkerOverlay(Point point, Color color) {
super(); super(color);
this.point = point; this.point = point;
this.color = color; this.color = color;
this.image = MarkerUtils.getMarkerForColor(color); this.image = MarkerUtils.getMarkerForColor(color);
@ -117,8 +128,9 @@ public class BasicDrawing extends JPanel implements Drawing {
} }
@Override @Override
public Color getColor() { public void setColor(Color color) {
return color; super.setColor(color);
this.image = MarkerUtils.getMarkerForColor(color);
} }
@Override @Override
@ -144,14 +156,12 @@ public class BasicDrawing extends JPanel implements Drawing {
// List of points // List of points
private final List<Point> points; private final List<Point> points;
// Color for the path
private Color color;
// Origin / Destination markers. // Origin / Destination markers.
private BasicMarkerOverlay origin, destination; private BasicMarkerOverlay origin, destination;
public BasicPathOverlay(List<Point> points, Color color, BasicMarkerOverlay origin, public BasicPathOverlay(List<Point> points, Color color, BasicMarkerOverlay origin,
BasicMarkerOverlay destination) { BasicMarkerOverlay destination) {
super(color);
this.points = points; this.points = points;
this.origin = origin; this.origin = origin;
this.destination = destination; this.destination = destination;
@ -164,7 +174,7 @@ public class BasicDrawing extends JPanel implements Drawing {
if (!points.isEmpty()) { if (!points.isEmpty()) {
graphics.setStroke(new BasicStroke(2)); graphics.setStroke(new BasicStroke(2));
graphics.setColor(color); graphics.setColor(getColor());
Iterator<Point> itPoint = points.iterator(); Iterator<Point> itPoint = points.iterator();
Point prev = itPoint.next(); Point prev = itPoint.next();
@ -206,6 +216,7 @@ public class BasicDrawing extends JPanel implements Drawing {
private int width = DEFAULT_POINT_WIDTH; private int width = DEFAULT_POINT_WIDTH;
public BasicPointSetOverlay() { public BasicPointSetOverlay() {
super(Color.BLACK);
this.image = new BufferedImage(BasicDrawing.this.width, BasicDrawing.this.height, this.image = new BufferedImage(BasicDrawing.this.width, BasicDrawing.this.height,
BufferedImage.TYPE_4BYTE_ABGR); BufferedImage.TYPE_4BYTE_ABGR);
this.graphics = image.createGraphics(); this.graphics = image.createGraphics();
@ -214,6 +225,7 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override @Override
public void setColor(Color color) { public void setColor(Color color) {
super.setColor(color);
this.graphics.setColor(color); this.graphics.setColor(color);
} }

View File

@ -63,11 +63,25 @@ public class MapViewDrawing extends MapView implements Drawing {
// Marker associated. // Marker associated.
protected Layer[] layers; protected Layer[] layers;
public MapViewOverlay(Layer[] layers) { // Current color
protected Color color;
public MapViewOverlay(Layer[] layers, Color color) {
this.layers = layers; this.layers = layers;
for (Layer layer: this.layers) { for (Layer layer: this.layers) {
MapViewDrawing.this.getLayerManager().getLayers().add(layer); MapViewDrawing.this.getLayerManager().getLayers().add(layer);
} }
this.color = color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public Color getColor() {
return this.color;
} }
@Override @Override
@ -101,12 +115,8 @@ public class MapViewDrawing extends MapView implements Drawing {
*/ */
private class MapViewMarkerOverlay extends MapViewOverlay implements MarkerOverlay { private class MapViewMarkerOverlay extends MapViewOverlay implements MarkerOverlay {
// Color of this marker
Color color;
public MapViewMarkerOverlay(Marker marker, Color color) { public MapViewMarkerOverlay(Marker marker, Color color) {
super(new Layer[]{ marker }); super(new Layer[]{ marker }, color);
this.color = color;
} }
@Override @Override
@ -117,8 +127,10 @@ public class MapViewDrawing extends MapView implements Drawing {
} }
@Override @Override
public Color getColor() { public void setColor(Color color) {
return color; super.setColor(color);
MarkerAutoScaling marker = (MarkerAutoScaling) super.layers[0];
marker.setImage(MarkerUtils.getMarkerForColor(color));
} }
@Override @Override
@ -138,12 +150,21 @@ public class MapViewDrawing extends MapView implements Drawing {
*/ */
private class MapViewPathOverlay extends MapViewOverlay implements PathOverlay { private class MapViewPathOverlay extends MapViewOverlay implements PathOverlay {
public MapViewPathOverlay(PolylineAutoScaling path, Marker origin, Marker destination) { public MapViewPathOverlay(PolylineAutoScaling path, MarkerAutoScaling origin,
super(new Layer[]{ path, origin, destination }); MarkerAutoScaling destination) {
super(new Layer[]{ path, origin, destination }, path.getColor());
} }
public MapViewPathOverlay(PolylineAutoScaling path) { public MapViewPathOverlay(PolylineAutoScaling path) {
super(new Layer[]{ path }); super(new Layer[]{ path }, path.getColor());
}
@Override
public void setColor(Color color) {
super.setColor(color);
((PolylineAutoScaling) this.layers[0]).setColor(color);
((MarkerAutoScaling) this.layers[1]).setImage(MarkerUtils.getMarkerForColor(color));
((MarkerAutoScaling) this.layers[2]).setImage(MarkerUtils.getMarkerForColor(color));
} }
} }
@ -155,11 +176,7 @@ public class MapViewDrawing extends MapView implements Drawing {
private class MapViewPointSetOverlay extends MapViewOverlay implements PointSetOverlay { private class MapViewPointSetOverlay extends MapViewOverlay implements PointSetOverlay {
public MapViewPointSetOverlay() { public MapViewPointSetOverlay() {
super(new Layer[0]); super(new Layer[0], Color.BLACK);
}
@Override
public void setColor(Color color) {
} }
@Override @Override
@ -304,9 +321,8 @@ public class MapViewDrawing extends MapView implements Drawing {
repaint(); repaint();
} }
protected Marker createMarker(Point point, Color color) { protected MarkerAutoScaling createMarker(Point point, Color color) {
Image image = MarkerUtils.getMarkerForColor(color); Image image = MarkerUtils.getMarkerForColor(color);
// Bitmap bitmap = new AwtBitmap(MarkerUtils.getMarkerForColor(color));
return new MarkerAutoScaling(convertPoint(point), image); return new MarkerAutoScaling(convertPoint(point), image);
} }
@ -373,7 +389,8 @@ public class MapViewDrawing extends MapView implements Drawing {
} }
PathOverlay overlay = null; PathOverlay overlay = null;
if (markers) { if (markers) {
Marker origin = createMarker(path.getOrigin().getPoint(), DEFAULT_PATH_COLOR), MarkerAutoScaling origin = createMarker(path.getOrigin().getPoint(),
DEFAULT_PATH_COLOR),
destination = createMarker(path.getDestination().getPoint(), destination = createMarker(path.getDestination().getPoint(),
DEFAULT_PATH_COLOR); DEFAULT_PATH_COLOR);
overlay = new MapViewPathOverlay(line, origin, destination); overlay = new MapViewPathOverlay(line, origin, destination);

View File

@ -14,28 +14,39 @@ import org.mapsforge.map.layer.overlay.Marker;
public class MarkerAutoScaling extends Marker { public class MarkerAutoScaling extends Marker {
// Original image. // Original image.
private final Image originalImage; private Image image;
public MarkerAutoScaling(LatLong latLong, Image image) { public MarkerAutoScaling(LatLong latLong, Image image) {
super(latLong, null, 0, 0); super(latLong, null, 0, 0);
this.originalImage = image; this.image = image;
} }
/** /**
* @return * Set a new image for this marker overlay
*
* @param image New image to set.
*/
public void setImage(Image image) {
this.image = image;
}
/**
* @return Current image (marker) of this overlay.
*/ */
public Image getImage() { public Image getImage() {
return originalImage; return image;
} }
@Override @Override
public synchronized void draw(BoundingBox boundingBox, byte zoomLevel, Canvas canvas, Point topLeftPoint) { public synchronized void draw(BoundingBox boundingBox, byte zoomLevel, Canvas canvas,
Point topLeftPoint) {
int width = (int) PaintUtils.getStrokeWidth(8, zoomLevel), int width = (int) PaintUtils.getStrokeWidth(8, zoomLevel),
height = (int) PaintUtils.getStrokeWidth(16, zoomLevel); height = (int) PaintUtils.getStrokeWidth(16, zoomLevel);
BufferedImage bfd = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); BufferedImage bfd = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = bfd.createGraphics(); Graphics2D g = bfd.createGraphics();
g.drawImage(this.originalImage.getScaledInstance(bfd.getWidth(), bfd.getHeight(), Image.SCALE_SMOOTH), 0, 0, g.drawImage(
null); this.image.getScaledInstance(bfd.getWidth(), bfd.getHeight(), Image.SCALE_SMOOTH),
0, 0, null);
setBitmap(new AwtBitmap(bfd)); setBitmap(new AwtBitmap(bfd));
setVerticalOffset(-height / 2); setVerticalOffset(-height / 2);

View File

@ -1,16 +1,9 @@
package org.insa.graphics.drawing.overlays; package org.insa.graphics.drawing.overlays;
import java.awt.Color;
import org.insa.graph.Point; import org.insa.graph.Point;
public interface MarkerOverlay extends Overlay { public interface MarkerOverlay extends Overlay {
/**
* @return Color associated with this marker.
*/
public Color getColor();
/** /**
* @return The point associated with this marker. * @return The point associated with this marker.
*/ */

View File

@ -1,7 +1,21 @@
package org.insa.graphics.drawing.overlays; package org.insa.graphics.drawing.overlays;
import java.awt.Color;
public interface Overlay { public interface Overlay {
/**
* Set the color of this overlay.
*
* @param color New color for the overlay.
*/
public void setColor(Color color);
/**
* @return The current color of this overlay.
*/
public Color getColor();
/** /**
* Show or hide this marker - A marker should be visible when created. * Show or hide this marker - A marker should be visible when created.
* *

View File

@ -11,11 +11,24 @@ public class PaintUtils {
private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE; private static final GraphicFactory GRAPHIC_FACTORY = AwtGraphicFactory.INSTANCE;
/** /**
* Convert the given AWT color to a mapsforge compatible color.
*
* @param color * @param color
* @return * @return
*/ */
public static int convertColor(Color color) { public static int convertColor(Color color) {
return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(), color.getBlue()); return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(),
color.getBlue());
}
/**
* Convert the given mapsforge color to an AWT Color.
*
* @param color
* @return
*/
public static Color convertColor(int color) {
return new Color(color, true);
} }
/** /**
@ -38,4 +51,5 @@ public class PaintUtils {
} }
return width * mul; return width * mul;
} }
} }

View File

@ -6,13 +6,6 @@ import org.insa.graph.Point;
public interface PointSetOverlay extends Overlay { public interface PointSetOverlay extends Overlay {
/**
* Set the color of this overlay for future addPoint().
*
* @param color
*/
public void setColor(Color color);
/** /**
* Set the width of this overlay for future addPoint(). * Set the width of this overlay for future addPoint().
* *

View File

@ -31,6 +31,22 @@ public class PolylineAutoScaling extends Polyline {
this.width = width; this.width = width;
} }
/**
* Set the color for this polyline.
*
* @param color New color for this polyline.
*/
public void setColor(Color color) {
getPaintStroke().setColor(PaintUtils.convertColor(color));
}
/**
* @return Color of this polyline.
*/
public Color getColor() {
return PaintUtils.convertColor(getPaintStroke().getColor());
}
/** /**
* @param point * @param point
*/ */