Clean code.

This commit is contained in:
Holt59
2018-03-10 20:39:21 +01:00
parent 84f01ce47e
commit 094a2331af
20 changed files with 343 additions and 547 deletions

View File

@@ -8,19 +8,10 @@ import org.insa.graph.RoadInformation.RoadType;
public class BasicGraphPalette implements GraphPalette {
// Color types for arc.
static final Color motorway = Color.RED;
static final Color bigroad = new Color(255, 105, 0);
static final Color smallroad = new Color(255, 234, 0);
static final Color coastline = Color.BLUE;
// Default point width
static final int DEFAULT_POINT_WIDTH = 1;
/**
*
*/
public BasicGraphPalette() {
}
private static final Color MOTORWAY_COLOR = Color.RED;
private static final Color BIG_ROAD_COLOR = new Color(255, 105, 0);
private static final Color SMALL_ROAD_COLOR = new Color(255, 234, 0);
private static final Color COASTLINE_COLOR = Color.BLUE;
@Override
public int getDefaultPointWidth() {
@@ -35,19 +26,16 @@ public class BasicGraphPalette implements GraphPalette {
@Override
public Color getColorForArc(Arc arc) {
RoadType type = arc.getRoadInformation().getType();
Color color = Color.BLACK;
switch (type) {
case MOTORWAY:
color = motorway;
break;
return MOTORWAY_COLOR;
case TRUNK:
case PRIMARY:
case SECONDARY:
case MOTORWAY_LINK:
case TRUNK_LINK:
case PRIMARY_LINK:
color = bigroad;
break;
return BIG_ROAD_COLOR;
case SECONDARY_LINK:
case TERTIARY:
case RESIDENTIAL:
@@ -58,14 +46,12 @@ public class BasicGraphPalette implements GraphPalette {
case PEDESTRIAN:
case CYCLEWAY:
case TRACK:
color = smallroad;
break;
return SMALL_ROAD_COLOR;
case COASTLINE:
color = coastline;
break;
return COASTLINE_COLOR;
}
return color;
return Color.BLACK;
}
@Override

View File

@@ -14,31 +14,34 @@ public interface Drawing {
/**
* Add a listener to click to this drawing.
*
* @param listener
* @param listener DrawingClickListener to add to this Drawing.
*/
public void addDrawingClickListener(DrawingClickListener listener);
/**
* Remove the given listener from the drawing.
*
* @param listener
* @param listener DrawingClickListener to remove from this Drawing.
*/
public void removeDrawingClickListener(DrawingClickListener listener);
/**
* Clear the drawing.
* Clear the drawing (overlays and underlying graph/map).
*/
public void clear();
/**
* Remove overlays from the drawing.
* Remove overlays from the drawing (do not remove the underlying graph/map).
*/
public void clearOverlays();
/**
* Draw the given point with the given color.
* Draw a marker at the given position with the given color.
*
* @param point
* @param point Position of the marker to draw.
* @param color Color of the marker to draw.
*
* @return A MarkerOverlay instance representing the newly drawn marker.
*/
public MarkerOverlay drawMarker(Point point, Color color);
@@ -48,65 +51,86 @@ public interface Drawing {
*
* PointSetOverlay are heavy memory resources, do not use one for each point!
*
* @return A new PointSetOverlay for this drawing.
*/
public PointSetOverlay createPointSetOverlay();
/**
* Create a new PointSetOverlay with the original width and color that can be
* used to add overlay points to this drawing.
* Create a new PointSetOverlay with the given initial width and color that can
* be used to add overlay points to this drawing.
*
* PointSetOverlay are heavy memory resources, do not use one for each point!
*
* @param width Initial width of points in the overlay.
* @param color Initial width of points in the overlay.
*
* @return A new PointSetOverlay for this drawing.
*/
public PointSetOverlay createPointSetOverlay(int width, Color color);
/**
* Draw the given graph using the given palette.
*
* @param graph
* @param palette
* @param graph Graph to draw.
* @param palette Palette to use to draw the graph.
*
* @see BasicGraphPalette
* @see BlackAndWhiteGraphPalette
*/
public void drawGraph(Graph graph, GraphPalette palette);
/**
* Draw the given graph using a default palette specific to the implementation.
*
* @param graph
* @param graph Graph to draw.
*/
public void drawGraph(Graph graph);
/**
* Draw a path using the given color.
*
* @param path
* @param color
* @param markers Show origin and destination markers.
* @return
* @param path Path to draw.
* @param color Color of the path to draw.
* @param markers true to show origin and destination markers.
*
* @return A PathOverlay instance representing the newly drawn path.
*/
public PathOverlay drawPath(Path path, Color color, boolean markers);
/**
* Draw a path using the given color with markers.
* Draw a path with both origin and destination markers using the given color.
*
* @param path
* @param color
* @param path Path to draw.
* @param color Color of the path to draw.
*
* @return A PathOverlay instance representing the newly drawn path.
*
* @see Drawing#drawPath(Path, Color, boolean)
*/
public PathOverlay drawPath(Path path, Color color);
/**
* Draw a path using a default color specific to the implementation
*
* @param path Path to draw.
* @param markers true to show origin and destination markers.
*
* @param path
* @param markers Show origin and destination markers.
* @return A PathOverlay instance representing the newly drawn path.
*
* @see Drawing#drawPath(Path, Color, boolean)
*/
public PathOverlay drawPath(Path path, boolean markers);
/**
* Draw a path using a default color specific to the implementation
* Draw a path with both origin and destination markers using a default color
* specific to the implementation
*
*
* @param path
* @param path Path to draw.
*
* @return A PathOverlay instance representing the newly drawn path.
*
* @see Drawing#drawPath(Path, Color, boolean)
*/
public PathOverlay drawPath(Path path);

View File

@@ -7,7 +7,7 @@ public interface DrawingClickListener {
/**
* Event triggered when a click is made on the map.
*
* @param point
* @param point Position (on the map) of the mouse click.
*/
public void mouseClicked(Point point);

View File

@@ -17,14 +17,14 @@ public interface GraphPalette {
public Color getDefaultPointColor();
/**
* @param arc
* @param arc Arc for which color should be retrieved.
*
* @return Color associated with the given arc.
*/
public Color getColorForArc(Arc arc);
/**
* @param arc
* @param arc Arc for which width should be retrieved.
*
* @return Width associated with the given arc.
*/

View File

@@ -449,11 +449,15 @@ public class BasicDrawing extends JPanel implements Drawing {
* Return the longitude and latitude corresponding to the given position of the
* MouseEvent.
*
* @param event
* @param event MouseEvent from which longitude/latitude should be retrieved.
*
* @return
* @return Point representing the projection of the MouseEvent position in the
* graph/map.
*
* @throws NoninvertibleTransformException if the actual transformation is
* invalid.
*/
public Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException {
protected Point getLongitudeLatitude(MouseEvent event) throws NoninvertibleTransformException {
// Get the point using the inverse transform of the Zoom/Pan object, this gives
// us
// a point within the drawing box (between [0, 0] and [width, height]).

View File

@@ -111,7 +111,7 @@ public class MapZoomControls {
/**
* Add a zoom-in listener.
*
* @param listener
* @param listener Zoom-in listener to add to this MapZoomControls instance.
*/
public void addZoomInListener(ActionListener listener) {
this.zoomInListeners.add(listener);
@@ -120,7 +120,7 @@ public class MapZoomControls {
/**
* Add a zoom-out listener.
*
* @param listener
* @param listener Zoom-out listener to add to this MapZoomControls instance.
*/
public void addZoomOutListener(ActionListener listener) {
this.zoomOutListeners.add(listener);
@@ -134,9 +134,9 @@ public class MapZoomControls {
}
/**
* Set the current zoom level.
* Set the current zoom level without requesting a redraw.
*
* @param level
* @param level Zoom level to set.
*/
public void setZoomLevel(int level) {
this.currentLevel = level;
@@ -161,10 +161,10 @@ public class MapZoomControls {
* Check if a point is contained inside an element of this zoom controls, useful
* to avoid spurious click listeners.
*
* @param point
* @param point Point to check.
*
* @return true if the given point correspond to an element of this zoom
* controls.
* controls.
*/
public boolean contains(Point point) {
return zoomInRect.contains(point) || zoomOutRect.contains(point);

View File

@@ -11,11 +11,27 @@ import org.mapsforge.core.model.Point;
import org.mapsforge.map.awt.graphics.AwtBitmap;
import org.mapsforge.map.layer.overlay.Marker;
/**
* Class extending the default Mapsforge's {@link Marker} with auto-scaling.
*
* Mapsforge's Markers do not scale with zoom level, this class aims at
* correcting this. Internally, this image stores an {@link Image} instance and
* scale it when a redraw is requested.
*
* @see MarkerUtils#getMarkerForColor(java.awt.Color)
* @see PaintUtils#getStrokeWidth(int, byte)
*/
public class MarkerAutoScaling extends Marker {
// Original image.
private Image image;
/**
* Create a new MarkerAutoScaling at the given position with the given image.
*
* @param latLong Initial position of the marker.
* @param image Image for this marker.
*/
public MarkerAutoScaling(LatLong latLong, Image image) {
super(latLong, null, 0, 0);
this.image = image;

View File

@@ -5,12 +5,14 @@ import org.insa.graph.Point;
public interface MarkerOverlay extends Overlay {
/**
* @return The point associated with this marker.
* @return The current position of this marker.
*/
public Point getPoint();
/**
* Move this marker to the specified location.
*
* @param point New position for the marker.
*/
public void moveTo(Point point);

View File

@@ -11,15 +11,17 @@ public class MarkerUtils {
private static final String MARKER_MASK_FILE = "res/marker_mask.bin";
/**
* Create a Bitmap representing a marker of the given color.
* Create an Image representing a marker of the given color.
*
* @param color
* @return
* @param color Color of the marker.
*
* @return A new Image representing a marker with the given 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);
BufferedImage image = new BufferedImage(mask[0].length, mask.length,
BufferedImage.TYPE_4BYTE_ABGR);
// Color[] map = getColorMapping(color);
int rgb = color.getRGB() & 0x00ffffff;

View File

@@ -4,6 +4,8 @@ import java.awt.Color;
import org.mapsforge.core.graphics.GraphicFactory;
import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
import org.mapsforge.map.layer.overlay.Marker;
import org.mapsforge.map.layer.overlay.Polyline;
public class PaintUtils {
@@ -13,8 +15,9 @@ public class PaintUtils {
/**
* Convert the given AWT color to a mapsforge compatible color.
*
* @param color
* @return
* @param color AWT color to convert.
*
* @return Integer value representing a corresponding mapsforge color.
*/
public static int convertColor(Color color) {
return GRAPHIC_FACTORY.createColor(color.getAlpha(), color.getRed(), color.getGreen(),
@@ -24,16 +27,23 @@ public class PaintUtils {
/**
* Convert the given mapsforge color to an AWT Color.
*
* @param color
* @return
* @param color Integer value representing a mapsforge color.
*
* @return AWT color corresponding to the given value.
*/
public static Color convertColor(int color) {
return new Color(color, true);
}
/**
* @param width
* @return
* Compute an updated value for the given width at the given zoom level. This
* function can be used to automatically scale {@link Polyline} or
* {@link Marker} when zooming (which is not done by default in Mapsforge).
*
* @param width Original width to convert.
* @param zoomLevel Zoom level for which the width should be computed.
*
* @return Actual width at the given zoom level.
*/
public static float getStrokeWidth(int width, byte zoomLevel) {
float mul = 1;

View File

@@ -9,48 +9,61 @@ public interface PointSetOverlay extends Overlay {
/**
* Set the width of this overlay for future addPoint().
*
* @param width
* @param width New default width for this overlay.
*/
public void setWidth(int width);
/**
* Set color and width for this overlay for future addPoint().
*
* @param width
* @param color
* @param width New default width for this overlay.
* @param color New default color for this overlay.
*/
public void setWidthAndColor(int width, Color color);
/**
* Add a new point using the current width and color.
*
* @param point
* @param point Position of the point to add.
*
* @see #setWidth(int)
* @see #setColor(Color)
*/
public void addPoint(Point point);
/**
* Set the current width and then add a new point.
*
* @param point
* @param width
* @param point Position of the point to add.
* @param width New default width for this overlay.
*
* @see #setWidth(int)
* @see PointSetOverlay#addPoint(Point)
*/
public void addPoint(Point point, int width);
/**
* Set the current color and then add a new point.
*
* @param point
* @param color
* @param point Position of the point to add.
* @param color New default color for this overlay.
*
* @see #setColor(Color)
* @see PointSetOverlay#addPoint(Point)
*/
public void addPoint(Point point, Color color);
/**
* Add a new point to this set at the given location, with the given color and
* width, and update the current color.
* Add a new point at the given location, with the given color and width, and
* update the current width and color.
*
* @param point
* @param width
* @param color
* @param point Position of the point to add.
* @param width New default width for this overlay.
* @param color New default color for this overlay.
*
* @see #setWidth(int)
* @see #setColor(Color)
* @see PointSetOverlay#addPoint(Point)
*/
public void addPoint(Point point, int width, Color color);

View File

@@ -12,6 +12,15 @@ import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.awt.graphics.AwtGraphicFactory;
import org.mapsforge.map.layer.overlay.Polyline;
/**
* Class extending the default Mapsforge's {@link Polyline} with auto-scaling.
*
* Mapsforge's Polylines do not scale with zoom level, this class aims at
* correcting this. When a redraw is requested, the width of the line is
* recomputed for the current zoom level.
*
* @see PaintUtils#getStrokeWidth(int, byte)
*/
public class PolylineAutoScaling extends Polyline {
// Graphic factory.
@@ -21,8 +30,12 @@ public class PolylineAutoScaling extends Polyline {
private final int width;
/**
* @param width
* @param color
* Create a new PolylineAutoScaling with the given width and color.
*
* @param width Original width of the line (independent of the zoom level).
* @param color Color of the line.
*
* @see PaintUtils#getStrokeWidth(int, byte)
*/
public PolylineAutoScaling(int width, Color color) {
super(GRAPHIC_FACTORY.createPaint(), GRAPHIC_FACTORY);
@@ -48,14 +61,14 @@ public class PolylineAutoScaling extends Polyline {
}
/**
* @param point
* @param point Point to add to this line.
*/
public void add(Point point) {
getLatLongs().add(new LatLong(point.getLatitude(), point.getLongitude()));
}
/**
* @param points
* @param points Points to add to this line.
*/
public void add(List<Point> points) {
for (Point point: points) {